← All cheatsheets
Docker Compose Cheatsheet
Define and run multi-container applications with a single compose.yaml.
What it is
Use Compose to spin up an app plus its dependencies (db, cache, queue) locally, share a reproducible dev stack with your team, or run small production stacks.
Installation
Docker Compose v2 is bundled with Docker Desktop and modern Docker Engine. Use docker compose (space, not hyphen). The legacy docker-compose binary is end-of-life.
Quick start
Build/pull and start the stack in the background.
Show service status.
Tail logs for one service.
Stop and remove containers + default network.
Common commands
| Task | Command | Description |
|---|---|---|
| Start services | docker compose up | Add -d for detached, –build to rebuild. |
| Stop and remove | docker compose down | Add -v to also remove named volumes. |
| Build images | docker compose build | Rebuilds images defined by build:. |
| Pull images | docker compose pull | Fetch the latest image: tags. |
| List services | docker compose ps | State, ports, health. |
| View logs | docker compose logs -f | Follow logs across all services. |
| Exec in service | docker compose exec <svc> sh | Run a command in a running service container. |
| One-off task | docker compose run --rm <svc> <cmd> | Start a temporary container with overrides. |
| Render merged config | docker compose config | Resolved YAML after env interpolation. |
| Watch & sync | docker compose watch | Auto-sync code or rebuild on file changes. |
Useful flags
| Flag | Example | Meaning |
|---|---|---|
-d | docker compose up -d | Detach; run in the background. |
--build | docker compose up --build | Rebuild images before starting. |
-f | docker compose -f compose.ci.yaml up | Pick a specific Compose file (repeatable). |
-p | docker compose -p staging up | Override project name. |
--profile | docker compose --profile debug up | Enable services tagged with a profile. |
-v | docker compose down -v | Remove named volumes on down. |
Real-world examples
Minimal compose.yaml
services:
web:
image: nginx:alpine
ports: ["8080:80"]
redis:
image: redis:7-alpineRebuild and recreate one service
docker compose up -d --build --force-recreate apiLive dev with watch
services:
api:
build: .
develop:
watch:
- action: sync
path: ./src
target: /app/src
- action: rebuild
path: package.jsonThen run docker compose watch.
Best practices
- Keep one compose.yaml per app; layer overrides via compose.override.yaml or -f.
- Use named volumes for stateful services so
downdoesn't wipe data. - Pin image tags and Dockerfile base images for reproducibility.
- Use profiles to keep optional services (debug, seed) out of default up.
- Run
docker compose configin CI to catch YAML/env mistakes early.
Troubleshooting
Changes to Dockerfile not picked up
Force rebuild.
Variable from .env not substituted
Confirm interpolation with the merged config.
Volume keeps old data after schema change
Remove the named volume on down.
Related cheatsheets
Last reviewed: 2026-06-15