All cheatsheets
CoreBeginner

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 -d

Build/pull and start the stack in the background.

docker compose ps

Show service status.

docker compose logs -f api

Tail logs for one service.

docker compose down

Stop and remove containers + default network.

Common commands

TaskCommandDescription
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 and overrides.
Watch & sync (dev)
docker compose watch
Auto-sync code or rebuild on file changes.
Custom file
docker compose -f compose.prod.yaml up -d
Use a non-default Compose file.
OCI Compose project
docker compose -f oci://registry.example.com/my-app:latest up
Run a Compose project published to a registry.

Useful flags

FlagExampleMeaning
-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 (affects container/network prefix).
--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-alpine

Rebuild and recreate one service

docker compose up -d --build --force-recreate api

Live dev with watch

services:
  api:
    build: .
    develop:
      watch:
        - action: sync
          path: ./src
          target: /app/src
        - action: rebuild
          path: package.json

Then 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 --build

Variable from .env not substituted

Confirm interpolation with the merged config.

docker compose config

Volume keeps old data after schema change

Remove the named volume on down.

docker compose down -v

Port conflict on up

Stop the other process or change the host port mapping in compose.yaml.

Official Docker Docs references

Last reviewed: