Docker Compose file cheatsheet

Docker Compose file: Profiles, development and reuse

5 common Docker Compose file tasks.

Make services optional

Start a service only when its profile is enabled, with docker compose --profile debug up.

services:
  adminer:
    image: adminer
    profiles: ["debug"]

Sync or rebuild on file changes

Update running containers as you edit code, with docker compose watch.

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

Override settings for development

compose.override.yaml is merged automatically over compose.yaml.

# compose.override.yaml
services:
  web:
    ports:
      - "8000:8000"
    volumes:
      - ./src:/app/src

Include another Compose file

Reuse a service definition kept in a separate file.

include:
  - path: ./infra/database.yaml

Avoid repetition with anchors

Define settings once and reuse them in several services.

x-common: &common
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: "10m"

services:
  web:
    <<: *common
    image: web:1.0
  worker:
    <<: *common
    image: worker:1.0

Was this page helpful?