4

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?

1 Answer 1

2

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}

Sign up to request clarification or add additional context in comments.

3 Comments

Docker interpreting the variable after the = is working as intended, that is how they're assigned a default. What should be happening is that I export variable name itself. If you notice in my question, I export FERRO_LOG and FERRO_SECRET, nothing after an =. I have read about variable substitution and using a .env file, though that is not my desired usecase since I'd like it to pull directly from the environment.
I updated my answer with what I'm hoping will work for your case in your docker-compose file.
This looks like it's more useful than what I was looking at using originally, thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.