Environment & Configuration

.env Files and Variables

Compose reads a .env file next to compose.yaml and substitutes ${VARIABLES}. Keep secrets out of the YAML and out of Git.

# .env  (add to .gitignore)
POSTGRES_PASSWORD=change-me
API_PORT=8000
services:
  api:
    build: .
    ports:
      - "${API_PORT:-8000}:8000"     # default 8000 if not set
    env_file: .env.api               # all variables from a file
  db:
    image: postgres:16-alpine
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}

Override Files and Profiles

  • compose.override.yaml is merged automatically: put development-only settings there (bind mounts, debug ports).
  • docker compose -f compose.yaml -f compose.prod.yaml up combines files explicitly.
  • profiles: ["tools"] on a service (e.g. pgAdmin) starts it only with --profile tools.