79421030

Date: 2025-02-07 13:26:57
Score: 0.5
Natty:
Report link

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

Reasons:
  • Blacklisted phrase (0.5): How can I
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: mikalai