Marking Environment Variables as Required in Docker Compose#

Suppose you have a docker-compose.yml for a simple app that needs an admin password:

version: '3.7'
services:
  app:
    image: tenzer/http-echo-test
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${ADMIN_PASSWORD}

Unfortunately Docker Compose only warns if it is missing:

$ docker-compose up -d
WARN[0000] The "ADMIN_PASSWORD" variable is not set. Defaulting to a blank string.

If you want Docker Compose to fail if the password is missing, use the following syntax (note the question mark in ADMIN_PASSWORD?):

version: '3.7'
services:
  app:
    image: tenzer/http-echo-test
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=${ADMIN_PASSWORD?}

Now Docker Compose fails if ADMIN_PASSWORD is not set:

$ docker-compose up -d
parsing /tmp/x/docker-compose.yml: error while interpolating services.app.environment.[]: required variable ADMIN_PASSWORD is missing a value

From the docs:

Similarly, the following syntax allows you to specify mandatory variables:

  • ${VARIABLE:?err} exits with an error message containing err if VARIABLE is unset or empty in the environment.

  • ${VARIABLE?err} exits with an error message containing err if VARIABLE is unset in the environment.