Skip to content

Docker Compose cheat sheet

Compose v2 (the docker compose plugin). The file is compose.yaml; docker-compose.yml still works.

14 entries

Commands

docker compose up -d

Create and start everything in the background.

docker compose down
docker compose down -v

Stop and remove containers (and volumes with -v).

docker compose ps
docker compose logs -f api

Status; follow one service’s logs.

docker compose exec api sh
docker compose run --rm api npm test

Shell in a running service; one-off command.

docker compose build --no-cache
docker compose up -d --build

Rebuild images.

docker compose pull
docker compose restart api

Update images; restart one service.

docker compose config

Print the resolved file (checks syntax and variables).

compose.yaml

services:
  api:
    build: .
    ports:
      - "8000:8000"
    env_file: .env
    depends_on:
      db:
        condition: service_healthy
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - pgdata:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      retries: 5
volumes:
  pgdata:

API + Postgres with a healthcheck so the API waits for the DB.

    restart: unless-stopped

Restart policy.

    volumes:
      - ./src:/app/src

Bind-mount code for live reload in development.

    environment:
      DATABASE_URL: postgres://postgres:example@db:5432/postgres

Services reach each other by service name (db).

    profiles: ["debug"]

Only started with --profile debug.

Overrides

docker compose -f compose.yaml -f compose.prod.yaml up -d

Layer files; later ones override earlier ones.

# compose.override.yaml is loaded automatically

Put dev-only settings there.

Frequently asked questions

docker-compose or docker compose?

docker compose (with a space) is Compose v2, built into the Docker CLI. The old Python docker-compose v1 is deprecated; the file format is the same.

Does depends_on wait for the database to be ready?

Only if you use condition: service_healthy together with a healthcheck on the dependency. Plain depends_on only waits for the container to start.

How do I pass environment variables?

Use environment: for inline values, env_file: for a file, and ${VAR} interpolation which reads from your shell or a .env file next to compose.yaml.

Related cheat sheets