Deploying Docker Compose Applications to AWS EC2: Step-by-Step Guide

You’ve built and tested your Docker Compose app locally β€” now it’s time to go live!

In this guide, I’ll walk you through deploying a multi-container application using Docker Compose on an AWS EC2 instance. This setup is perfect for staging, testing, or lightweight production environments.


🌐 Why Deploy to EC2?

βœ… Full control over environment
βœ… Cost-effective and scalable
βœ… Great for Dockerized apps and DevOps testing
βœ… Useful for MVPs, personal projects, and client demos


🧰 Prerequisites

  • AWS account
  • Docker + Docker Compose installed locally
  • A working docker-compose.yml file
  • A Linux EC2 instance (Ubuntu recommended)
  • SSH access to EC2 (.pem key)

βœ… Step 1: Launch EC2 Instance

  1. Go to the AWS Console β†’ EC2 β†’ Launch Instance
  2. Choose Ubuntu Server 22.04
  3. Select t2.micro (Free Tier eligible)
  4. Configure your Key Pair and download the .pem file
  5. Under Security Group, allow ports 22 (SSH), 80, and 443

βœ… Step 2: SSH into the EC2 Instance

chmod 400 your-key.pem
ssh -i "your-key.pem" ubuntu@your-ec2-public-ip

βœ… Step 3: Install Docker and Docker Compose

sudo apt update
sudo apt install -y docker.io docker-compose
sudo usermod -aG docker ubuntu

Reboot or log out and log back in to apply group permissions.


βœ… Step 4: Upload Your App Files

Option 1: Upload via SCP

scp -i your-key.pem -r /local/project/path ubuntu@your-ec2-ip:/home/ubuntu/app

Option 2: Clone from GitHub

git clone https://github.com/yourusername/your-repo.git

🧾 Sample Docker Compose File

If you don’t have a Compose setup yet, here’s a working example of a Flask + PostgreSQL stack.

πŸ“ Directory structure:

app/
β”œβ”€β”€ docker-compose.yml
└── web/
β”œβ”€β”€ app.py
β”œβ”€β”€ requirements.txt
└── Dockerfile

πŸ“„ docker-compose.yml

version: '3.8'

services:
web:
build: ./web
container_name: flask_app
ports:
- "5000:5000"
environment:
- DATABASE_URL=postgres://postgres:postgres@db:5432/mydb
depends_on:
- db

db:
image: postgres:14
container_name: postgres_db
restart: always
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: mydb
volumes:
- pgdata:/var/lib/postgresql/data

volumes:
pgdata:

πŸ“„ web/Dockerfile

FROM python:3.10-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

πŸ“„ web/requirements.txt

Flask
psycopg2-binary

βœ… Step 5: Run Docker Compose on EC2

Navigate to your project directory:

cd app/
docker compose up -d

Check running containers:

docker compose ps

If all goes well, your app should be live at:

http://<your-ec2-public-ip>:5000

Make sure port 5000 is allowed in your EC2 security group.


πŸ§ͺ Verify the Deployment

Open a browser and enter your EC2 instance’s public IP with the exposed port (e.g., :5000). You should see your app running.

You can also view logs:

docker compose logs -f

πŸ›‘οΈ Bonus: Add a Domain and SSL (Next Blog Teaser)

Want to serve your app on a domain with HTTPS?
In the next blog, I’ll cover:

  • Mapping a domain to EC2
  • Installing Let’s Encrypt SSL with Nginx
  • Reverse proxying to your container app

βœ… Conclusion

Deploying Docker Compose apps on EC2 gives you a fast, flexible way to go live. It’s an essential skill for any DevOps engineer building or testing full-stack containerized applications.


πŸ”— Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *