Compose in Production
Compose v2 can run small production workloads on a single VM — perfect for side projects, internal tools, and MVPs that don't need Kubernetes complexity.
Introduction
Compose v2 can run small production workloads on a single VM — perfect for side projects, internal tools, and MVPs that don't need Kubernetes complexity.
Purpose of this lesson
This lesson teaches Compose in Production as an engineering decision: what problem it solves, when to use it, how to implement it safely, and what signals tell you it is failing.
Understanding the topic
Use this when a project has more than one runtime dependency and the team needs a repeatable local, CI, or small-production environment. Compose is most valuable when it replaces tribal setup notes with executable infrastructure.
Core concepts to understand:
- Use
restart: unless-stoppedfor resilience. - Front everything with a reverse proxy (traefik, caddy) for TLS.
- Back up named volumes on a schedule.
- Monitor with Watchtower (auto-image-update) + uptime checks.
Visual explanation
Architecture or command flow to keep in mind:
# compose.prod.yml (overlay file)services:api:image: ghcr.io/acme/api:${VERSION}restart: unless-stoppeddeploy:resources:limits: { cpus: '1.0', memory: 512M }# usage: docker compose -f compose.yml -f compose.prod.yml up -d
Step-by-step explanation
- Model each service with its own image, ports, environment, healthcheck, networks, and volumes.
- Keep internal service-to-service traffic on the Compose network and publish only the edge service to the host.
- Use named volumes for state, bind mounts for local source code, and profiles for optional developer tools.
- Bring the stack up, read health status and logs, then restart one dependency to see whether the app recovers.
- Create a production override file with pinned images, restart policies, resource limits, logging, backup, and rollback guidance.
Informative example
Use the example below as a working baseline, then verify the runtime behavior instead of assuming the command or file is correct.
# compose.prod.yml (overlay file)services:api:image: ghcr.io/acme/api:${VERSION}restart: unless-stoppeddeploy:resources:limits: { cpus: '1.0', memory: 512M }# usage: docker compose -f compose.yml -f compose.prod.yml up -d
A production-minded check usually includes docker ps, docker logs, docker inspect, and one validation from outside the container such as curl, a database connection, or a registry pull.
# Verification loop for Compose in Productiondocker ps -adocker logs --tail=100 <container-name>docker inspect <container-or-image-name>docker system df
Real-world use
Plenty of profitable SaaS products run on a single $40/mo VM with Compose + Caddy + Postgres. Don't reach for K8s prematurely.
Enterprise use cases
In a mature engineering organization, Compose in Production is documented as a repeatable pattern with approved base images, ownership labels, CI checks, security expectations, rollback notes, and troubleshooting commands. The difference between a tutorial and production practice is that every container decision must be observable, reviewable, and reversible.
Best practices
- Pin image versions in prod overlays.
- Take backups before any
docker compose down. - Use overlay compose files to layer prod-only settings.
Common mistakes
docker compose down -vdeletes named volumes — data loss.
Debugging tips
- Read logs before restarting; a restart often removes the timing context you need for root cause analysis.
- Use
docker inspectto compare configured state with actual runtime state. - Check daemon health, disk usage, image tags, exit code, and port mappings before blaming application code.
Optimization strategies
- Use profiles to keep optional developer tools out of the default startup path.
- Give long-running services healthchecks and restart policies so failures are visible and recoverable.
- Separate dev and prod overrides instead of stretching one Compose file with fragile conditionals.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1QuestionCan you run Compose in production?+
Answer
2QuestionHow do you layer dev/prod overrides?+
Answer
3QuestionHow do resource limits work in Compose?+
Answer
Hands-on exercise
Create a small lab for Compose in Production: run the example, inspect the created Docker object, intentionally introduce one mistake, and record the command that reveals the failure. The goal is not just to make the happy path work; it is to build operational reflexes.
# Hands-on lab scaffoldmkdir -p docker-compose-in-production-labcd docker-compose-in-production-lab# Add the Dockerfile, compose.yml, or command from this lesson.# Then run one happy-path test and one broken-path test.docker versiondocker infodocker system df
Summary
Compose in Production matters because Docker is not only a packaging tool; it is a runtime, build, networking, storage, and delivery workflow. Treat each lesson as a production habit: make it repeatable, inspectable, secure, and easy to debug.