Docker Compose
Docker Compose describes a multi-container app in one YAML file (compose.yaml) and runs it with one command. It creates the networks and volumes and starts everything in the right order. It's the standard way to run apps locally.
A full-stack app in one file
Let's run a Node API with PostgreSQL and Redis:
# compose.yaml
services:
api:
build: . # build from ./Dockerfile
ports:
- "3000:3000"
environment:
DATABASE_URL: postgres://app:secret@db:5432/appdb
REDIS_URL: redis://cache:6379
depends_on:
db:
condition: service_healthy # wait until Postgres is ready
cache:
condition: service_started
restart: unless-stopped
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: secret
POSTGRES_DB: appdb
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app -d appdb"]
interval: 5s
timeout: 3s
retries: 10
cache:
image: redis:7-alpine
volumes:
pgdata:- Every service becomes a container. Service names (
db,cache) are their hostnames. - Compose automatically creates a private network for the project.
depends_onwithservice_healthywaits for the health check to pass, not just for the container to start.- Only
apipublishes a port. The database stays private.
Everyday commands
docker compose up -d # build (if needed) and start everything in the background
docker compose ps # status
docker compose logs -f api # follow one service's logs
docker compose exec db psql -U app appdb # shell into a service
docker compose restart api
docker compose up -d --build # rebuild after code changes
docker compose down # stop and remove containers + network
docker compose down -v # …and delete volumes (wipes the database!)Environment variables and .env
Don't hard-code secrets in compose.yaml. Compose automatically reads a .env file in the same folder:
# .env (add to .gitignore!)
POSTGRES_PASSWORD=super-secret
API_PORT=3000services:
db:
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
api:
ports:
- "${API_PORT:-3000}:3000" # default value if unset
env_file:
- ./api.env # load many variables from a fileDevelopment setup: live reload
Use a second file to layer dev-only settings on top:
# compose.override.yaml — loaded automatically by `docker compose up`
services:
api:
command: npx nodemon server.js
volumes:
- ./:/app
- /app/node_modules
environment:
NODE_ENV: developmentFor production, use explicit files: docker compose -f compose.yaml -f compose.prod.yaml up -d.
Newer Compose versions also support develop: watch:, which syncs files and rebuilds automatically with docker compose watch.
Scaling and profiles
docker compose up -d --scale worker=3 # run 3 copies of a stateless serviceservices:
adminer: # DB admin UI, only when asked for
image: adminer
ports: ["8081:8080"]
profiles: ["tools"]docker compose --profile tools up -dCompose vs Kubernetes
Compose is perfect for local development, CI test environments and small single-server deployments. When you need many servers, self-healing, rolling updates and autoscaling, you graduate to Kubernetes. The concepts (services, volumes, env, health checks) carry over directly.
Practice
Write a compose.yaml for WordPress + MySQL:
db:mysql:8with a named volume and a health check (mysqladmin ping)wordpress: thewordpressimage on port 8080, connecting todb, starting after the DB is healthy- Passwords come from a
.envfile
Run it, finish the WordPress installer at http://localhost:8080, then down and up again and confirm your site survived.
1.In Compose, how does the api service connect to the db service?
2.What does docker compose down -v do that down doesn't?