Compose: Feature of reading multiple .env or similar function files

Created on 24 Feb 2018  路  6Comments  路  Source: docker/compose

Apologize if this is duplicated or there are other ways to achieve this.

So at this moment it looks like docker-compose only reads a .env within same working folder to do 'variable substitution'. I'd like to have a feature that it reads multiple .env files since the compose can deal with multiple docker-compose.yml already. It may have a similar form like:

docker-compose --file docker-compose1.yml \
                          **--env 1.env \**
                          --file docker-compose2.yml \
                          **--env 2.env \**
......

I am aware of that there is env-files section inside docker-compose.yml which however is used to pass variables into containers and has nothing to do with variable substitution with docker-compose.yml itself.

Most helpful comment

Fair enough. I may still think different variable set is better to be in separate .env.

All 6 comments

@chxzqw You can pass env_file to service in your yml file. As far as I know there is no way to pass env_file as command line argument.

@armpogart thx for your comment and time reading my post.

So as I mentioned if my understanding is right, the env_file section in yml file is used to pass environment variables into containers which is different with the feature I ask for. I am after a feature which only does 'varaible substitution' just inside yml file itself by supporting multiple .env file which currently is read by yml only once.

I think currently the .env file read by docker-compose.yml and env_file section inside the yaml srcipt serve for different purposes.

@chxzqw I'm confused - why do you need multiple .env files?

@shin- In some cases I may need to do different 'variable substitution' in multiple docker-compose files.

Say for example when I run docker-compose -f docker-compose1.yml -f docker-compose2.yml up -d and have a .env file in same folder with following setting:

ROOT=value1

then both yml files will replace $ROOT part with value1. If I need the second yml file to do '$ROOT' with value2, currently I have to let .env to be

ROOT1=value1
ROOT2=value2

and both yml files have to be updated correspondently as well, which may reduce readability and maintainability.

Hm ; so you'd be looking to have each yaml file use their own .env file?

To be honest, I don't think it's something we're likely to implement. It would require a significant overhaul of the code to support it, and the value is questionable at best.

If these 2 Compose files are supposed to be used together and each need their own separate $ROOT value, then it makes sense to me that you would want to use 2 different variables anyway.

Fair enough. I may still think different variable set is better to be in separate .env.

Was this page helpful?
0 / 5 - 0 ratings