Join our Discord Server

Docker Compose Cheatsheet

← 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 servicesdocker compose upAdd -d for detached, –build to rebuild.
Stop and removedocker compose downAdd -v to also remove named volumes.
Build imagesdocker compose buildRebuilds images defined by build:.
Pull imagesdocker compose pullFetch the latest image: tags.
List servicesdocker compose psState, ports, health.
View logsdocker compose logs -fFollow logs across all services.
Exec in servicedocker compose exec <svc> shRun a command in a running service container.
One-off taskdocker compose run --rm <svc> <cmd>Start a temporary container with overrides.
Render merged configdocker compose configResolved YAML after env interpolation.
Watch & syncdocker compose watchAuto-sync code or rebuild on file changes.

Useful flags

FlagExampleMeaning
-ddocker compose up -dDetach; run in the background.
--builddocker compose up --buildRebuild images before starting.
-fdocker compose -f compose.ci.yaml upPick a specific Compose file (repeatable).
-pdocker compose -p staging upOverride project name.
--profiledocker compose --profile debug upEnable services tagged with a profile.
-vdocker compose down -vRemove 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

Related cheatsheets


Last reviewed: 2026-06-15
Join our Discord Server