9

Given a very simple docker-compose file:

version: '3'

services:
  test:
    image: busybox
    env_file:
      - my.env
    command: env

And the my.env file referenced in there:

FOO=BAR

Running docker-compose up prints as expected (container name prefix defaults to the name of the parent directory):

test_1  | PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
test_1  | HOSTNAME=00dc4fa7f38a
test_1  | FOO=BAR
test_1  | HOME=/root
tmp_test_1 exited with code 0

Overriding the variable from the env_file from the outside, as in FOO=SOMETHING_ELSE docker-compose up, will not be reflected in the container though. The output will still contain FOO=BAR as defined in the my.env.

Is it possible somehow to override variables defined in the env_file from the outside, without changing them inside the file? I know that it will work if I extend my docker-compose.yml file with the line

environment:
  - FOO

However, this does not really scale to a larger amount of variables - one always has to make sure that the env_file and docker-compose.yml are in sync to prevent nasty bugs.

Dirk
  • 9,381
  • 17
  • 70
  • 98

2 Answers2

4

I'm not aware of a way to do what you are asking, at least in the context of docker-compose up. There is an option for specifying env vars with docker-compose run):

docker-compose run -e DEBUG=1 web python console.py

I'm not sure what your use case is, but using override files has been the best way for me to accomplish having different env vars defined on a per user basis during development or testing/CI.

supremebeing7
  • 1,073
  • 9
  • 26
  • My use case is to have sane variable defaults listed in the `env_file` but have the possibility to override them when starting my stack from Jenkins CI where I would set them via build parameters which end up as environment variables. My current work-around is to write these environment variables to a file and replace the default `env_file` with it. Apparently, it will not get much better than this. – Dirk Aug 07 '18 at 09:26
  • Yeah. I generally end up with an `env_file` specific to CI environment that I specify my CI provider use. Sounds like a similar approach to what you're doing. I don't know of a better way - hopefully someone else will come along with another suggestion! – supremebeing7 Aug 07 '18 at 18:02
0

Try the following

version: '3'

services:
  test:
    image: busybox
    env_file:
      - my.env
    environment:
      - FOO=SOMETHING_ELSE
    command: env

For whatever reason, env_file takes precedence over shell despite the docs saying otherwise. It may be because env_file is explicitly stated in the docker-compose.yml

Pants
  • 2,563
  • 1
  • 20
  • 34
  • [By the way, according to the docs](https://docs.docker.com/compose/environment-variables/set-environment-variables/), "When both `env_file` and `environment` are set for a service, values set by `environment` have precedence." – Michael Currie Jul 02 '23 at 14:44
  • @MichaelCurrie right that's why I said, "despite the docs saying otherwise" – Pants Jul 03 '23 at 15:10
  • 1
    You said "`env_file` takes precedence over shell despite the docs saying otherwise", (which have not checked). I am saying "`environment` takes precedence over `env_file`, according to the docs", which is different and also I verified it. – Michael Currie Jul 16 '23 at 15:03