Kratik Jain
Kratik's What Notes!💡

Kratik's What Notes!💡

Using Multiple Docker-Compose files

Using Multiple Docker-Compose files

Kratik Jain's photo
Kratik Jain

Published on Sep 19, 2021

3 min read

Subscribe to my newsletter and never miss my upcoming articles

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?

image.png

  • 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 docker-compose.prod.yml 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!


Examples

#1 Usual docker-compose.yml file

web:
  image: example/my_web_app:latest
  build: .
  volumes:
    - ".:/code"
  ports:
    - 8883:80
  depends_on:
    - db
    - cache

db:
  image: postgres:latest
  command: "-d"

cache:
  image: redis:latest
  ports:
    - 6379:6379

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

#2 docker-compose-dev.yml

web:
  environment:
    DEBUG: 'true'

db:
  environment:
    production: 'true'
  ports:
    - 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.

References

  1. docs.docker.com/get-started/overview
  2. docs.docker.com/compose/extends/#multiple-c..

Thanks for reading! have a nice day!


Wanna buy me a Coffee ☕?

buy-me-a-coffee.jpeg

 
Share this
Proudly part of