
From Docker to AWS: Deploying Your First Container on Amazon EC2
Learn how to launch an Amazon EC2 instance, install Docker, run a container, configure network access, and make your containerized application accessible from the internet.
From Docker to AWS: Deploying Your First Container on Amazon EC2
If you've learned Docker locally, the next question is usually:
How do I take my Docker container and run it on a cloud server?
One simple way to understand this process is to use Amazon EC2.
In this guide, we'll go through the basic workflow:
1
2
3
4
5
6
7
8
9
Application
↓
Docker Image
↓
Docker Container
↓
Amazon EC2
↓
InternetThe goal isn't to build a production-ready architecture. Instead, this is a beginner-friendly exercise to understand how Docker and AWS EC2 work together.
1. What are Docker and EC2?
Before deploying anything, let's understand the two technologies.
Docker
Docker packages an application and its dependencies into a container image.
That image can then be used to create containers that run consistently across different environments.
For example:
1
2
3
4
5
Dockerfile
↓
Docker Image
↓
Docker ContainerAmazon EC2
Amazon Elastic Compute Cloud (EC2) provides virtual servers in the AWS Cloud.
Instead of running your Docker container only on your laptop, you can launch an EC2 instance and run Docker there.
So the relationship is:
1
2
3
4
5
6
7
8
9
10
11
12
13
Your Laptop
│
│ Docker
▼
Docker Image
│
│ Deploy
▼
Amazon EC2
│
│ Docker
▼
Running ContainerAWS provides documentation for launching EC2 instances and connecting to them. AWS Documentation
2. What do we need?
For this tutorial, we'll use:
- AWS account
- Amazon EC2
- Amazon Linux 2023
- Docker
- Security Groups
- A simple Docker image
AWS specifically documents using an Amazon Linux 2023 EC2 instance with Docker for container work. AWS Documentation
3. Launch an EC2 instance
Go to the Amazon EC2 console and choose Launch instance.
For a beginner experiment, you can use:
AMI:
1
Amazon Linux 2023Choose an instance type that is eligible for your account's applicable free-tier/credit benefits.
You will also need a way to connect to the instance.
You can use EC2 Instance Connect or SSH with a key pair. AWS documents both approaches. AWS Documentation
4. Configure the Security Group
This is an important step.
A security group acts as a firewall for your EC2 instance and controls inbound and outbound traffic. AWS Documentation
For this tutorial, you need access for:
| Purpose | Protocol | Port |
|---|---|---|
| SSH | TCP | 22 |
| Web application | TCP | 80 |
For SSH, it is better to restrict the source to your IP address instead of allowing SSH from everyone.
AWS specifically warns that allowing SSH from
0.0.0.0/0 is unsafe for production environments. AWS Documentation For HTTP:
1
2
3
Type: HTTP
Port: 80
Source: 0.0.0.0/0Port 80 can be publicly accessible because it is the port through which visitors will access the web application. AWS's EC2 documentation shows HTTP port 80 as the standard inbound rule for web servers. AWS Documentation
5. Connect to your EC2 instance
Once the instance is running, select it and choose Connect.
If using SSH, the command will look similar to:
1
ssh -i "your-key.pem" ec2-user@YOUR_PUBLIC_IPThe exact command will be provided by the EC2 console for your instance.
For Amazon Linux, the default username is typically:
1
ec2-userAWS documents this connection process in its EC2 getting-started tutorial. AWS Documentation
6. Install Docker
After connecting to the EC2 instance, install Docker.
For Amazon Linux, AWS's container documentation provides the following workflow:
1
2
3
sudo yum update -y
sudo yum install docker -y
sudo service docker startThen add
ec2-user to the Docker group:1
sudo usermod -a -G docker ec2-userLog out and reconnect so the group membership takes effect.
Then verify Docker:
1
docker --versionYou can also check:
1
docker infoAWS documents this process for running Docker on an Amazon Linux 2023 EC2 instance. AWS Documentation
7. Run your first container
Now comes the exciting part.
Let's use the official NGINX image as a simple example:
1
docker run -d -p 80:80 nginxLet's break down this command:
1
docker runCreates and starts a container.
1
-dRuns the container in detached mode.
1
-p 80:80Maps:
1
EC2 port 80 → Container port 80And:
1
nginxis the Docker image we're running.
You can check the running container with:
1
docker psYou should see something similar to:
1
2
CONTAINER ID IMAGE PORTS
xxxxxxxxxxxx nginx 0.0.0.0:80->80/tcp8. Access the application
Now open a browser and enter your EC2 instance's public IPv4 address:
1
http://YOUR_EC2_PUBLIC_IPIf everything is configured correctly, you should see the NGINX welcome page.
🎉 Your first Docker container is now running on an AWS EC2 instance.
9. What actually happened?
This is the most important part to understand.
When you opened:
1
http://YOUR_EC2_PUBLIC_IPthe request travelled approximately like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Internet
│
▼
EC2 Public IP
│
▼
Security Group
Port 80
│
▼
EC2 Instance
│
▼
Docker
│
▼
NGINX Container
│
▼
NGINX Web Server
│
▼
ResponseUnderstanding this flow is more valuable than simply memorizing the Docker command.
10. Understanding Port Mapping
One of the most important Docker concepts in this example is:
1
-p 80:80The format is:
1
-p HOST_PORT:CONTAINER_PORTSo:
1
-p 80:80means:
1
2
3
EC2 port 80
↓
Container port 80For example, if your application runs on port 8000 inside the container:
1
docker run -d -p 80:8000 your-imageThe user accesses:
1
http://YOUR_EC2_PUBLIC_IPwhile the application continues listening on port
8000 inside the container.11. Useful Docker commands
Once your container is running, these commands are useful:
View running containers
1
docker psView all containers
1
docker ps -aStop a container
1
docker stop CONTAINER_IDStart a stopped container
1
docker start CONTAINER_IDView container logs
1
docker logs CONTAINER_IDRemove a container
1
docker rm CONTAINER_IDView downloaded images
1
docker imagesThese basic commands are enough to start experimenting with containers on EC2.
12. What about my own application?
Instead of using NGINX, you can build your own Docker image.
For example, a typical project might look like:
1
2
3
4
5
my-app/
├── Dockerfile
├── requirements.txt
├── app.py
└── ...You can build an image:
1
docker build -t my-app .Then run it:
1
docker run -d -p 80:8000 my-appThe exact port depends on the application inside your container.
For example:
1
2
3
4
5
6
7
8
9
Django
↓
Docker
↓
EC2
↓
Security Group
↓
InternetThis is where Docker becomes particularly useful for developers.
13. EC2 + Docker vs a production container platform
Running Docker directly on EC2 is a great way to learn the fundamentals.
But in production, you may not want to manually manage everything yourself.
AWS provides container services such as:
- Amazon ECS
- Amazon EKS
- Amazon ECR
A common progression is:
1
2
3
4
5
6
7
8
9
Docker
↓
Docker + EC2
↓
Amazon ECR
↓
Amazon ECS
↓
Amazon EKSDocker
Build and package the application.
EC2
Provides the virtual server where the container can run.
ECR
Stores container images.
ECS
Manages containers without requiring you to manually manage the underlying container orchestration yourself.
EKS
Provides managed Kubernetes.
This progression is useful because you first understand containers, then learn how cloud platforms manage them at scale.
14. Don't forget security and cost
When experimenting with AWS, two things should always be on your checklist:
🔐 Security
Don't expose unnecessary ports.
For example, SSH on port 22 should generally be restricted to your own IP rather than opened to everyone. AWS recommends restricting SSH sources in production. AWS Documentation
Also, never put AWS access keys or other secrets inside your Dockerfile or application source code.
💰 Cost
An EC2 instance is a cloud resource, so don't simply leave resources running after you're finished experimenting.
When you're done:
1
2
3
4
5
Check resources
↓
Stop or terminate what you don't need
↓
Check your AWS billing/usageAlso remember that stopping an instance does not necessarily eliminate every possible charge associated with attached resources.
15. What I learned from this exercise
The biggest lesson is that Docker and cloud computing solve different parts of the deployment problem.
Docker helps package and run the application consistently.
AWS provides the infrastructure and services needed to make that application available in the cloud.
Together:
1
2
3
4
5
6
7
8
9
Docker
↓
Application Packaging
↓
EC2
↓
Cloud Infrastructure
↓
InternetOnce this basic workflow makes sense, moving toward ECR, ECS, CI/CD, and Kubernetes becomes much easier.
Conclusion
Deploying a Docker container on EC2 is a simple but valuable first step into cloud and DevOps.
You learned how to:
✅ Launch an EC2 instance
✅ Connect to a Linux server
✅ Install Docker
✅ Run a container
✅ Configure a security group
✅ Map container ports
✅ Access the application through the internet
✅ Understand the basic Docker → AWS deployment flow
✅ Connect to a Linux server
✅ Install Docker
✅ Run a container
✅ Configure a security group
✅ Map container ports
✅ Access the application through the internet
✅ Understand the basic Docker → AWS deployment flow
The next step could be replacing the manually downloaded Docker image with your own application image and then pushing that image to Amazon ECR.
From there, you can explore Amazon ECS and eventually Kubernetes/EKS.
💬 Question for the community
If you're learning Docker and AWS, what would you like to learn next: Amazon ECR, ECS, or Kubernetes/EKS?
I'd be interested to hear what other beginners are currently learning.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article