docker,

Multiple Docker Compose files in the same directory

Mar 03, 2022 · 1 min read · Post a comment
Multiple Docker Compose files in the same directory

Having multiple docker-compose files in the same directory cleary is not a good idea, although I could see the use case if you are working on multiple different projects with custom docker-compose filenames and being lazy enough to put everything under the home or root directory.
Obviously, you’ll run docker-compose up and down commands using the -f flag to specify the filename. But, what happens if you plan to run multiple environment of the same docker-compose stack?
Well, from my experience only one instance could be active at the same time (it will tear down the existing stack) if you have identical service names, meaning copy paste the same docker-compose template but with different env files or exposed ports for instance. Here are a few ways to manage it better.

Prerequisites

  • Docker
  • Docker Compose

Solution(s)

Solution 1. Have different subdirectories for each project and environment stack.

Solution 2. Specify the docker-compose stack name using the COMPOSE_PROJECT_NAME env var / -p flag. For instance:

docker-compose -f docker-compose.preprod.yml up -p APP_PREPROD -d
docker-compose -f docker-compose.prod.yml up -p APP_PROD -d

Solution 3. In case of having multiple environments while sharing the same base docker-compose file, try using different override files.

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

Note: Personally, I haven’t tried it yet. Not sure if multiple environments can be active in the same time.

Check out official Docker documentation when dealing with Multiple Compose files.

Solution 4. Last but not least, deploy a Container Orchestration tool. Few options available, including:

Conclusion

If you have any other alternative solution, please do let me know in the comment section below, no sign up required though.
On a side note, follow our official channel on Telegram.