A development compose.yaml is optimized for iteration: live code mounts, exposed ports for debugging, no resource limits, latest-tag images. Carrying that file to production gives you a stack that races on startup, fills the host’s disk with logs, dies on the first daemon restart, and exposes services to interfaces that should never see them. This article covers the deltas — what to add, what to remove, what to lock down before a Compose stack runs on a real host.
How to verify
Audit a deployed stack against production expectations:
docker compose ps -a
docker compose config | grep -E "restart:|healthcheck:|deploy:|limits:|secrets:|read_only:"
docker compose logs --tail=20 | head -50
docker inspect $(docker compose ps -q) --format '{{.Name}} {{.HostConfig.RestartPolicy.Name}} {{.HostConfig.Memory}} {{.HostConfig.ReadonlyRootfs}}'
ls -la /etc/docker/daemon.json
If restart is missing on any service, the stack won’t recover from a daemon restart. If Memory is 0, the container has no memory limit. If ReadonlyRootfs is false, the rootfs is writable (rarely necessary).
What’s happening
The dev workflow assumes you’re watching: a crash, a port collision, a log file growing — you see it and fix it. Production has no such observer. Every default that requires human attention in dev needs to become an automatic behavior in prod. Restart policies, health checks, resource caps, log rotation, persistent secrets — each is the production replacement for “I’ll just notice.”
The Compose schema accommodates this with fields that are inert on compose up but consumed by Swarm or by Compose itself (restart:, healthcheck:, deploy.resources:). On a single-host Compose deployment, the deploy: keys are mostly ignored — use the top-level equivalents (mem_limit:, cpus:, etc.) for non-Swarm deployments.
The procedure
A production-shaped compose.prod.yaml for the same stack you saw in Compose basics:
name: myapp
services:
web:
image: ghcr.io/example/web:1.42.0
restart: unless-stopped
pull_policy: always
init: true
user: "1000:1000"
read_only: true
tmpfs:
- /tmp:size=64m
cap_drop:
- ALL
cap_add:
- NET_BIND_SERVICE
security_opt:
- no-new-privileges:true
mem_limit: 512m
mem_reservation: 256m
cpus: 1.0
pids_limit: 200
ports:
- "127.0.0.1:8080:8080"
networks:
- app-net
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/app
REDIS_URL: redis://cache:6379/0
secrets:
- db_password
depends_on:
db:
condition: service_healthy
cache:
condition: service_started
healthcheck:
test: ["CMD", "curl", "-fsS", "http://localhost:8080/healthz"]
interval: 30s
timeout: 5s
retries: 3
start_period: 30s
logging:
driver: json-file
options:
max-size: "10m"
max-file: "5"
tag: "{{.Name}}|{{.ImageName}}"
db:
image: postgres:16-alpine
restart: unless-stopped
user: "999:999" # postgres user inside the official image
mem_limit: 1g
cpus: 2.0
pids_limit: 500
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
POSTGRES_DB: app
secrets:
- db_password
volumes:
- pg-data:/var/lib/postgresql/data
networks:
- app-net
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d app"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
logging:
driver: json-file
options:
max-size: "20m"
max-file: "5"
cache:
image: redis:7-alpine
restart: unless-stopped
command:
- "redis-server"
- "--maxmemory"
- "256mb"
- "--maxmemory-policy"
- "allkeys-lru"
- "--appendonly"
- "yes"
mem_limit: 384m
volumes:
- redis-data:/data
networks:
- app-net
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 3s
retries: 5
networks:
app-net:
driver: bridge
ipam:
config:
- subnet: 172.30.0.0/24
volumes:
pg-data:
driver: local
redis-data:
driver: local
secrets:
db_password:
file: /etc/myapp/secrets/db_password
- Pinned image tags.
1.42.0notlatest. The deploy is reproducible because the tag is immutable.pull_policy: alwaysforces a fresh check against the registry on everyup. restart: unless-stoppedon every service. The stack comes back after host reboot.init: truerunstinias PID 1 so signals propagate correctly.- Non-root user. Every service runs as a specific UID. Combine with read-only rootfs (
read_only: true) and explicittmpfsfor paths that need to be writable. - Dropped capabilities, no-new-privileges. The container has only the kernel capabilities it actually needs.
- Resource limits.
mem_limit,cpus,pids_limitcap each service. Without these, one runaway container kills the host. - Loopback-only port binding. Public services live behind a reverse proxy (Nginx, Caddy, Traefik) on the host or in another container.
- Secrets via files.
POSTGRES_PASSWORD_FILEreads from/run/secrets/db_passwordwhich Compose mounts from the file declared at the top level. Secrets are never on the command line and never indocker inspect. See Docker secrets handling for the registry-aware approach. - Health checks with
start_period. Postgres takes 5-30 seconds to come up on first start;start_periodkeeps the orchestrator from marking the container unhealthy during init. - Log rotation per service. Even with daemon-wide defaults, set the values explicitly on services that need different limits.
Deploy and tear down:
# First deploy (or after image bump)
docker compose -f compose.yaml -f compose.prod.yaml pull
docker compose -f compose.yaml -f compose.prod.yaml up -d
# Rolling restart (single-host approximation)
docker compose -f compose.yaml -f compose.prod.yaml up -d --no-deps --force-recreate web
# Drain
docker compose -f compose.yaml -f compose.prod.yaml stop
# Followed by your backup, then 'down' if needed
For zero-downtime updates on a single host, Compose isn’t ideal — --force-recreate brings the container down and back up, which means a few seconds of unavailability. For true rolling updates, move to Swarm (docker stack deploy) or to Kubernetes.
Common pitfalls
- Leaving
version: "3.8"(or similar) at the top of the file — deprecated in Compose v2, harmless but a sign that the file hasn’t been maintained. - Using
restart: always(ignores manual stops) when you wantedunless-stopped. The first time youdocker stopsomething for maintenance, it comes back. - Setting
mem_limit: 512mwithout settingmem_reservation— Docker can over-commit the host. Both should be specified, with reservation as a soft minimum. - Mounting host paths read-write when read-only would do — a compromised container can rewrite host config (
/etc/nginx/, certs). - Forgetting
start_periodon slow-starting services (Postgres, Java apps) — the container gets marked unhealthy during init anddepends_onblocks dependents forever.
We ship every Compose-based client deployment with these production deltas baked in, then layer managed operations on top — image pulls, health watches, restart escalation, backup verification.