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 |3 Answers
Reset to default 2Reproducing 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
Env var is indeed not set. I can assume that you are either
- Not
export
ing your env var (
) which is why the child process does not see it. Confirm by setting env var withexportADC=...$ export ADC=...
- Running a
docker compose
command in different shell session (e.g. in different terminal) where theADC
var is not set. Could you please confirm that by runningdocker compose up...
andecho $ADC
sequentially in one terminal?
- Not
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.
本文标签:
版权声明:本文标题:google cloud platform - docker-compose.yml can't a read a variable in the host machine at run time, although 'do 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739350015a2159340.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ADC
on the host to a folder path works with bothdocker
anddocker compose
. – DazWilkin Commented Feb 7 at 3:14