Docker Compose Production Deployment: Health Checks and Resource Limits
Docker Compose is often treated as a local development tool, but it is officially supported for production: on a single server or a handful of hosts, Compose can run a maintainable application stack. The key is separating "development configuration" from "production configuration" and adding the production essentials: health checks, resource limits and restart policies.
1. Why Compose Can Be Used in Production
Compose is a single-host multi-container orchestrator. For projects with thousands to tens of thousands of daily active users that do not need horizontal scaling across many nodes, it is far lighter than Kubernetes: no control plane, no steep learning curve, and a single docker compose up -d to go live. When you need cluster-level scheduling, upgrade to Kubernetes instead.
2. Key Changes for Production Configuration
Per the official guidance, do not reuse compose.yaml as-is in production. Put the differences in a separate compose.production.yaml and layer them with -f:
docker compose -f compose.yaml -f compose.production.yaml up -d
Typical production changes include:
- Remove application-code bind mounts: in production the code should stay inside the image and never be editable from the host.
- Change port bindings: do not expose databases or Redis to the public internet.
- Reduce log verbosity: set environment variables to suppress debug output.
- Set a restart policy: for example
restart: always, so a crash does not leave the service down. - Add log aggregation: for example JSON logs plus a collector.
A concrete split: in the development compose.yaml, the web service mounts source via bind mount and exposes port 3000 locally; compose.production.yaml carries only the overrides:
# compose.production.yaml
services:
web:
image: myapp:1.0.0 # override image instead of build
ports:
- "127.0.0.1:8080:8080" # bind to localhost only; put Nginx in front
environment:
- NODE_ENV=production
restart: always
logging:
driver: json-file
options: { max-size: "10m", max-file: "3" }
On the server, docker compose -f compose.yaml -f compose.production.yaml up -d starts everything with production settings, while dev machines keep using the original file untouched.
| Setting | Production value | What it does | Risk when omitted |
|---|---|---|---|
restart |
always |
Brings the process back after it exits | A stopped container stays down |
logging.options |
max-size: "10m", max-file: "3" |
Caps one log file and the number of rotations | Logs fill the disk |
healthcheck.interval |
10s (production example), 20s (verification step) |
Probe frequency | Probes are too sparse and failures surface late |
healthcheck.timeout |
5s (production example), 3s (verification step) |
Per-probe timeout | A slow response counts as healthy |
healthcheck.retries |
5 (production example), 3 (verification step) |
Failures before the container is unhealthy | One hiccup triggers a restart |
healthcheck.start_period |
30s (production example), 40s (verification step) |
Grace window at startup | A slow starter restarts in a loop |
deploy.resources.limits |
cpus: "1.0", memory: 512M |
Ceiling set at 1.5 times the docker stats peak |
One container eats the whole machine |
deploy.resources.reservations |
cpus: "0.25", memory: 128M |
Reservation set at 80% of baseline | Resources get taken by other containers |
pids_limit |
512 |
Caps the process count in the container | A fork bomb takes the host down |
3. Health Checks and Startup Order
The short syntax of depends_on only guarantees that a dependency starts first, not that it is ready. In production use the long syntax with healthcheck so the web service truly waits until the database is available:
services:
web:
image: myapp:1.0.0
depends_on:
db:
condition: service_healthy
restart: always
db:
image: postgres:16
healthcheck:
test: ["CMD", "pg_isready", "-U", "appuser"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
start_period is critical: it gives a container time to warm up, preventing a slow-starting service from being marked unhealthy and restarted repeatedly.
4. Resource Limits
An unbounded container can exhaust the memory of the whole machine. In production, declare CPU and memory limits for every service:
services:
web:
image: myapp:1.0.0
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
reservations:
cpus: "0.25"
memory: 128M
pids_limit: 512
read_only: true
tmpfs:
- /tmp
The read_only plus tmpfs combination makes the filesystem read-only, sharply reducing the ability to write malicious files after a compromise; pids_limit also helps defend against fork-bomb style attacks.
How do you size the limits? There is no universal answer, but a reasonable starting point: run first and watch docker stats for the peaks, then set limits at ~1.5x the peak and reservations at ~80% of the baseline. When a 4GB box has to host a database, an app and Redis at once, remember to count MySQL's innodb_buffer_pool_size and Redis's maxmemory into the total, or container limits and process memory end up fighting each other.
5. The Right Way to Roll Out Updates
docker compose build web
docker compose up --no-deps -d web
Build first, then up --no-deps to avoid recreating dependent services. After the update, verify health:
docker compose ps
docker compose logs --tail=100 web
The point of up --no-deps is rebuilding only the web service. To get closer to zero downtime, boot a staging instance from the new image tag and verify before switching, or rehearse a --rollback. Do not sneak database schema changes into docker compose up: run migrations as a separate step with a backup in place, then update the application code.
6. Security and Secrets
Never hard-code passwords in a Compose file in production. Prefer a .env file with environment-variable injection, and use Docker Secrets for more sensitive credentials:
services:
web:
secrets:
- db_password
secrets:
db_password:
file: ./secrets/db_password.txt
For more container security practices see container security best practices, and for configuration and secret management see environment variables and configuration management.
7. Monitoring and Operations
After go-live, wire up monitoring: use monitoring and alerting to track restart counts and resource usage, and follow the backup and restore runbook for data protection. For fresh hosts, see the server initialization script.
Common Pitfalls
- Forget to back up named volumes: containers can be recreated, volumes cannot. Back one up with
docker run --rm -v myvolume:/data -v $PWD:/backup alpine tar czf /backup/vol.tgz -C /data .. - Commit .env to the repo:
.envusually holds passwords; add it to.gitignoreand route secrets through Docker Secrets or a dedicated secret manager. - Expose every port: binding database or Redis ports to
0.0.0.0hands internal services to the public internet — a prime target for scanners. - Ignore log rotation: without
max-size, log files grow until they fill the disk; one of the most common production incidents.
16IDC Note
Productionizing Compose is really about turning development convenience into operational discipline: health checks make deployments verifiable, resource limits let one machine safely host many applications, and restart policies let failures self-heal. For small and mid-sized projects this is the crucial step from "it runs" to "it runs reliably", and a natural stepping stone before migrating to Kubernetes later.
Reference: Docker Compose production guide https://docs.docker.com/compose/how-tos/production/ ; Compose file reference https://docs.docker.com/reference/compose-file/services/ ; Backing up volumes https://docs.docker.com/storage/volumes/#back-up-restore-or-migrate-data-volumes
Source: https://docs.docker.com/compose/how-tos/production/ , https://docs.docker.com/reference/compose-file/services/
Operation & Verification Steps
Example 1: resource limits, health checks, and log rotation for production
services:
app:
image: registry.example.com/app:${APP_TAG}
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-fsS", "http://127.0.0.1:8080/healthz"]
interval: 20s
timeout: 3s
retries: 3
start_period: 40s
deploy:
resources:
limits:
cpus: "1.5"
memory: 512M
reservations:
memory: 256M
logging:
driver: json-file
options:
max-size: "20m"
max-file: "5"
Note: limits stop one container from consuming the host, reservations give capacity planning something to stand on, and without rotation it is usually logs — not business data — that fill the disk.
Example 2: multi-stage builds, keeping build dependencies out of the image
# syntax=docker/dockerfile:1
FROM node:22-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:22-alpine AS runtime
ENV NODE_ENV=production
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
USER node
EXPOSE 8080
CMD ["node", "dist/server.js"]
Note: the runtime image carries only artifacts and production dependencies, shrinking both size and attack surface, while USER node stops the process from running as root.
Limitations
- Per the official guidance, do not reuse
compose.yamlas-is in production. - This article reflects public information as of its publication date; terms, prices and quotas follow the provider's current published terms.
- Third-party capability and limit statements follow their official documentation; no further commitment is implied here.
- Licensing, filing and data-residency decisions must be verified against the regulations of your operating region.
FAQ
What prerequisites does a production Compose deploy need?
You need the two files split: overrides live in compose.production.yaml and are stacked with -f. Install Compose v2, confirm volumes and secret files exist, then add health checks, resource limits and a restart policy; the official guidance says not to reuse the dev file as-is.
Why does a container exit right after start?
Typically it is dependency readiness: the short depends_on form only orders startup, so give the database a healthcheck and require service_healthy, with start_period: 30s and retries: 5 for warm-up. Then read compose ps and the last 100 log lines, and check that the healthcheck command itself runs inside the container.
How should secrets reach a Compose stack?
Through files, never inline: ordinary variables come from .env kept in .gitignore, and sensitive values mount via Docker Secrets so neither the repository nor the image holds plaintext. On rotation add the new secret before switching traffic and retire the old one 1 release later, so no request hits a missing credential.
Do production overrides break existing setups?
No, as long as overrides live in compose.production.yaml and the dev machine keeps the original file. Three things change: image overrides build, the source bind mount disappears, and restart plus log rotation get added. Render with docker compose config first and confirm ports still bind to 127.0.0.1 before shipping.
Related Providers
- Docker: when healthcheck, deploy.resources and restart policy must be aligned item by item in Compose.
- Kubernetes: when multi-replica rollout outgrows single-host Compose and the migration target needs comparing.
- GitHub Actions: when image builds and Compose deploys must pass a pipeline with health checks as the gate.
Community & Resources
-
Environment Deployment: other coverage in this category.
Official docs and communities -
Compose in production — Official positioning and caveats for production use.
-
Compose services reference — Field definitions for
deploy.resources,healthcheck, andrestart. -
Multi-stage builds — The standard way to shrink images.
-
Configure logging drivers — Official reference for
max-sizeandmax-file.
Learning resources and tools
- Stack Overflow: docker — Practical questions on limits and runtime behaviour.
- Stack Overflow: docker-compose — Q&A on production orchestration and rollout.
- Stack Overflow: dockerfile — Multi-stage builds and layer cache optimisation.
Original source: Docker Docs ↗