admin管理员组

文章数量:1221395

I'm debugging a container and trying to speed up my development time I find myself writing a docker-compose.yml to replace a 'docker run' command.

The 'docker run' command works fine, the container starts and behave as expected. (I'm having an issue with authentication but it's a different topic). See below:

docker run \
-e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/adc_creds.json \
-v ${ADC}:/tmp/keys/adc_creds.json:ro \
"image_name"

The .yml file looks like this:

docker-compose.yml

services:

The error I get is:

WARN[0000] The "ADC" variable is not set. Defaulting to a blank string. 

From this I understand that the variable "ADC" doesn't exist in the host but when I do

echo $ADC

I get the expected value, therefore I don't understand what's going on inside the docker-compose.yml. I hope someone can give me a hint on how to go about.

Thanks

I'm debugging a container and trying to speed up my development time I find myself writing a docker-compose.yml to replace a 'docker run' command.

The 'docker run' command works fine, the container starts and behave as expected. (I'm having an issue with authentication but it's a different topic). See below:

docker run \
-e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/adc_creds.json \
-v ${ADC}:/tmp/keys/adc_creds.json:ro \
"image_name"

The .yml file looks like this:

docker-compose.yml

services:

The error I get is:

WARN[0000] The "ADC" variable is not set. Defaulting to a blank string. 

From this I understand that the variable "ADC" doesn't exist in the host but when I do

echo $ADC

I get the expected value, therefore I don't understand what's going on inside the docker-compose.yml. I hope someone can give me a hint on how to go about.

Thanks

Share Improve this question edited Feb 7 at 8:00 VLAZ 29k9 gold badges62 silver badges83 bronze badges asked Feb 7 at 1:43 Horacio NesmanHoracio Nesman 1112 silver badges11 bronze badges 2
  • 1 Please don't include images when you can easily copy-and-paste the text. Images may not outlive questions and they inhibit others' ability to use their (text) content. – DazWilkin Commented Feb 7 at 3:00
  • I'm unable to repro your issue. Setting ADC on the host to a folder path works with both docker and docker compose. – DazWilkin Commented Feb 7 at 3:14
Add a comment  | 

3 Answers 3

Reset to default 2

Reproducing the issue

I just tested your case on macOS with Docker Desktop v27.4.0 and Docker Compose v2.31.0-desktop.2 (since you didn't specify your versions) and everything just works:

$ echo $ADC
/Users/mikalai/Documents/personal/compose/adc_creds.json

$ cat .env # just to have it

$ cat compose.yml 
services:
  web:
    build: .
    ports:
      - "8000:5000"
    env_file:
      - .env
    environment:
      - GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/adc_creds.json
    volumes:
      - ${ADC}:/tmp/keys/adc_creds.json:ro

$ docker compose up -d
[+] Running 1/1
 ✔ Container compose-web-1  Started                                                                                                                                                                              0.1s

$ docker ps -a
CONTAINER ID   IMAGE         COMMAND               CREATED         STATUS         PORTS                    NAMES
678b8a3058af   compose-web   "flask run --debug"   7 minutes ago   Up 3 seconds   0.0.0.0:8000->5000/tcp   compose-web-1

$ docker exec -it compose-web-1 /bin/sh
/code # ls -l $GOOGLE_APPLICATION_CREDENTIALS
-rw-r--r--    1 root     root             0 Feb  7 12:39 /tmp/keys/adc_creds.json

Possible root causes

  1. Env var is indeed not set. I can assume that you are either

    • Not exporting your env var (export ADC=...) which is why the child process does not see it. Confirm by setting env var with $ export ADC=...
    • Running a docker compose command in different shell session (e.g. in different terminal) where the ADC var is not set. Could you please confirm that by running docker compose up... and echo $ADC sequentially in one terminal?
  2. Your Docker / Compose version doesn't support direct host env var reading. Since I'm not sure if this "feature" was actually added at some point (I think it should have always worked) I'm not going to go over the versions right now, but just wait for your answer.

Related resources

  • How can I use environment variables in docker-compose?
  • https://docs.docker.com/compose/how-tos/environment-variables/set-environment-variables/
  • https://forums.docker.com/t/docker-compose-not-seeing-environment-variables-on-the-host/11837/3

Docker-compose accept key-value pair and for the key you cannot provide a variable, so if you want to achieve same behaviour you need to first substitute the variables with another command and then start the docker-compose. You can do it by running the below command:

envsubst < docker-compose.yaml | docker compose -f - up

Thanks both for your response.

My setup is macOS with Docker Desktop v27.4.0 and Docker Compose v2.31.0-desktop.2.

I just tested now and it works fine. I guess last night I was tired and I was pressing the wrong button. My '.yml' file looks the same as yours @mikalai.

Thanks very much for your time.

本文标签: