Having multiple Docker Compose files in the same directory clearly 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 and 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.
- Docker Compose
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 or the
-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
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:
- If you are Windows or macOS user, enable the Kubernetes feature.
- Multiple ways to run Kubernetes clusters locally.
- Install Nomad.
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.