Module 6 Lesson 5: Multi-File Configurations
·DevOps

Module 6 Lesson 5: Multi-File Configurations

Master the environment split. Learn how to use multiple Compose files to handle development, staging, and production differences without repeating code.

Module 6 Lesson 5: Multi-File Configurations

In a professional project, you don't just have one Compose file. You have different needs for different environments. This lesson covers how to split your configuration cleanly.

1. The Default Override

Docker Compose automatically looks for two files:

  1. docker-compose.yml: The base configuration.
  2. docker-compose.override.yml: Overrides for your local machine.
  • If both exist, Docker merges them automatically when you run docker-compose up.
  • Best Practice: Never commit docker-compose.override.yml to Git. It's for private settings (like your personal developer API keys).

2. Manual File Merging (-f)

What if you have docker-compose.prod.yml or docker-compose.test.yml?

docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
  • The order matters! The second file (-f ...prod.yml) overwrites any duplicate settings in the first file.

3. The Power of "Base + Delta"

Base File (docker-compose.yml):

Defines things that never change: The images, the networks, and the service names.

Production Delta (docker-compose.prod.yml):

Defines production-only settings:

  • Actual SSL certificates.
  • Port 443 mapping.
  • restart: always.
  • A larger memory limit.

4. Viewing the "Merged" Result

When using multiple files, it is very easy to get confused about the final state.

docker-compose -f file1.yml -f file2.yml config

This command won't start any containers; it simply prints the Final Merged YAML to your screen so you can verify it.


Exercise: The Production Split

  1. Create a docker-compose.yml with one service using nginx.
  2. Create a docker-compose.prod.yml that adds a restart: always and maps port 80:80.
  3. Run the manual merge command from Section 2.
  4. Run docker ps and check the "Ports" and "Status" of the container. Did the production settings take effect?
  5. Why is this better than having two completely separate 100-line files called dev.yml and prod.yml? (Think about maintaining the image names).

Conclusion of Module 6

You have mastered Advanced Docker Compose. You can now build self-healing, scalable, and modular application stacks that are clean enough for a team of 100 developers and robust enough for production.

Next Module: Locking down the gates: Module 7: Networking and Security.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn