Variable is not set in env file

+ docker compose config
WARN[0000] The "SHARED" variable is not set. Defaulting to a blank string. 
name: tmp
services:
  test:
    environment:
      SHARED: xyz
    image: bash
    networks:
      default: null
    volumes:
      - type: bind
        source: /tmp
        target: /shared
networks:
  default:
    name: tmp_default

WARN[0000] The “SHARED” variable is not set. Defaulting to a blank string.

Why?

Here’s some info

+ lsb_release -a
Description:	Ubuntu 22.04.4 LTS

+ docker compose version
Docker Compose version v2.29.0

+ cat docker-compose.yml
services:
    test:
        image: bash
        env_file:
          - path: shared.env
            required: true
        volumes:
            - type: bind
              source: $SHARED
              target: /shared

+ cat shared.env
SHARED=xyz

The env_file does not define which .env Docker Compose will interpolate vairable from

The env_file property selects a .env file to be used as the container’s environment variables

Docker automatically interpolates variables from the file ,env, to specify a different one, use

docker compose --env-file shared.env

That is why running Docker Inspect shows you the container’s enivronment has SHARED=xyz

1 Like