Docker Compose
Run multi-container stacks locally or on a single host with automatic service DNS, health-gated depends_on, isolated networks, env files, secrets, and profiles.
TL;DR
Docker Compose wires multi-container apps together on one host using a single compose.yaml. Every service gets an entry in Compose's embedded DNS, explicit networks isolate traffic, depends_on with condition: service_healthy prevents race conditions at startup, and profiles keep optional tools out of your default stack.
Intent
Understand how Docker Compose resolves service names over DNS, gates startup on real healthchecks, and separates configuration (env_file) from credentials (secrets) without needing Kubernetes.
Problem
Running docker run by hand for a web server, database, and cache quickly breaks down: container IPs change on every restart, the web app crashes if it connects before Postgres finishes initializing, and passing passwords via -e exposes them in docker inspect.
Solution
Compose reads a declarative compose.yaml and sets up three primitives automatically:
- Service DNS & networks: Compose creates a project-scoped bridge network. Every container's
/etc/resolv.confpoints to Docker's embedded DNS server (127.0.0.11), which resolves service names (likedb) to the active container IP. You can also splitfrontendandbackendnetworks withinternal: trueso the database has no outbound internet route. - Startup order with healthchecks: Plain
depends_on: [db]only waits for the container to start, not for the process inside to accept connections. Pairingdepends_onwithcondition: service_healthyand ahealthcheckensureswebstarts only afterdbpasses its readiness command (e.g.,pg_isready). env_filevssecrets, plusprofiles: Useenv_filefor non-sensitive config (NODE_ENV,PORT),secretsto mount sensitive files at/run/secrets/<name>, andprofiles: ["debug"]so admin UIs or seeders only run when requested (docker compose --profile debug up).
Real-world analogy
▶ Flow: watch it work
Service-to-Service Request via Compose DNS
Press Start or tap any step dot to trace the path.
▸All steps (4)
Code
A minimal, production-minded compose.yaml using service_healthy, isolated networks, env_file, secrets, and profiles:
# compose.yaml (not run)
services:
web:
build: .
ports:
- "3000:3000"
env_file:
- .env.app
secrets:
- db_password
networks:
- frontend
- backend
depends_on:
db:
condition: service_healthy
db:
image: postgres:17-alpine
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
- db_password
networks:
- backend
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
timeout: 3s
retries: 5
volumes:
- pgdata:/var/lib/postgresql/data
adminer:
image: adminer:4
profiles: ["debug"]
ports:
- "8080:8080"
networks:
- backend
networks:
frontend:
backend:
internal: true
volumes:
pgdata:
secrets:
db_password:
file: ./secrets/db_password.txtWhen you DON'T need this
- Single-container apps or static sites: If your app is one stateless binary or static assets (like this site),
docker runor a static host is simpler than maintaining acompose.yaml. - Multi-node scheduling or auto-scaling: Compose targets a single Docker Engine host. Once you need rolling updates across multiple machines, bin-packing, or self-healing across node failures, move to Kubernetes or a managed container platform.
- Cost / complexity trade-off: Compose is free and ships with Docker Desktop / Docker Engine, so its operational overhead is near zero—but single-host simplicity means zero built-in high availability if that host goes down.
When to use / when to avoid
- Local development environments where an app needs a database, cache, or queue with one command.
- Single-VM deployments where simplicity matters more than multi-node orchestration.
- Production workloads requiring zero-downtime multi-node failover or horizontal autoscaling.
- Sharing host secrets via plain .env files committed to Git.
How to implement
Define isolated networks
Put web on frontend and backend, and put db only on backend with internal: true.
Add a real healthcheck on dependencies
Gate web with depends_on.db.condition: service_healthy so startup waits for readiness.
Separate env_file from secrets
Keep non-sensitive flags in .env.app and mount credentials via secrets: at /run/secrets/.
Pros / Cons
- Zero cluster overhead: one YAML file and docker compose up -d.
- Automatic service discovery via embedded DNS without hardcoding IPs.
- Profiles keep heavy debug or migration containers out of normal runs.
- Single-host only: no built-in multi-node scheduling or automatic node failover.
- Compose secrets on standalone Engine are plain bind-mounted files, not encrypted-at-rest Swarm/KMS secrets.
Relations
Check yourself
Check Yourself
1. Why does depends_on: [db] without a condition still let your web app fail on startup?
2. Where does Docker Compose mount a secret named db_password inside a container by default?
Sources
Dockerfile Best Practices
Write small, deterministic, and secure container images using pinned tags, non-root users, .dockerignore, layer ordering, healthchecks, and BuildKit secrets.
Volumes vs Bind Mounts vs tmpfs
Choose the right Docker storage mount—named volumes, bind mounts, or tmpfs—avoid UID permission mismatches, and back up persistent volumes safely.