compose: WARNING: The X variable is not set. Defaulting to a blank string.

My environment:

# docker-compose version
docker-compose version 1.8.1, build 878cff1
docker-py version: 1.10.3
CPython version: 2.7.12
OpenSSL version: OpenSSL 1.0.2h  3 May 2016
# 

I’m seeing following WARNING message:

# docker-compose ps
WARNING: The ELK variable is not set. Defaulting to a blank string.
    Name              Command          State                    Ports                   
---------------------------------------------------------------------------------------
nginx_nginx_1   nginx -g daemon off;   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp 
#

Per Environment variables in Compose - Docker:

# grep ELK docker-compose.yaml 
        gelf-address: "udp://${ELK}:12201"
# 
# grep -A1 env_file docker-compose.yaml 
    env_file:
      - docker-compose.env
# grep ELK docker-compose.env 
ELK=172.17.0.2
# 

Please advise.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 52
  • Comments: 28

Commits related to this issue

Most upvoted comments

You need to escape the variable if you want it to be expanded inside the container, using a double-dollar sign ($${ELK}). If however, you want it to be interpreted on your host, the $ELK needs to be defined in your environment or in the .env file. The env_file option defines environment variables that will be available inside the container only.

just for future googlers. my problem was with environment variables not being passed through sudo (e.g. on Ubuntu). Working solution:

sudo -E docker-compose up -d

To reiterate what I wrote earlier in this thread:

  • The environment and env_file sections of the Compose file declare variables that will be available inside the container.
  • Variables written using the $VAR or ${VAR} syntax inside the Compose file are replaced by the value found on the host machine (i.e. the machine you’re executing docker-compose from) at the time of execution. They’re found either in the OS’s environment or the statically named .env file.
  • If variable substitution is not desired, one should use the double-dollar-sign notation ($$VAR or $${VAR}) to escape the sequence.

However @shin- 's comment made it clear to me… the “.env” file’s vars are available at docker-compose stage whereas all other env vars set via env_file or env directive are only available within the container… ok

That feels a lot like a bug. There shouldn’t be “too specific” behavior. It only confuses people.

It’s not intuitive to me that the file specified by the “env_file” directive acts differently than the “.env” file…

However @shin- 's comment made it clear to me… the “.env” file’s vars are available at docker-compose stage whereas all other env vars set via env_file or env directive are only available within the container… ok

Found this issue first in the search for a resolution, and ended up understanding then solving it with the info here: http://staxmanade.com/2016/05/how-to-get-environment-variables-passed-through-docker-compose-to-the-containers/

Specifically; set the variable in ./.env, which makes it available to substitute in docker-compose.yml

EXTERNAL_PORT=8000

then in docker-compose.yml, pass it through to the container with:

environment:
      - EXTERNAL_PORT=${EXTERNAL_PORT}

edit: added ./ to make it clear we are not talking about <file>.env

@oojacoboo $UID is not an environment variable, it’s a shell variable:

$ echo $UID
1000
$ printenv | grep UID  # no output
$

See #2380

@shin- can you explain what I’m missing here?

Variables written using the $VAR or ${VAR} syntax inside the Compose file are replaced by the value found on the host machine (i.e. the machine you’re executing docker-compose from) at the time of execution.

services:
  nginx:
    user: ${UID}

I get this: WARNING: The UID variable is not set. Defaulting to a blank string.

However:

[jacob:.../foo]$ echo $UID
501

My problem was that I tried to execute docker compose from a subdirectory, not a root directory where .env file exists

@a1exus ./docker-compose.env != ./.env

I managed to find a solution! I used the env_file setting itself, and defined the file variables.env within the same line. And it worked like a charm!

Here is how my docker-compose.yml looks like:

services:
  web:
    env_file: variables.env

you can also use .env in root of your project it does same…

Hello, I am problem with set variable in yaml file: docker-compose build master WARNING: The CI_ARTEFACTS_HOSTPATH variable is not set. Defaulting to a blank string. Building master … (cut log)

I am use:

      CI_ARTEFACTS_HOSTPATH: '/ci'
      CI_ARTEFACTS_HOSTPATH: "/ci"
      - CI_ARTEFACTS_HOSTPATH=/ci

docker-compose --version docker-compose version 1.24.0-rc1, build 0f3d4dda

docker-compose.yaml:

version: "3.4"
services:
  master:
    environment:
      CI_ARTEFACTS_HOSTPATH: '/ci'
    build:
      context: master
      network: host
    image: buildbot-master
    command: buildbot start --nodaemon master
    ports:
      - 8010:8010
      - 9989:9989
    volumes:
      - ${CI_ARTEFACTS_HOSTPATH}:/ci/artefacts

This example may help if you would like to provide default value in docker-compose env:

Use "${ENV_VAR:-defaultValue}"

    environment:
      HASURA_GRAPHQL_DATABASE_URL: "${HASURA_GRAPHQL_DATABASE_URL:-postgres://postgres:@postgres:5432/postgres}"

Using .env can override default url.

Using double dollar signs fixed it for me:

web:
  build: .
  environment:
      FOLDER: test
  command: cmake -S ./src -B ./build/$${FOLDER}

It is mentioned in the official documentation here:

You can use a $$ (double-dollar sign) when your configuration needs a literal dollar sign. This also prevents Compose from interpolating a value, so a $$ allows you to refer to environment variables that you don’t want processed by Compose.

web:
  build: .
  command: "$$VAR_NOT_INTERPOLATED_BY_COMPOSE"

If you forget and use a single dollar sign ($), Compose interprets the value as an environment variable and warns you:

The X variable is not set. Defaulting to a blank string.

https://docs.docker.com/compose/environment-variables/

For those who can’t/don’t want to use a ./.env file you can pass in the same env_file: entry as a command line argument and it will work for making the values available at build time:

docker-compose --env-file my/own/specific.env build
docker-compose --env-file my/own/specific.env up
# my/own/specific.env
FOO=bar
# docker-compose.yml

# ...
    build:
      args:
        - FOO=${FOO}
    env_file: my/own/specific.env
# ...

My problem is that I am using white space in .env file You should remove all spaces

VAR = VALUE

->

VAR=VALUE

Per @Joshfindit I’ve tried with .env and everything works like a charm! Thank you!