Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

environment variables not recognized in docker compose

Environment variables defined in docker-compose.yml are not set.

services:
  service1:
    image: alpine
    environment:
      - VAR1:H
      - VAR2:HI
    command:
      - /bin/sh
      - -c
      - |
        echo $VAR1
        echo $VAR2

This outputs:

$ docker compose up
WARN[0000] The "VAR1" variable is not set. Defaulting to a blank string.
WARN[0000] The "VAR2" variable is not set. Defaulting to a blank string.
[+] Running 1/0
 â ż Container compose-service1-1  Recreated                                                                                                                                                                              0.1s
Attaching to compose-service1-1
compose-service1-1  |
compose-service1-1  |
compose-service1-1 exited with code 0

Docker Compose version v2.12.0

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Envrionment variables can be either passed with map syntax or array syntax, see:

# Array syntax
services:
  service1:
    image: alpine
    environment:
      - VAR1=H
      - VAR2=HI
    command:
      - /bin/sh
      - -c
      - |
        echo $$VAR1
        echo $$VAR2

Or,

# Map syntax
services:
  service1:
    image: alpine
    environment:
      VAR1: "H"
      VAR2: "HI"
    command:
      - /bin/sh
      - -c
      - |
        echo $$VAR1
        echo $$VAR2

Then, you have to use $$. As using echo $VAR1 makes docker-compose look for the variables from your host’s environment.

See:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading