Is it possible to substitute (without the use of external tools like envsubst) values in an environment file used in docker-compose (using env_file) with values on a .env file in the folder?

Say I have this .env file:


Now in my docker-compose.yml I can have:

# ...
        - USE_IMAGE:${REGISTRY}/my_image:${TAG}
# ...

What I'd like is having a different environment file (say, myservice.env), like this:


And use it on the compose file like:

# ...
        - myservice.env
# ...

However by default, docker-compose (which is the one reading the .env file) doesn't seem to be substituting anything on the other files and I get the literal value.

Is there a way to do this without requiring to have a "rendered" file, directly with docker compose?

I understand I could make a script to pre-process those files using envsubst or something similar, but I'm trying to find a solution without that

  • Your myservice.env file must be in the format of KEY=VALUE. That still may not give you what you want, stackoverflow.com/q/37926473/8254477 is an alternative pattern that works quite well. – swysocki Jan 16 at 2:36
  • @swysocki oh yes, sorry, that was a typo... corrected. I'll check your link, thanks – Jcl Jan 16 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.