How to reference host environment variable values in Docker-compose when using env_file?

53 views Asked by At

I'm currently deploying an application using Docker-compose and using env_file for managing environment variables. I'd like to reference the value of a specific environment variable from the host's environment in my env_file.

How can I configure this?

For example, let's assume the following scenario: there is an environment variable called MY_SECRET_KEY on the host, and I want to use this value in the env_file of Docker-compose.

docker-compose.yml:

version: '3.7'
services:
  myapp:
    image: myapp:latest
    env_file:
      - .my.env

.my.env:

API_KEY=${MY_SECRET_KEY}

Container config Env:

$ docker inspect myapp | jq '.[].Config.Env'
[
  ...
  "API_KEY=${MY_SECRET_KEY}",
  ...
]

In the above setup, is there a way to make ${MY_SECRET_KEY} in the .my.env file reference the value of MY_SECRET_KEY from the host?

0

There are 0 answers