🗓️ 03042026 2100
What it is:
- Strategies for managing environment variables and secrets in docker_compose
- Multiple mechanisms with a defined precedence order
Problem it solves:
- Multi-client deployments need different credentials, ports, domains per client
- Hardcoding config in YAML is insecure and inflexible
- Need clear separation between Compose-level substitution and container runtime config
Precedence Order (highest wins)
- CLI environment (
docker compose run -e VAR=val) - Shell environment (exported in your terminal)
.envfile (auto-loaded by Compose)environmentdirective in YAMLenv_filedirective contents- Dockerfile
ENVinstruction
Understanding this prevents "why isn't my variable taking effect" debugging.
Three Mechanisms
.env file
- Auto-loaded by Compose for variable substitution in the YAML file itself
image: myapp:${VERSION}readsVERSIONfrom.env- NOT automatically passed into containers
env_file directive
- Loads variables directly into the container at runtime
- Separate file per client:
env_file: .env.client-a - Good for secrets and runtime config (DB passwords, API keys)
environment directive
- Inline key-value pairs in YAML
- Visible in the compose file — use for non-sensitive defaults only
- Can reference shell/
.envvariables:DB_HOST: ${DB_HOST:-localhost}
Per-Client Pattern
For multi-client isolation on one host:
- Shared
.envfor Compose-level config (image versions, network names) - Per-client
env_filefor runtime secrets (.env.client-a,.env.client-b) - Keep secrets out of the YAML file entirely
- Combine with docker_compose_profiles to activate client-specific stacks
Default Values and Required Variables
${VAR:-default}provides fallback if unset${VAR:?error message}fails with error if unset — use for required config- Prevents silent misconfiguration in multi-client setups
DANGER
Never commit .env files with secrets to version control: Use .env.example with placeholder values. Add .env* to .gitignore. Leaked credentials in git history are extremely hard to fully revoke.
WARNING
.env vs env_file confusion: The .env file only works for variable substitution in the YAML file. To pass variables into running containers, use the env_file or environment directives. Mixing these up is the most common Compose config mistake.