Compose for Production
Health checks, restart policies, secrets, resource limits, and production-ready patterns
Skills taught: docker.healthcheck, docker.secrets, docker.resource-limits, docker.compose-prod
Health checks — knowing your services are actually ready
services:
api:
image: myapi
healthcheck:
# Command to run inside the container
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
# How often to check
interval: 30s
# How long to wait for a response
timeout: 10s
# How many failures before marking unhealthy
retries: 3
# Grace period before first check (startup time)
start_period: 40s
postgres:
image: postgres:16-alpine
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 5
redis:
image: redis:7-alpine
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 3s
retries: 3
Secrets management in Compose
# Production approach: use Docker secrets or env_file with .env
# Option 1: env_file (for simple setups)
services:
api:
image: myapi
env_file:
- .env.production # file excluded from git
# Option 2: Docker secrets (Swarm/compose secrets)
secrets:
db_password:
file: ./secrets/db_password.txt # or 'external: true' for Swarm
jwt_secret:
file: ./secrets/jwt_secret.txt
services:
api:
image: myapi
secrets:
- db_password
- jwt_secret
# Secrets mounted at /run/secrets/<name> — read them in your app
db:
image: postgres:16-alpine
secrets:
- db_password
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
Resource limits and profiles
services:
api:
image: myapi
deploy:
resources:
limits:
cpus: '1.0' # max 1 CPU core
memory: 512M # max 512 MB RAM
reservations:
cpus: '0.25' # guaranteed minimum
memory: 128M
# Profiles — only start certain services in specific scenarios
# docker compose --profile debug up
debug-ui:
image: adminer
ports:
- "8080:8080"
profiles:
- debug
# docker compose --profile monitoring up
prometheus:
image: prom/prometheus
profiles:
- monitoring
A production-ready Compose file
services:
api:
image: ghcr.io/myorg/api:${IMAGE_TAG:-latest}
restart: unless-stopped
env_file: .env.production
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 60s
depends_on:
postgres:
condition: service_healthy
deploy:
resources:
limits:
memory: 512M
cpus: '1.0'
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3" # rotate: keep only 3 × 10 MB log files
postgres:
image: postgres:16-alpine
restart: unless-stopped
env_file: .env.production
volumes:
- postgres-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $POSTGRES_USER"]
interval: 10s
timeout: 5s
retries: 5
deploy:
resources:
limits:
memory: 1G
volumes:
postgres-data:
external: true # must be created before compose up
Frequently Asked Questions
Should I use Docker Compose in production?
Yes, for single-server deployments — a small API, a personal project, or a startup's MVP. It's simple, well-understood, and reliable. For multi-server orchestration, automatic scaling, rolling deployments, or high availability, consider Kubernetes or managed container services (ECS, Cloud Run). Most projects run fine on a single server with Compose for years.