How to pass host's environment variables as args to Docker Compose using a .env file?

Viewed 2758

I'm trying to pass some host's environment variables content as args to the docker-compose file, through a .env file. But the variable is interpreted as a string.

Follows the content of my files:

.env:

USER=$USER
UID=$UID
GID=$GID

docker-compose.yml:

version: "2"

services:
    opencv_python:
        build:
            args:
                - username=${USER}
                - uid=${UID}
                - gid=${GID}
            context: .
            dockerfile: opencv_base.Dockerfile
        container_name: ocv-data-augmentation
        image: ocv-data-augmentation
        environment: 
            DISPLAY: $DISPLAY
            QT_X11_NO_MITSHM: 1
        volumes: 
            - "../project:/home/&{USER}/data_augmentation/" # Host : Container
            - "/tmp/.X11-unix:/tmp/.X11-unix"
        tty: true

And this is the output of the command docker-compose config:

 services:
  opencv_python:
    build:
      args:
        gid: $$GID
        uid: $$UID
        username: fsalvagnini
      context: /home/fsalvagnini/Documents/containers/data_augmentation/dockerfiles
      dockerfile: opencv_base.Dockerfile
    container_name: ocv-data-augmentation
    environment:
      DISPLAY: :1
      QT_X11_NO_MITSHM: 1
    image: ocv-data-augmentation
    tty: true
    volumes:
    - /home/fsalvagnini/Documents/containers/data_augmentation/project:/home/&{USER}/data_augmentation:rw
    - /tmp/.X11-unix:/tmp/.X11-unix:rw
version: '2.0'
2 Answers

If you need to use the .env file and assuming that all the env variables are defined, you just need to follow one step:

.env file

source .env

The above statement will source all the variables defined in .env and hence the env variables will be accessible to docker-compose.

Just added thing, you should also look at ${VARIABLE:-default} just in case you need to pass a default value.

More documentation here

According to the docker-compose manual:

When you set the same environment variable in multiple files, here’s the priority used by Compose to choose which value to use:

  1. Compose file
  2. Shell environment variables
  3. Environment file
  4. Dockerfile
  5. Variable is not defined

So if shell environment variables are not set, then env file will be used.

For your case, if you need to use shell env vars, you don't need to create .env file. To solve your issue, you need to export the variables before invoking docker-compose.

export GID
export UID
export DISPLAY
docker-compose config

output:

services:
  opencv_python:
    build:
      args:
        gid: '20'
        uid: '501'
        username: enix
      context: /Users/enix/source/devops/stackoverflow
      dockerfile: opencv_base.Dockerfile
    container_name: ocv-data-augmentation
    environment:
      DISPLAY: :1
      QT_X11_NO_MITSHM: 1
    image: ocv-data-augmentation
    tty: true
    volumes:
    - /Users/enix/source/devops/project:/home/&{USER}/data_augmentation:rw
    - /tmp/.X11-unix:/tmp/.X11-unix:rw
version: '2.0'
Related