compose: docker-compose does not set environment variables

Description of the issue

When using docker-compose up or docker-compose run -e ENVIRONMENT_VAR=value, environment variables are not being applied either through the -e command line parameters or from the docker-compose.yml file.

The docker-compose.yml file I am using is as follows:

version: '3'
services:
    nginx-reverse-proxy:
        image: nginx
        volumes:
         - C:/Users/<Username>/nginx-reverse-proxy/configdir:/etc/nginx/conf.d/
        ports:
         - "80:80"
        environment:
            NGINX_SERVER_NAME: "test.mysite.org"
            NGINX_REMOTE_URL: "http://remote.mysiteengine.somehost.com:13345"

Context information (for bug reports)

Output of docker-compose version

docker-compose version 1.25.5, build 8a1c60f6

Output of docker version

Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:23:10 2020
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:29:16 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

Output of docker-compose config

WARNING: The NGINX_SERVER_NAME variable is not set. Defaulting to a blank string.
WARNING: The NGINX_REMOTE_URL variable is not set. Defaulting to a blank string.
services:
  nginx-reverse-proxy:
    environment:
      NGINX_SERVER_NAME: "test.mysite.org"
      NGINX_REMOTE_URL: "http://remote.mysiteengine.somehost.com:13345"
    image: nginx
    ports:
    - 80:80/tcp
    volumes:
    - C:\Users\<Username>\nginx-reverse-proxy\configdir:/etc/nginx/conf.d/:rw
version: '3.0'

Steps to reproduce the issue

  1. Create a docker-compose.yml file that sets environment variables
  2. Run docker-compose up

Observed result

Environment Variables are not applied

Expected result

Environment variables should be applied to containers started by docker-compose

Stacktrace / full error message

WARNING: The NGINX_SERVER_NAME variable is not set. Defaulting to a blank string.
WARNING: The NGINX_REMOTE_URL variable is not set. Defaulting to a blank string.
Creating network "nginx-reverse-proxy_default" with the default driver

Additional information

OS Version: Windows 10 Pro v1909, docker-compose install method: Docker Desktop for Windows Installer, followed by manual upgrade of docker-compose (Issue occurred on docker-compose version 1.25.4 which was included with Docker Desktop and with manual upgrade to 1.25.5)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 17
  • Comments: 40 (2 by maintainers)

Most upvoted comments

??? This yaml is broken. Please post the actual yaml you are deploying with.

        volumes:
         - C:/Users/<Username>/nginx-reverse-proxy/configdir:/etc/nginx/conf.d/

I have the same exact bug with the latest version of Docker 20.10 and Docker-compose 1.27 on Mac OS

I have the same problem, using docker compose v2 extremely frustrated! 😦

Any update on this?

I have the exact same bug with Docker version 20.10.11 and Docker-compose 1.29.2 on Mac OS.

Removing and re-building the containers did not help. Doesn’t matter whether I docker-compose up with or without -d flag either; bug remains!

Having similar issue right now Docker version 20.10.14, build a224086

In my case only newly added one environment variable is not set in running container others get set normally.

     environment:
           Api__ConnectionString: ${Api_Docker__ConnectionString}
           Api__Recaptcha__Secret: ${Api_Docker__Recaptcha__Secret}
           ASPNETCORE_URLS: 'https://+;http://+'
           ASPNETCORE_HTTPS_PORT: 10081
           ASPNETCORE_Kestrel__Certificates__Default__Password: ${Api__Kestrel__Certificates__Default__Password}
           ASPNETCORE_Kestrel__Certificates__Default__Path: /https/Api/api.pfx

I run printenv command inside running Linux container and verified that the Api__Recaptcha__Secret variable doesn’t get set.

I had a similar issue, but substituting RUN with CMD for the last command made the environment variables discoverable. This Stackoverflow post helped me with this.

Any update on this? Seems that docker-compose is still not picking up .env files?

ok, so this is an application issue then 😛

docker compose up --build <your-container> fixed the issue for me.

@hholst80 indeed, my mistake.

After reading docker documentation one more time and few additional articles, it’s now obvious for me that for substitution in docker-compose file itself used environment variables of my current shell, and to send environment variables inside docker container I may use either docker run CLI or env_file.

Thanks!

Facing the same issue.

And I found that environment variables are working only from .env file placed in the folder where the docker-compose command is executed (current working directory).

Providing environment variables from command line giving the same warnings as @Kytech mentioned.

$ docker-compose run -e PRODUCT=1 all
WARNING: The PRODUCT variable is not set. Defaulting to a blank string.

Providing environment variables from user-specific docker-compose.env file still resulting with the same warnings

$ cat docker-compose.env 
PRODUCT=1
RELEASE=0
FLASH=0
DEBUG=3

$ cat docker-compose.yml 
version: '3'
services:
  all:
    image: 82100_src:latest
    env_file: ./docker-compose.env
    volumes:
      - .:/home/user
    command: make PRODUCT=${PRODUCT} all RELEASE=${RELEASE} FLASH=${FLASH} DEBUG=${DEBUG}

$ docker-compose run all
WARNING: The PRODUCT variable is not set. Defaulting to a blank string.
WARNING: The RELEASE variable is not set. Defaulting to a blank string.
WARNING: The FLASH variable is not set. Defaulting to a blank string.
WARNING: The DEBUG variable is not set. Defaulting to a blank string.

I’m using Linux Debian 10 with next versions of Docker and Docker-compose:

$ docker-compose version
docker-compose version 1.23.2, build 1110ad01
docker-py version: 3.6.0
CPython version: 3.6.7
OpenSSL version: OpenSSL 1.1.0f  25 May 2017

$ docker version
Client: Docker Engine - Community
 Version:           19.03.9
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        9d988398e7
 Built:             Fri May 15 00:25:25 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.9
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       9d988398e7
  Built:            Fri May 15 00:23:57 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683