Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose not reading environment variables

When trying to use docker-compose, it does not appear to read exported environment variables from the host system.

The full code in question at the time of writing can be found here.

$ docker info

Client: Docker Engine - Community
Version:           19.03.6
API version:       1.40
Go version:        go1.12.16
Git commit:        369ce74a3c
Built:             Thu Feb 13 01:27:58 2020
OS/Arch:           linux/amd64
Experimental:      false
Server: Docker Engine - Community
Engine:
Version:          19.03.8
API version:      1.40 (minimum version 1.12)
Go version:       go1.12.17
Git commit:       afacb8b
Built:            Wed Mar 11 01:29:16 2020
OS/Arch:          linux/amd64
Experimental:     false
containerd:
Version:          v1.2.13
GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version:          1.0.0-rc10
GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version:          0.18.0
GitCommit:        fec3683

$ docker-compose info

docker-compose version 1.21.0, build unknown
docker-py version: 3.4.1  
CPython version: 3.7.3    
OpenSSL version: OpenSSL 1.1.1d  10 Sep 2019 

Specifically, I'm interested in the file docker-compose.yml and its section environment

version: "3"
...
environment:
    - FERRO_MONGO_HOST=ferro-mongo
    - FERRO_MONGO_USER=ferrothorn
    - FERRO_MONGO_PASS=ferrothorn-local
    - FERRO_MONGO_BASE=ferrothorn
    - FERRO_LOG_LEVEL=1
    - FERRO_SECRET

When calling docker-compose up, environment variables are set the way that I would expect them to be looking at the docker-compose file. However if I export some variable, that variable exported to the shell is not used over the default defined in the docker-compose file. This is the case for variables like FERRO_SECRET that have no default, and FERRO_LOG_LEVEL where a default is set.

The command sequence that I'm using looks like

export FERRO_SECRET=very_secret_pw
export FERRO_LOG_LEVEL=2
docker-compose up --build # tried with and without --build flag

and it just... doesn't work? I've also tried using this trick that I saw online

FERRO_SECRET=very_secret_pw FERRO_LOG_LEVEL=2 docker-compose up --build

but no dice.

Since this box is actually a wsl instance running on windows 10 using a localhost docker server, I figured it might be some bug there, but recreating these steps in windows powershell on the same machine yields the same results, as does building on a regular fedora-server box

Am I setting environment variable wrong? I seem to remember doing this before somewhere and it working, is there some bug with this docker-compose version or my configuration of it?

like image 524
gastrodon Avatar asked Oct 17 '25 16:10

gastrodon


1 Answers

It looks like Docker is interpreting the value after the equal sign as a literal.

Check out https://docs.docker.com/compose/compose-file/#variable-substitution. It mentions using an env file to set defaults or doing them inline. It also uses a dollar sign and braces for the variable.

For example: - MY_VAR=${MY_ENV_VAR:my_var_default_value}

In all of my cases I either prefix the substitution with '$'. In some, I also surround it with braces.

FERRO_LOG_LEVEL=${FERRO_LOG_LEVEL:1}
FERRO_SECRET=${FERRO_SECRET}

like image 184
Rich Avatar answered Oct 20 '25 04:10

Rich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!