Docker Compose Spec Cheat Sheet
Quick reference guide for Docker Compose: Specification format, service definitions, volume/network mounts, and CLI.
Containers & Cloud
docker
docker-compose
containers
Docker Compose is a tool for defining and running multi-container Docker applications using a declarative Compose specification YAML file.
`compose.yaml` Structure Example
yaml
name: my-app-stack
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
NODE_ENV: production
DATABASE_URL: postgres://user:pass@db:5432/appdb
depends_on:
db:
condition: service_healthy
restart: unless-stopped
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: appdb
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U user -d appdb"]
interval: 10s
timeout: 5s
retries: 5
volumes:
pgdata:
Service Directives Key Reference
Table
| Directive | Purpose | Example Value |
|---|---|---|
image | Specify pre-built Docker hub image | image: redis:7-alpine |
build | Build image from Dockerfile path | build: { context: ./app } |
ports | Map host port to container port | ports: ["8080:80"] |
environment | Set container environment variables | environment: [DEBUG=true] |
env_file | Import env variables from file | env_file: [.env.production] |
volumes | Mount host directory or named volume | volumes: ["./data:/app/data"] |
restart | Container restart policy | restart: unless-stopped |
Essential `docker compose` CLI v2 Commands
Table
| Command | Purpose / Action |
|---|---|
docker compose up -d | Build, create, and start containers in background |
docker compose down | Stop containers and remove networks created by up |
docker compose down -v | Stop containers AND delete persistent named volumes |
docker compose ps | List status of all running stack containers |
docker compose logs -f web | Follow real-time log output for service |
docker compose exec web sh | Execute interactive shell command inside container |
docker compose build --no-cache | Force rebuild of service image without build cache |
Common Pitfalls & Tips
[!WARNING] Use
docker compose(with space, CLI v2 plugin) instead of deprecateddocker-compose(with hyphen, Python v1 CLI).
[!TIP] Always quote port bindings like
"80:80"in YAML to prevent YAML parsers from converting60:60into sexagesimal base-60 integers!