I have a situation where it would be nice to have multiple .env files, one for each service in my docker-compose.yml. Is there any way to specify a different filename to use? Can this be done on the level of individual services?
I attempted to use the env_file
tag, unfortunately, this sets variables for use in Dockerfile and at run-time. The .env file, on the other hand, sets variables to be expanded in docker-compose.yml.
Unfortunately, it's not possible to specify an alternative name for the
.env
file. I've wanted to be able to swap that out too, but you can see in the source code that's it's coded directly to.env
.Is there a reason you want to split out substitution variables per service? It feels like you should be able to put the variables for all of your services into a single
.env
file.Yes, with the
env_file
service option.Source docs: https://docs.docker.com/compose/environment-variables/#the-env_file-configuration-option