Dirk
Dirk

Reputation: 10121

Override variables in docker-compose env_file with the actual environment from the outside

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.

Upvotes: 10

Views: 11672

Answers (2)

Pants
Pants

Reputation: 2772

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

Upvotes: 0

supremebeing7
supremebeing7

Reputation: 1083

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.

Upvotes: 4

Related Questions