How to deploy a Docker container on an Amazon EC2 Instance

Search for a command to run...

No comments yet. Be the first to comment.
It’s been a few weeks since the completion of Amazon Web Services (AWS) re:Invent. I wanted to share some thoughts. Unfortunately, Amazon Web Services (AWS) re: Invent 2025 didn’t go as planned. Before the event, I had planned all my sessions, the ke...

The first self-replicating worm that compromised npm packages with cloud token-stealing malware

We all know that since the advent of the COVID-19 pandemic, businesses are embracing cloud computing for its scalability, flexibility, and cost savings. However, these advantages come with a growing attack surface, and cloud security flaws have becom...

Imagine you're in a security assessment with your Compliance Team or an External Assessor, and they point out that your AWS environment CloudWatch Log doesn't have a retention period set. CloudWatch Logs retention is an important part of AWS security...

Another incredible year at AWS re:Inforce, and the 2025 edition in Philadelphia did not disappoint. Over the three days, I gained a deeper understanding of AWS security, reconnected with familiar faces who have become like family, and welcomed new pr...

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
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.

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
sudo yum update -y
sudo yum install docker
sudo systemctl start docker
sudo systemctl enable docker
You'll need to log out and back in for group changes to take effect.
sudo usermod -aG docker ec2-user
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.
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.
my-nginx-container:docker run -d --name my-nginx-container -p 8080:80 nginx
Let me break down the above command
-d Runs the container in detached mode (in the background)
—name allows you to name your container whatever you prefer. In this lab, my container is namedmy-nginx-container:
-p 8080:80 maps the instance’s port 8080 to the container’s port 80
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 ps
List all Containers, including stopped containers : docker ps -a
Stop the Container: docker stop my-nginx-container
Start the Container Again: docker start my-nginx-container
Restart the Container: docker restart my-nginx-container
Yay! Congratulations, you have just completed this lab.