Ever tried installing Docker on an EC2 instance and hit a weird error you couldnāt make sense of?
Yeah... same here.
When I first started learning DevOps and AWS together, I assumed "just run it on EC2" would be simple. Spoiler: itās not always straightforward, especially if youāre new to Docker, permissions, or cloud instances.
In this post, Iāll walk you through how to get Docker + Docker Compose up and running on an AWS EC2 instance ā
step-by-step, with tips, fixes, and analogies to make it stick.
š§ Why This Matters (Especially if Youāre into DevOps or AWS)
Before jumping into ECS, EKS, or fancy CI/CD pipelines, it helps to understand how Docker runs on a basic EC2 instance. It's like learning to ride a bike before buying a Tesla.
š Step-by-Step: Docker on EC2 (Amazon Linux 2)
1. Launch Your EC2 Instance
Choose Amazon Linux 2 AMI
Instance type: t2.micro (free-tier friendly)
Open port 22 (SSH), and port 80 if you're going to run a web server
Generate/download your .pem key file
SSH into it:
chmod 400 your-key.pem
ssh ec2-user@<your-ec2-ip> -i your-key.pem
2. Install Docker & Docker Compose
sudo yum update -y
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -aG docker ec2-user
Now either log out and back in, or run:
newgrp docker
Then install Docker Compose:
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
3. Run a Test Container
docker run hello-world
If all goes well, Docker will show a welcome message. š
But... if you see something like this:
failed to create shim task: OCI runtime create failed...
Donāt panic! This usually means Docker isnāt running, or you need privileges.
ā
Fix it by restarting Docker:
sudo service docker restart
Or try running with --privileged:
docker run --privileged hello-world
š§ Analogy Time: Docker + EC2 Like a Coffee Shop
Hereās how I like to visualize it:
EC2 instance = your coffee shop (in the cloud)
Docker = your coffee machine
Containers = different drinks you serve
Docker Compose = your pre-set menu board
Once you install Docker on EC2, youāre basically setting up a cloud-based kitchen ready to serve anything ā be it NGINX, a Python app, or even a PostgreSQL database.
š Bonus: Your First Docker Compose File
Create a file called docker-compose.yml:
version: '3'
services:
web:
image: nginx
ports:
- "80:80"
Start it:
docker-compose up -d
Now open your EC2ās public IP in the browser ā hello, NGINX homepage!
Top comments (2)
How do you make it so interesting that it felt like you are talking about a movie scene š¬. I love your narrative ā¤ļø. Great work! Looking forward to more!
Very helpful blogpost!