Cheatsheet: Docker Compose

Last updated 2026-09-26

Basic Commands

Start services in the foreground (build if needed)

docker compose up

Start services in the background (detached)

docker compose up -d

Rebuild images and start services

docker compose up -d --build

Stop and remove containers and the default network

docker compose down

Stop and remove containers, network, and named volumes

docker compose down -v

Stop services without removing them

docker compose stop

Start previously-stopped services

docker compose start

Restart services

docker compose restart

List containers for this project

docker compose ps

Logs, Exec & One-off Commands

Follow logs for all services

docker compose logs -f

Follow logs for one service, last 100 lines

docker compose logs -f --tail 100 <service>

Open a shell in a running service's container

docker compose exec <service> /bin/sh

Run a one-off command in a new container, then remove it

docker compose run --rm <service> <command>

View resolved config after variable substitution and merges

docker compose config

List running processes per service

docker compose top

Building, Images & Cleanup

Build (or rebuild) images without starting containers

docker compose build

Build without using the Docker layer cache

docker compose build --no-cache

Pull the latest images for all services

docker compose pull

List images used by this project's containers

docker compose images

Remove stopped service containers

docker compose rm

Remove containers, default network, and any orphaned containers

docker compose down --remove-orphans

Scaling, Profiles & Multiple Compose Files

Run multiple instances of one service

docker compose up -d --scale worker=3

Start only services tagged with a given profile (plus untagged ones)

docker compose --profile debug up -d

Layer a second compose file on top of the base file (later files win)

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

Load environment variables from a specific .env-style file

docker compose --env-file .env.production up -d

compose.yml Syntax

A minimal compose.yml: two services, a named volume, and a dependency

services:
  web:
    build: .
    ports:
      - "8080:80"
    environment:
      - NODE_ENV=production
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./src:/app/src

  db:
    image: postgres:16
    volumes:
      - db_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      timeout: 3s
      retries: 5

volumes:
  db_data:

build: — build from a local Dockerfile instead of a prebuilt image

ports: — map host:container ports

volumes: — named volumes vs. bind mounts

depends_on with condition: — wait for a dependency to actually be healthy

FAQ