Service Configuration
Configure services for production-ready deployments.
Health Checks
services:
api:
image: myapp:latest
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
db:
image: postgres:16
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5Restart Policies
services:
web:
restart: always # Always restart
worker:
restart: unless-stopped # Unless manually stopped
cron:
restart: on-failure # Only on failure
restart: "on-failure:5" # Max 5 retries
test:
restart: "no" # Never restart (default)Resource Limits
services:
api:
deploy:
resources:
limits:
cpus: '0.50'
memory: 512M
reservations:
cpus: '0.25'
memory: 256M
# Alternative syntax (older)
mem_limit: 512m
cpus: 0.5Logging Configuration
services:
api:
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"
# Other drivers
syslog:
logging:
driver: syslog
options:
syslog-address: "tcp://192.168.1.1:514"Command and Entrypoint
services:
api:
image: node:20
command: npm run start
# Override entrypoint
debug:
image: myapp
entrypoint: /bin/sh
command: -c "echo debugging && exec node app.js"
# Multiple commands
worker:
command: ["sh", "-c", "npm run migrate && npm run worker"]User and Permissions
services:
api:
user: "1000:1000"
# Or use user name
app:
user: nodeLabels and Metadata
services:
api:
labels:
- "com.example.description=API Service"
- "com.example.environment=production"
# Traefik labels
- "traefik.enable=true"
- "traefik.http.routers.api.rule=Host(`api.example.com`)"Working Directory
services:
api:
working_dir: /app/srcComplete Production Service
services:
api:
image: myapi:1.0.0
container_name: production-api
hostname: api
restart: unless-stopped
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- DATABASE_URL=postgres://user:pass@db:5432/app
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
deploy:
resources:
limits:
cpus: '1.0'
memory: 1G
reservations:
cpus: '0.5'
memory: 512M
logging:
driver: json-file
options:
max-size: "50m"
max-file: "5"
depends_on:
db:
condition: service_healthy
networks:
- backend
- frontend
networks:
backend:
frontend:Profiles
services:
api:
# Always started
debug:
profiles: ["debug"]
# Only started with: docker compose --profile debug up
test:
profiles: ["test"] - docker compose
- services
- healthcheck
- restart
- deploy