## 📝 Author
Birat Aryal — birataryal.github.io
Created Date: 2025-06-18
Updated Date: Wednesday 18th June 2025 10:54:39
Website - birataryal.com.np
Repository - Birat Aryal
LinkedIn - Birat Aryal
DevSecOps Engineer | System Engineer | Cyber Security Analyst | Network Engineer
📦 Docker Compose Examples and Use Cases
Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to configure services, networks, and volumes in a single docker-compose.yml file.
🧰 Basic Usage
Sample docker-compose.yml
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
app:
build: ./app
volumes:
- ./app:/usr/src/app
environment:
- NODE_ENV=development
depends_on:
- db
db:
image: postgres:13
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydb
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
Commands
docker-compose up # Start all services
docker-compose down # Stop and remove containers, networks, volumes
docker-compose build # Build all services defined in the file
docker-compose ps # List running services
docker-compose logs -f # View logs
💡 Use Cases
1. Development Environments
-
Run backend, frontend, and database containers together.
-
Hot reload changes using volumes.
2. CI/CD Testing Pipelines
-
Define disposable environments for integration tests.
-
Reproducible setups using same Compose config in test pipelines.
3. Service Dependencies
-
Automatically manage dependencies (e.g.,
depends_on). -
Use health checks to ensure readiness.
4. Local Replication of Production
- Simulate production stack locally using the same Compose setup.
5. Database Migrations
- Add migration containers with custom entrypoints to perform migrations at startup.
6. Multi-network Applications
- Define multiple networks to isolate internal services from public-facing ones.
🛠 Advanced Features
Profiles (v3.9+)
services:
debug:
image: alpine
command: sleep infinity
profiles: ["debug"]
docker-compose --profile debug up
Override Configuration
docker-compose -f docker-compose.yml -f docker-compose.override.yml up
Resource Limits
services:
app:
deploy:
resources:
limits:
cpus: "0.5"
memory: "512M"
🌐 Real-World Compose Template: Full Stack Example
version: '3.8'
services:
frontend:
build: ./frontend
ports:
- "3000:3000"
depends_on:
- backend
backend:
build: ./backend
ports:
- "5000:5000"
environment:
- DATABASE_URL=postgres://user:pass@db:5432/mydb
depends_on:
- db
db:
image: postgres:14
restart: always
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydb
volumes:
- db_data:/var/lib/postgresql/data
volumes:
db_data:
🚀 Production Deployment Strategies
1. Use .env File for Configuration
POSTGRES_USER=user
POSTGRES_PASSWORD=securepassword
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
Run with:
docker-compose --env-file .env up
2. Secrets Management
Use Docker Swarm or external secret managers for secure handling.
secrets:
db_password:
file: ./secrets/db_password.txt
services:
db:
secrets:
- db_password
3. Restart Policies
Ensure services restart on failure or reboot:
restart: unless-stopped
Options:
-
no -
always -
on-failure -
unless-stopped
4. Health Checks
Ensure containers are ready before serving traffic:
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:80"]
interval: 30s
timeout: 10s
retries: 3
5. Separate Production Overrides
Use a separate override file for prod-specific config:
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
6. Use Volumes for Persistent Data
Avoid using bind mounts for databases or production data.
volumes:
db_data:
driver: local