dockerdocker-composeenvironment-variables

Docker-Compose Environment-Variables blank string


I'm trying to get the Env-Variables in Docker-Compose to work. My Files:

env/test.env:

XUSER=you
XHOME=/home/${XUSER}

docker-compose.yml:

version: '3'
    services:
     abc:
        build: .
        image: xyz:latest
        container_name: xyz
        env_file:
          - env/test.env
        user: "${XUSER}"

docker-compose up --build

docker-compose config

WARNING: The XUSER variable is not set. Defaulting to a blank string.
    services:
      kernel:
        build:
          context: xyz
        container_name: xyz
        environment:
          XHOME: /home/you
          XUSER: you
        image: xyz:latest
        user: ''

As you can see user: '' is an empty string, but the env_file works. I found some old Bug reports about this issue, I'm not sure I doing something wrong or not.


Solution

  • Although the other answers are both correct they do not highlight the underlying misunderstanding here enough:

    With the env_file option you can specify a file with variables to be injected into the environment in the container.

    Using variable substitution in the docker-compose.yml you can access variables in the environment of the docker-compose command, i.e. on the host. You can set these using the usual mechanisms of your OS/shell, e.g. in bash:

    export XUSER=you
    docker-compose up
    

    Additionally with docker-compose you can use a .env file in the current directory.

    So in your concrete example you should just move env/test.env to .env to add the variables to the environment of docker-compose for variable substitution. If you also want to add them to the environment in the container you can do it like this:

    version: '3'
    services:
      abc:
        build: .
        image: xyz:latest
        container_name: xyz
    
        # add variables from the docker-compose environment to the container:
        environment:
          - XUSER=$XUSER
          # or even shorter:
          - XHOME
    
        # use variable from the docker-compose environment in the config:
        user: "${XUSER}"