Docker Compose Production Deployment Guide
Containerization is standard for modern delivery. Docker Compose simplifies multi-service orchestration but production demands extra safeguards.
Service Configuration and Resource Limits
Set reasonable CPU/memory limits per service to prevent cascading failures. Pair with healthchecks to auto-restart unhealthy containers.
version: "3.9"
services:
web:
build: { context: ., dockerfile: Dockerfile, target: production }
image: registry.example.com/app:${TAG:-latest}
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost/health"]
interval: 30s
timeout: 5s
retries: 3
start_period: 40s
deploy:
resources:
limits: { cpus: "2.0", memory: 2G }
reservations: { cpus: "0.5", memory: 512M }
logging:
driver: json-file
options: { max-size: "10m", max-file: "5" }
networks: [frontend, backend]
secrets: [db_password]
depends_on:
db: { condition: service_healthy }
db:
image: postgres:16-alpine
volumes: [pgdata:/var/lib/postgresql/data]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app"]
interval: 10s
timeout: 5s
retries: 5
volumes: { pgdata: null }
networks: { frontend: null, backend: { internal: true } }
secrets: { db_password: { file: ./secrets/db_password.txt } }
Network and Security Isolation
Place internal services on internal networks without exposed ports. Mount credentials via secrets instead of hardcoding in images.
| Config Item | Development | Production | Recommended |
|---|---|---|---|
| restart | no | unless-stopped | Enable in prod |
| healthcheck | Optional | Required | 30s/3 retries |
| resource limits | None | Required | 2C CPU/2G RAM |
| logging driver | default | json-file | max-size 10m |
| network mode | bridge | split networks | internal for DB |
Best Practices
Use multi-stage builds to shrink image size, combine with .dockerignore, and regularly prune dangling images and volumes.