Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 501
Dirk Avatar asked Aug 06 '18 16:08

Dirk


People also ask

How do I override Docker compose environment variables?

Overriding a single value in your docker-compose . env file is reasonably simple: just set an environment variable with the same name in your shell before running your docker-compose command.

What does Docker compose override do?

The docker-compose. override. yml file, as its name suggests, contains configuration settings that override the base configuration, such as configuration that depends on the deployment environment. You can have multiple override files with different names also.

Can you use environment variables in Docker compose?

In Docker Compose, IT admins can use environment variables to generalize configurations for different situations, deployment environments and security contexts without editing the main project file(s) manually. Variables can either be passed as command-line arguments -- suitable for only a few parameters -- or via a .

How do I set an environment variable on an existing container?

Use the -e , --env , and --env-file flags to set simple (non-array) environment variables in the container you're running, or overwrite variables that are defined in the Dockerfile of the image you're running.


1 Answers

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.

like image 81
supremebeing7 Avatar answered Oct 04 '22 11:10

supremebeing7