DEV Community

Anusha Kuppili
Anusha Kuppili

Posted on

🐳 Docker on AWS EC2 — The Real Beginner’s Guide (What I Wish I Knew)

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Now either log out and back in, or run:

newgrp docker
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

3. Run a Test Container

docker run hello-world
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Or try running with --privileged:

docker run --privileged hello-world
Enter fullscreen mode Exit fullscreen mode

🧠 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"
Enter fullscreen mode Exit fullscreen mode

Start it:

docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

Now open your EC2’s public IP in the browser — hello, NGINX homepage!

Top comments (2)

Collapse
 
sai_nagasumalyapatnala_ profile image
Sai Naga Sumalya Patnala

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!

Collapse
 
sai_nagasumalyapatnala_ profile image
Sai Naga Sumalya Patnala

Very helpful blogpost!