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.
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.
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.
See Environment variables in Compose and Profiles.
Test yourself
How do you see the final configuration after .env interpolation and override files?
docker compose psdocker compose configcat compose.yamldocker inspect
Show answer
Answer: docker compose config
config prints the fully resolved, merged model.