How to Simulate an EC2 Server Using Docker

Sujal GoswamiSujal Goswami
3 min read

In this blog post, we will walk through the steps to create a Docker container that simulates an EC2 instance. This environment will allow you to SSH into the container, mimicking the experience of managing an EC2 instance on AWS. This setup is perfect for those who want to practice server management without incurring cloud costs.

Prerequisites

  • Docker installed on your local machine. You can download it from the Docker website.

  • Basic knowledge of Docker and SSH.

Step-by-Step Guide

  1. Create a Dockerfile

First, we'll create a Dockerfile to set up an Ubuntu environment with SSH capabilities.

# Use an official Ubuntu base image
FROM ubuntu:20.04

# Prevents interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive

# Update the package list and install necessary packages
RUN apt-get update && apt-get install -y \
    openssh-server \
    sudo \
    vim \
    net-tools \
    && rm -rf /var/lib/apt/lists/*

# Create a user with a chosen username and password
ARG USERNAME
ARG PASSWORD

RUN useradd -m $USERNAME && echo "$USERNAME:$PASSWORD" | chpasswd && adduser $USERNAME sudo

# Set up SSH server configuration
RUN mkdir /var/run/sshd

# Allow password authentication
RUN sed -i 's/#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config

# Expose SSH port
EXPOSE 22

# Start the SSH service
CMD ["/usr/sbin/sshd", "-D"]
  1. Build the Docker Image

Navigate to the directory containing your Dockerfile and build the Docker image. Replace myuser and mypassword with your desired username and password.

docker build --build-arg USERNAME=myuser --build-arg PASSWORD=mypassword -t my_ubuntu_ssh .
  1. Run the Docker Container with a Specific IP Address

    To simulate an environment similar to an EC2 instance, we'll create a Docker network and assign a specific IP address to our container.

docker network create --subnet=172.18.0.0/16 my_network
docker run -d --name my_ssh_container --network my_network --ip 172.18.0.22 -p 2222:22 my_ubuntu_ssh
  1. Access the Container via SSH

You can now SSH into the container using the IP address assigned within the Docker network.

ssh myuser@172.18.0.22 -p 2222
  1. Simulating Public IP Access

To simulate accessing the container as if it had a public IP address, you can use port forwarding from your local machine's IP:

  1. Find Your Local Machine's IP Address: On Windows, use ipconfig, or on macOS/Linux, use ifconfig or ip a to find your local machine's IP address (e.g., 192.168.1.100).

  2. Access the Container via SSH Using Your Local Machine's IP Address: SSH into the container using your local machine's IP address and the forwarded port:

     ssh myuser@192.168.1.100 -p 2222
    

(Optional) Add SSH Key Authentication

For added security, you can set up SSH key authentication:

  1. Generate SSH keys on your host machine:

     ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  2. Copy the public key to the Docker container:

     docker exec -it my_ssh_container bash
     mkdir /home/myuser/.ssh
     echo "your-public-key" >> /home/myuser/.ssh/authorized_keys
     chown -R myuser:myuser /home/myuser/.ssh
     chmod 600 /home/myuser/.ssh/authorized_keys
     exit
    
  3. SSH into the container using your private key:

     ssh -i /path/to/your/private/key myuser@localhost -p 2222
    

Conclusion

By following these steps, you can create a Docker container that mimics an EC2 instance with its own IP address within a Docker network. While this setup doesn't provide a public IP like an actual EC2 instance, it allows you to practice managing and accessing an isolated server environment via SSH on your local machine. If you need to expose the container to the internet, consider using a cloud provider to launch a real VPS or EC2 instance.

0
Subscribe to my newsletter

Read articles from Sujal Goswami directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sujal Goswami
Sujal Goswami