Using Multiple Docker-Compose files

Using Multiple Docker-Compose files


3 min read

I have assumed that you have some basic knowledge of Docker & Docker Compose!

What is Docker ๐Ÿฌ ?

  • An Open-Source tool that can be used to bundle your application with all the required dependencies.
  • Similar to VMs but way more lightweight than it.
  • Uses Dockerfile
  • Written in Golang

What is Docker Compose?


  • When you have a requirement of multiple containers and need of managing it nicely, you can achieve that with Docker Compose.
  • Here you can define multiple containers, networking and how should they connect to each other, etc.
  • Uses docker-compose file
  • Written in Python

Ok So this was some basic info. Now, Let me ask you a question - Have you seen something like this before and ever wondered what it does exactly?

docker-compose -f docker-compose.yml -f up -d

By observing the few lines, one can deduce that here we are using two compose files and then launching the compose stack in detach mode(-d)

The basic idea behind this is to keep your main compose file generic for all environments (like dev, stage, and prod) as much as possible.

And for all the needs, which are specific to an environment, pass another file with those configs and it will append all the files to generate the desired config.

Use case example - You are using almost the same config on your environments but for dev, the database port is open for devs to testing OR Suppose if you want to run the dev and stage environment in DEBUG mode but not the prod one!


#1 Usual docker-compose.yml file

  image: example/my_web_app:latest
  build: .
    - ".:/code"
    - 8883:80
    - db
    - cache

  image: postgres:latest
  command: "-d"

  image: redis:latest
    - 6379:6379

Now, to pass some extra configuration updates, use another compose file called ...

#2 docker-compose-dev.yml

    DEBUG: 'true'

    production: 'true'
    - 5432:5432

Notice in the second override file we only wrote the config we wanted at the respective block of each service.

Now when you want to up your stack, you need to fire the below command :

docker-compose -f docker-compose.yml -f docker-compose-dev.yml up -d

Features of using override compose files

  1. It does not need to be a valid compose file, just wrote the part you want to update/add.
  2. By default, Compose reads two files, a docker-compose.yml and an optional docker-compose.override.yml
  3. We need to use -f to specify the file name or path.
  4. Helps you to keep your base compose file generic as much as possible, you just have to create separate override files only according to your requirements.



Thanks for reading! have a nice day!

Wanna buy me a Coffee โ˜•?


Did you find this article valuable?

Support Kratik Jain by becoming a sponsor. Any amount is appreciated!