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
- Go to the AWS Console β EC2 β Launch Instance
- Choose Ubuntu Server 22.04
- Select t2.micro (Free Tier eligible)
- Configure your Key Pair and download the
.pem
file - Under Security Group, allow ports
22
(SSH),80
, and443
β 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.