-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support multiple .env files #7326
Comments
Seems related to #7289 |
I'd like to see this as well. |
You can select dotEnv file to be used by Compose using |
Perhaps it would be sufficient to just specify the env files in docker-compose.yaml which is already supported:
And just specify ENV when running the command. |
env_file in compose.yaml is the one used to define container environment, not the one used to parse yaml and interpolate |
How about just allowing for multiple |
This is a real pain that I've come across also, however, I got a little creative in the meantime to try and work around it. I hope this may be helpful to someone else. I use makefiles to make the repetitive tying of commands easier, which also allows some extra abilities for things like env vars: .EXPORT_ALL_VARIABLES:
RED=$(shell tput setaf 1)
RESET=$(shell tput sgr0)
# Specify some legal environments and have a variable with a default
envs = dev,tst,prd
ENV ?= dev
# Throw if not a legal environment
ifeq (, $(findstring $(ENV),$(envs)))
$(error "$(RED)Provided ENV '$(ENV)' is not a valid environment. One of $(envs) must be used.$(RESET)")
endif
# Define some env file paths
defaults = .env.defaults
env = .env
dep = .env.$(ENV)
# Include (without erroring) those file paths
-include $(defaults)
export $(shell sed 's/=.*//' $(defaults) > /dev/null 2>&1)
-include $(env)
export $(shell sed 's/=.*//' $(env) > /dev/null 2>&1)
# Write out the commands, notice the dep env file is loaded here to ensure
# it is not confused by the normal .env file which we already loaded
up:
docker compose --env-file=$(dep) up Please bear in mind that the above is not tested as it's not exactly how I do things, but I thought it might help as an example of what can be done. |
So, couple of years later, the problem is still big pain when setuping dev projects. |
Sooo, if you happen to need a hacky script there you go ;) #!/usr/bin/env bash
##
# docker-compose bin wrapper that loads `.env` if found (default functionality) and additionally overrides values from `.env.local` if exists.
# Does not (and must not) produce additional output so that the PhpStorm could use it as docker-compose replacement.
# Also script name must be exactly `docker-compose` for the PhpStorm.
##
# exit script on first failed command or unset variable and if pipeline fails
set -euo pipefail
# echo on
#set -x
# if `--env-file` was not passed, load .env and .env.local
if [[ "$*" != *"--env-file"* ]]; then
set -o allexport
if [[ -f .env ]]; then
source .env
fi
if [[ -f .env.local ]]; then
source .env.local
fi
set +o allexport
fi
# `"$@"` is very important, it ensures that arguments are split and quoted correctly when passing quoted argument with space in the middle
# https://stackoverflow.com/a/10836225/846432
/usr/local/bin/docker-compose "$@" I was able to successfully use it from terminal, just add it to So as far as I am concerned I completely solved my problem. When docker-compose will add support for multiple env files I will still need to keep a wrapper so that my logic for How to adapt this script for you case?
|
@frob This command for me does not work: |
It's not implemented. "How about just allowing..." |
I can't just pass multiple |
@nemanjam I asked your same question and the answer is nope, It is not implemented unfortunately. |
and yet per https://docs.docker.com/compose/environment-variables/#the-env_file-configuration-option it says:
yet i see it doesn't work, even though multiple files are specified in "env_file" |
@a1exus Check my docker repo and see what are you doing wrongly. Note I have multiple branch and this is the docker-compose branch. I hope it can help you. Do not hesitate to contribute and create pull request if you wanted. |
"Merging" several .env files "on-the-fly" is possible by passing the content via stdin to the arg --env-file:
And if you want to check whether the .env.* files exists or not:
Hope it helps :) |
@samuelvi Does not work for me, warning shows that no env variables are loaded:
I tried the following minimal example:
docker-compose version v2.12.2 Any help would be appreciated! |
|
I was able to make it work with:
|
It works for me only with old docker compose version (v2.2.2). |
It would be really helpful to have a global definition of
|
@davidtrattnig if you don't want to repeat yourself you can use yaml anchors:
|
after #10284 |
Shouldn't this be mentioned in the Release Notes? And docs? |
indeed. Release note is generated from pull requests description, but here multiple changes/features were introduced |
I could finally make multiple --env-file work. The problem seems to be related with docker-compose being installed. Docker already has "docker compose" that seems to be the same as "docker-compose", but maybe there was a conflict. Fully uninstalling docker-compose and docker, and installing docker again made passing multiple --env-file work. Like the example "docker compose --env-file a.env --env-file b.env" |
Thanks! |
Hi all, the support for multiple environment files is part of which docker compose version? Regards. |
@karand1985 I think it is not supported the way that I think you expect it to be implemented. Just use this trick instead: https://github.com/kasir-barati/docker/tree/docker-compose?tab=readme-ov-file#tips |
I think this issue should not have been closed before the feature was also supported in yml, which as of today is still not the case AFAICS. |
@andreworg mutiple |
It is actually supported as a service top-leve element: https://docs.docker.com/compose/compose-file/05-services/#env_file I also tried using include with the "long syntax": https://docs.docker.com/compose/compose-file/14-include/#long-syntax |
there's no
indeed. |
I have been able to use include to supply multiple env files for use in interpolation in the compose file.
Note one potential gotcha: any file called ".env" will override anything that is supplied in the provided env_file files. So if your first env_file is called .env, which would not be unusual, it will actually be applied again at the end, making it look like the files are applied in the wrong order. Suggestions for a better name than |
This looks awesome and works manually on command line but inside of a standard .sh script it reports an error. Does anyone know why? |
Is it a "/bin/sh" script or a "/bin/bash" script? the process redirection feature |
It's pretty common to have multiple .env files to override settings and is used in many projects. Some of the examples would be:
.env.dev
) and prod (.env.prod
) stages and have general configuration applied to both stages (.env
)..env
and override some settings with.env.local
for each developer.Multiple file support is implemented for
docker run
and asenv_file
docker-compose property and it works well for environment variables in docker container. However different environment variables are very useful in docker-compose.yaml too. E.g. I have a project where url is setup via labels and I do have different urls for prod and dev stages.Right now I think the only way to have separated dev and prod stages is having two files with all options in both them but that is not DRY and error prone when adding/changing values.
The text was updated successfully, but these errors were encountered: