How to deploy a Docker container on an Amazon EC2 Instance

Imagine you've just started learning how to create Docker containers on your local machine. Now, what if you could do the same on your EC2 instance without worrying about whether your PC will crash? Don’t get me wrong about having a personal lab for practice and all. It’s just that cloud infrastructure has made many things super flexible, especially if you’re learning new technologies or want to master existing ones.
Docker containers, Images, and everything about Microservices have gained so much popularity in different industry domains, due to their persistent nature.
This article shows you how to do the following:
How to create an EC2 instance
SSH into an EC2 instance
Install Docker Desktop on an EC2 instance
Run a container application on an EC2 instance
Step 1: Create/Provision an EC2 Instance
AWS offers several ways to create EC2 instances, such as using the console, CLI, CDK, Python boto3, CloudFormation, and Terraform. For simplicity in this lab, we will use the console.
Access EC2 in the AWS Console
In the AWS Management Console, search for EC2 and select the service. Click "Launch Instance" to begin creating a new virtual server.


Choose an Amazon Machine Image (AMI)
Select an Amazon Linux AMI, which comes with pre-configured settings for your OS and application environment. For this lab, we will choose the Amazon Linux OS.
Select an Instance Type
For small-scale applications or testing, a t2.micro instance is a cost-effective option.
Configure Security Group
Set up a security group to manage traffic to your instance. Add rules to allow:
HTTP (port 80) & Port 8080 (We will use this for the container host port )
HTTPS (port 443)
Create a Key Pair
Generate and download a secure SSH key pair. You'll use this to remotely access your EC2 instance. Store the private key file in a secure location.
Launch the Instance
Once the above steps are complete, launch your instance. After deployment, go to the EC2 Instances page to find your instance’s public IP address.

Step 2: Install Docker on your EC2 Instance
NOTE: This section of the lab assumes that you are now familiar with Provisioning EC2 Instances and can manage EC2 Instances.
Connect to Your EC2 Instance via SSH
Use your terminal (Mac/Linux) or an SSH client (like PuTTY for Windows) to connect:
From your terminal, go to the directory where your EC2 key pair was downloaded. In this lab, my key pair is named. docker-container.pem After that, run the following command:
NOTE: Replace the below IP Address with your EC2 instance's Public IPv4 address
chmod 400 docker-container.pem
ssh -i docker-container.pem ec2-user@13.222.94.175
Install Docker on EC2
- When you SSH into your EC2 Instance and establish a connection, then update the package manager index by running the following commands in white color:
sudo yum update -y
- Install the Docker package by running the following command:
sudo yum install docker
- Start and enable the Docker service by running the following command:
sudo systemctl start docker
sudo systemctl enable docker
- Add your EC2 instance user to the Docker group. This will let you run Docker commands without getting access denied errors.
You'll need to log out and back in for group changes to take effect.
sudo usermod -aG docker ec2-user
- Verify that Docker is running by running the following command:
sudo service docker status
You should see something like the below output

After completing the steps above, Docker should be installed and running on your Amazon EC2 instance. You can now use Docker to run and manage containers on your EC2 instance.
Step 3: Run a Docker Container
Great job so far! You've successfully created an instance, connected via SSH, and installed Docker Desktop on the instance. Now, we're at the final part of this lab: running a sample NGINX Docker container. P.S.: To learn more about Docker and Docker containers, please visit the Docker-wiki-page.
- Let’s run an NGINX container on port 8080 and name it
my-nginx-container:
docker run -d --name my-nginx-container -p 8080:80 nginx
Let me break down the above command
-dRuns the container in detached mode (in the background)—nameallows you to name your container whatever you prefer. In this lab, my container is namedmy-nginx-container:-p 8080:80maps the instance’s port8080to the container’s port80
You can now open a browser and visit your EC2 instance's public IP with port 8080 to see the NGINX welcome page. Here is my URL: http://13.222.94.175:8080 By the time you read this, I will have disconnected the EC2 instance.

Here are some useful Docker commands to monitor and manage your container:
List Running Containers
docker psList all Containers, including stopped containers :
docker ps -aStop the Container:
docker stop my-nginx-containerStart the Container Again:
docker start my-nginx-containerRestart the Container:
docker restart my-nginx-container
Yay! Congratulations, you have just completed this lab.




