Docker Compose Multi-Environment Setup
Effective management of multiple environments is crucial for maintaining consistent deployments across development, staging, and production. This guide covers various environment management strategies in Docker Compose.
Basic Environment Configuration
services:
app:
image: myapp:${TAG:-latest}
environment:
- NODE_ENV=${NODE_ENV:-development}
- DB_HOST=${DB_HOST:-localhost}
- DB_PORT=${DB_PORT:-5432}
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3
profiles:
- ${ENV:-dev}
Docker Compose 3.x Features
- Extended Environment Configuration
services: app: image: ${IMAGE_NAME:-myapp}:${TAG:-latest} environment: - NODE_ENV=${NODE_ENV:-development} - DB_HOST=${DB_HOST:-localhost} profiles: - ${ENV:-dev}
- Conditional Services
services: app: image: myapp:latest profiles: - dev monitoring: image: prometheus:latest profiles: - prod
- Environment Templates
services: app: image: ${IMAGE_NAME_${ENV:-dev}:-myapp}:${TAG_${ENV:-dev}:-latest} environment: - NODE_ENV=${NODE_ENV_${ENV:-dev}:-development}
Environment Types
- Development
services: app: image: myapp:dev environment: - NODE_ENV=development - DEBUG=true volumes: - .:/app profiles: - dev
- Staging
services: app: image: myapp:staging environment: - NODE_ENV=staging - DEBUG=false profiles: - staging
- Production
services: app: image: myapp:prod environment: - NODE_ENV=production - DEBUG=false deploy: replicas: 3 profiles: - prod
Docker Swarm Mode Considerations
- Environment Management
services: app: image: myapp:${TAG:-latest} environment: - NODE_ENV=${NODE_ENV:-development} deploy: placement: constraints: - node.role == manager profiles: - ${ENV:-dev}
- Service Configuration
services: app: image: myapp:${TAG:-latest} environment: - NODE_ENV=${NODE_ENV:-development} deploy: replicas: ${REPLICAS:-1} restart_policy: condition: on-failure
- Resource Management
services: app: image: myapp:${TAG:-latest} deploy: resources: limits: cpus: ${CPU_LIMIT:-0.5} memory: ${MEMORY_LIMIT:-512M}
Common Environment Patterns
1. Development Setup
services:
app:
image: myapp:dev
environment:
- NODE_ENV=development
- DEBUG=true
volumes:
- .:/app
profiles:
- dev
2. Staging Configuration
services:
app:
image: myapp:staging
environment:
- NODE_ENV=staging
- DEBUG=false
deploy:
replicas: 2
profiles:
- staging
3. Production Setup
services:
app:
image: myapp:prod
environment:
- NODE_ENV=production
- DEBUG=false
deploy:
replicas: 3
resources:
limits:
cpus: '0.5'
memory: 512M
profiles:
- prod
Best Practices
- Environment Management
- Use environment variables
- Implement profiles
- Maintain separate configs
- Version control
- Documentation
- Configuration Security
- Use secrets
- Environment-specific configs
- Access controls
- Regular audits
- Backup procedures
- Deployment Strategy
- Automated deployments
- Environment promotion
- Rollback procedures
- Testing strategy
- Monitoring setup
- Maintenance
- Regular updates
- Configuration review
- Performance monitoring
- Security patches
- Documentation updates
Environment Operations
1. Environment Management
# Start development environment
docker compose --profile dev up
# Start staging environment
docker compose --profile staging up
# Start production environment
docker compose --profile prod up
2. Configuration Management
# List environment variables
docker compose config --format json | jq '.services.app.environment'
# Check service configuration
docker compose config --profile prod
# Validate configuration
docker compose config
3. Environment Monitoring
# Check service status
docker compose ps
# View service logs
docker compose logs
# Monitor resource usage
docker stats
Advanced Environment Features
1. Environment Variables
services:
app:
image: myapp:${TAG:-latest}
environment:
- NODE_ENV=${NODE_ENV:-development}
- DB_HOST=${DB_HOST:-localhost}
profiles:
- ${ENV:-dev}
2. Service Profiles
services:
app:
image: myapp:latest
profiles:
- dev
environment:
- NODE_ENV=development
monitoring:
image: prometheus:latest
profiles:
- prod
environment:
- NODE_ENV=production
3. Environment Templates
services:
app:
image: ${IMAGE_NAME_${ENV:-dev}:-myapp}:${TAG_${ENV:-dev}:-latest}
environment:
- NODE_ENV=${NODE_ENV_${ENV:-dev}:-development}
deploy:
replicas: ${REPLICAS_${ENV:-dev}:-1}
Troubleshooting
- Environment Issues
# Check environment variables docker compose config --format json | jq '.services.app.environment' # View service configuration docker compose config --profile prod # Check service status docker compose ps
- Configuration Problems
# Validate configuration docker compose config # Check service logs docker compose logs # Inspect service details docker compose inspect app
- Deployment Issues
# Check deployment status docker compose ps # View deployment logs docker compose logs # Monitor resource usage docker stats
Integration with Environment Tools
- Configuration Management
- Docker Configs
- Docker Secrets
- Environment Files
- Configuration Services
- Deployment Tools
- Docker Swarm
- Kubernetes
- CI/CD Pipelines
- Deployment Automation
- Monitoring Solutions
- Prometheus
- Grafana
- ELK Stack
- Cloud Monitoring
- Security Tools
- Vault
- AWS Secrets Manager
- Azure Key Vault
- Google Secret Manager
Remember to regularly review and update your environment management strategies based on the latest best practices and requirements. Implement a comprehensive environment management plan and continuously monitor your Docker Compose setup for optimal performance.