Docker Compose Environment Variables: Best Practices
Environment variables are essential for configuring containerized applications. This guide covers various approaches to manage them in Docker Compose.
Basic Environment Configuration
services:
app:
image: node:alpine
environment:
- NODE_ENV=production
- DATABASE_URL=postgres://user:pass@db:5432
- API_KEY=${API_KEY}
env_file:
- .env
- .env.production
Environment Variable Sources
- Direct Definition
environment: - KEY=value - ANOTHER_KEY=another_value
- Environment Files
env_file: - .env - .env.${ENVIRONMENT}
- Shell Variables
environment: - API_KEY=${API_KEY}
Environment File Priority
- File Loading Order
.env
(default).env.local
.env.${ENVIRONMENT}
.env.${ENVIRONMENT}.local
- Variable Override Rules
- Later files override earlier ones
- Direct environment variables override files
- Shell variables override all
Docker Compose 3.x Features
- Extended Environment Syntax
environment: NODE_ENV: production DATABASE_URL: postgres://user:pass@db:5432
- Conditional Environment
environment: - NODE_ENV=${NODE_ENV:-development} - API_URL=${API_URL:-http://localhost:3000}
- Environment Templates
environment: - CONFIG_PATH=/config/${ENVIRONMENT} - LOG_LEVEL=${LOG_LEVEL:-info}
Secrets Management
1. Docker Secrets
services:
db:
image: postgres
secrets:
- db_password
environment:
POSTGRES_PASSWORD_FILE: /run/secrets/db_password
secrets:
db_password:
file: ./secrets/db_password.txt
2. External Secrets
services:
app:
image: myapp
secrets:
- source: vault_secret
target: vault_token
secrets:
vault_secret:
external: true
Docker Swarm Mode Considerations
- Secret Management
services: app: secrets: - source: db_password target: db_password mode: 0440
- Environment Variables
services: app: environment: - NODE_ENV=production env_file: - .env.production
- Config Management
services: app: configs: - source: app_config target: /app/config.json
Best Practices
- Security
- Never commit sensitive data
- Use secrets for credentials
- Implement proper access controls
- Organization
- Use descriptive variable names
- Group related variables
- Document required variables
- Maintenance
- Version control .env.example
- Use environment-specific files
- Implement validation
Common Patterns
1. Development vs Production
services:
app:
env_file:
- .env.${NODE_ENV:-development}
2. Multi-stage Configuration
services:
app:
environment:
<<: *base_env
<<: *${ENVIRONMENT}_env
3. Dynamic Configuration
services:
app:
environment:
- CONFIG_PATH=/config/${ENVIRONMENT}
Troubleshooting
- Variable Substitution
# Check variable values docker compose config
- Missing Variables
# List all environment variables docker compose exec app env
- File Permissions
# Set proper permissions chmod 600 .env
Advanced Topics
- Variable Interpolation
- Nested variables
- Default values
- Conditional values
- Secret Rotation
- Automated updates
- Zero-downtime deployment
- Audit logging
- Configuration Validation
- Required variables
- Type checking
- Value constraints
Remember to always follow the principle of least privilege and implement proper security measures when handling sensitive data.