简体   繁体   中英

Passing env variables from one service to another inside docker-compose.yml

Is there a way to pass environment variables from one service to the other inside docker-compose.yml ?

services:
 testService:
    environment:
      TEST_KEY: 1234
 testServiceTests:
    environment:
      TEST_KEY: I want to pull in the value 1234 here from service1
    

No.

However, there's an alternative. You may provide environment variables to all the services within the Docker Compose file by exposing them either from your shell, when you run the Compose or by using a special .env file, See documentation .

Using this approach, you would have a global (for the Compose) environment variable, say GLOBAL_TEST_KEY (it needn't have a different name) and you would be able to share this across multiple services:

services:
 testService:
    environment:
      TEST_KEY: ${GLOBAL_TEST_KEY}
 testServiceTests:
    environment:
      TEST_KEY: ${GLOBAL_TEST_KEY}

And then: docker-compose run -e GLOBAL_TEST_KEY="Some value" ....

Or, create a file called .env alongside docker-compose.yaml and, in .env :

GLOBAL_TEST_KEY="Some value"

And then: docker-compose run ...

NOTE No need to reference .env as it's included by default

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM