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
docker compose up -dBuild/pull and start the stack in the background.
docker compose psShow service status.
docker compose logs -f apiTail logs for one service.
docker compose downStop and remove containers + default network.
Common commands
| Task | Command | Description |
|---|---|---|
| Start services | | Add -d for detached, --build to rebuild. |
| Stop and remove | | Add -v to also remove named volumes. |
| Build images | | Rebuilds images defined by build:. |
| Pull images | | Fetch the latest image: tags. |
| List services | | State, ports, health. |
| View logs | | Follow logs across all services. |
| Exec in service | | Run a command in a running service container. |
| One-off task | | Start a temporary container with overrides. |
| Render merged config | | Resolved YAML after env interpolation and overrides. |
| Watch & sync (dev) | | Auto-sync code or rebuild on file changes. |
| Custom file | | Use a non-default Compose file. |
| OCI Compose project | | Run a Compose project published to a registry. |
Useful flags
| Flag | Example | Meaning |
|---|---|---|
| -d | | Detach; run in the background. |
| --build | | Rebuild images before starting. |
| -f | | Pick a specific Compose file (repeatable). |
| -p | | Override project name (affects container/network prefix). |
| --profile | | Enable services tagged with a profile. |
| -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 `down` doesn'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 config` in CI to catch YAML/env mistakes early.
Troubleshooting
Changes to Dockerfile not picked up
Force rebuild.
docker compose up -d --buildVariable from .env not substituted
Confirm interpolation with the merged config.
docker compose configVolume keeps old data after schema change
Remove the named volume on down.
docker compose down -vPort conflict on up
Stop the other process or change the host port mapping in compose.yaml.