Docker Tutorial · Docker Compose · Lesson 21 of 26

Environment Files, Profiles and Overrides

Configure Compose per environment with .env files, variable interpolation, profiles and override files.

Create a free account to save this page, take notes and track your progress. Sign in

Variables

Compose reads a .env file next to compose.yaml and substitutes ${VAR} and ${VAR:-default} in the file. env_file: passes a file's variables into a container.

Profiles

Services with profiles: [debug] start only when that profile is enabled (docker compose --profile debug up), handy for admin tools you don't always want running.

Overrides

compose.override.yaml is merged automatically, so development settings (bind mounts, debug ports) can live there while compose.yaml stays production-like. docker compose config shows the final merged result.

Interpolation, defaults and profiles
cat > compose.yaml <<'EOF'
services:
  api:
    image: alpine:3.24
    environment:
      LOG_LEVEL: ${LOG_LEVEL:-info}
      DB_HOST: ${DB_HOST}
  adminer:
    image: alpine:3.24
    profiles: [debug]
EOF
echo "DB_HOST=db.internal" > .env
echo "--- services by default: $(docker compose config --services | tr '\n' ' ')"
echo "--- with --profile debug: $(docker compose --profile debug config --services | sort | tr '\n' ' ')"
echo "--- resolved environment:"
docker compose config --format json | python3 -c 'import json,sys; e=json.load(sys.stdin)["services"]["api"]["environment"]; [print(f"  {k}={v}") for k,v in sorted(e.items())]'
LOG_LEVEL=debug docker compose config --format json | python3 -c 'import json,sys; print("  with LOG_LEVEL=debug in the shell:", json.load(sys.stdin)["services"]["api"]["environment"]["LOG_LEVEL"])'

Output

--- services by default: api 
--- with --profile debug: adminer api 
--- resolved environment:
  DB_HOST=db.internal
  LOG_LEVEL=info
  with LOG_LEVEL=debug in the shell: debug

Run this on your computer — see “Get Started” for setup.

Test yourself

How do you see the final configuration after .env interpolation and override files?

  1. docker compose ps
  2. docker compose config
  3. cat compose.yaml
  4. docker inspect
Show answer

Answer: docker compose config

config prints the fully resolved, merged model.

Was this page helpful?