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.
- 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.
- 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
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!
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 ...
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
- It does not need to be a valid compose file, just wrote the part you want to update/add.
- By default, Compose reads two files, a
docker-compose.ymland an optional
- We need to use
-fto specify the file name or path.
- 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!
Did you find this article valuable?
Support Kratik Jain by becoming a sponsor. Any amount is appreciated!