Add option to include separate compose files under a namespace
WIStudent opened this issue · comments
What is the problem you're trying to solve
I really like the new include
top-level section, as it allowed me to split a huge docker-compose.yml file into smaller, more manageable ones. After splitting my docker-compose.yml, I found that many files contained the same service and volume definitions, just with different service names, volume names and environment variables.
I would like to include a single template.yml multiple times with different environment variables, but because service and volume names must be unique, this is not currently possible.
The following does not work
# docker-compose.yml
include:
- path: template.yml
env_file: env1.env
- path: template.yml
env_file: env2.env
# template.yml
services:
template-service:
image: someImage
environment:
VAR: ${VAR}
volumes:
- template-volume:/template-volume
volumes:
template-volume
Describe the solution you'd like
I would like to propose a new additional field namespace
. If namespace
is present, the service and volume names of the included compose file will be prefixed with its value.
# docker-compose.yml
include:
- path: template.yml
env_file: env1.env
namespace: template1
- path: template.yml
env_file: env2.env
namespace: template2
Using the same template.yml
file as above this would create the services template1-template-service
, template2-template-service
and the volumes template1-template-volume
, template2-template-volume
. I am not sure what a good separator between the namespace and the name would be, so I just used -
in this example.
Additional context
I am not the first person who has tried to include the same compose file with a different set of env files. Over at the docker compose project someone has proposed a different solution that would allow for the interpolation of service/volume names using environment variables: docker/compose#10994
Excellent idea! This would be a great addition. This could also help name mangling, such that included containers' auto-generated names are prefixed with their own project's name instead of the main (root) file's project name.