Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Docker Tip #87: Run Multiple Docker Compose Files with the -f Flag

blog/cards/docker-tips-and-tricks.jpg

We'll go over running a few docker-compose.yml files at once, even with custom file names. We'll also use the COMPOSE_FILE env variable.

In Docker tip #10 we covered how to organize multiple independent services using 1 docker-compose.yml file but this time around we’ll go over how to run multiple docker-compose.yml files at once with a single docker-compose command.

This could come in handy if you have a mono repo or a split repo with a bunch of services that can be run independently. This video demonstrates using this pattern, or you can read below for the TL;DR version.

Let’s say you have 3 docker-compose.yml files in a single project and you decide to name them a.yml, b.yml and c.yml. Each of them may have a component of your application.

You can run them all at once by running docker-compose -f a.yml -f b.yml -f c.yml up.

The -f flag lets you run a file with a custom name and you can chain together multiple -f flags. You can always run the config command to see what the final result will be. In this case Docker Compose will combine them all together as 1 unit.

If you find yourself using this pattern a lot you can define a COMPOSE_FILE environment variable in an .env file and set it to be COMPOSE_FILE=a.yml:b.yml:c.yml. Now you don’t need to pass multiple -f flags on the command line – you can run a typical docker-compose up.

The above will work on native Linux, WSL and macOS. If you’re using Windows with PowerShell you’ll want to separate each file with ; instead of :. You can also customize this path separator by setting COMPOSE_PATH_SEPARATOR in your .env file.

Free Intro to Docker Email Course

Over 5 days you'll get 1 email per day that includes video and text from the premium Dive Into Docker course. By the end of the 5 days you'll have hands on experience using Docker to serve a website.



Comments