Compose Override Files and Environments
What you'll learn: How to manage different deployment environments (dev, staging, production) using Docker Compose override files without duplicating configuration.
The Challenge of Multiple Environments
When you deploy applications, you rarely use identical settings everywhere. Your development environment might need verbose logging and exposed debug ports, while production requires tighter security and optimized performance. Copying your entire docker-compose.yml file for each environment creates maintenance nightmares.
How Override Files Work
Docker Compose automatically looks for two files:
- docker-compose.yml — your base configuration (shared across all environments)
- docker-compose.override.yml — local overrides (automatically merged if present)
Think of it like a transparent overlay: the base file is your blueprint, and override files add or modify specific details without rewriting everything.
Multiple Environment Files
You can create environment-specific overrides:
# docker-compose.yml (base)
services:
web:
image: myapp:latest
ports:
- "80:8000"
# docker-compose.dev.yml
services:
web:
build: .
volumes:
- ./src:/app/src # Live code reloading
environment:
- DEBUG=true
# docker-compose.prod.yml
services:
web:
restart: always
environment:
- DEBUG=false
Run with: docker-compose -f docker-compose.yml -f docker-compose.dev.yml up
The files merge intelligently—later files override earlier ones. This keeps your base config DRY (Don't Repeat Yourself) while adapting to each environment's needs.
Key Takeaway: Use docker-compose.override.yml for automatic local customization and explicit -f flags with multiple files to manage dev, staging, and production environments from a single base configuration.