Wonay
Wonay

Reputation: 1250

docker-compose up - set the environment variable for the containers

I don't understand how environment: in docker-compose.yml works.

So if I have

environment:
  - MY_ENV_VAR
  - MY_SECOND_ENV_VAR

Now, how do I set them when calling docker-compose up ?

I tried with export before.

I tried with -e KEY=VALUE

But nothing seems to be working.

When I ssh to the container and check the env, I don't find MY_ENV_VAR or MY_SECOND_ENV_VAR.

Upvotes: 2

Views: 3624

Answers (2)

Wonay
Wonay

Reputation: 1250

I found the issue.

If your yml file have several services:

one:
  environment:
    - VAR_ONE
two:
  environment:
    - VAR_ONE
    - VAR_TWO

And you do

export VAR_ONE=1
export VAR_TWO=2
docker-compose up

And then ssh to two, you will NOT see VAR_TWO set.

If you change the yml to:

one:
  environment:
    - VAR_ONE
    - VAR_TWO
two:
  environment:
    - VAR_ONE
    - VAR_TWO

Notice that each service have the exact same env keys.

Then it works !

Upvotes: 4

terrywb
terrywb

Reputation: 3956

You can assign a values in the yml file.

environment:
  - MY_ENV_VAR=foo
  - MY_SECOND_ENV_VAR=bar

You can also use environment variable in the yml file.

 environment:
  - MY_ENV_VAR=${foo}
  - MY_SECOND_ENV_VAR=${bar}

Upvotes: 2

Related Questions