Search code examples
dockerdocker-composeenvironment-variablesyamlvariable-substitution

Docker-compose variable-substitution mandatory variables


I have a docker-compose file that uses variable substitution for some secrets and I want to get an error if they are not supplied or empty, for this purpose I have tried this:

environment:
  - >-
    JAVA_OPTS=
    -DMYSQL_USER=${MYSQL_USER:?MYSQL_USER_NOT_SET}
    -DMYSQL_PASSWORD=${MYSQL_PASSWORD:?MYSQL_PASSWORD_NOT_SET}
    -DMYSQL_URL=db:3306/${MYSQL_DATABASE:?MYSQL_DATABASE_NOT_SET}

However, it gives me the error:

ERROR: Invalid interpolation format for "environment" option in service "myservice": "JAVA_OPTS= -DMYSQL_USER=${MYSQL_USER:?MYSQL_USER_NOT_SET}...

According to https://docs.docker.com/compose/compose-file/#variable-substitution this should work since it has this snippet:

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.

I also have version: "3.4" in my docker-compose so that shouldn't be the issue.

Already tried it with just ${MY_VAR?MY_ERROR} but it didn't work either. I have even gone as far as to look at the source code but found nothing helpful.

EDIT :

I tried to make a minimum size reproduction:

docker-compose.yml

version: "3.4"

services:
  hello:
    image: hello-world
    environment:
      - TEST=${TEST?err}

docker-compose up

ERROR: Invalid interpolation format for "environment" option in service "hello": "TEST=${TEST?err}


Solution

  • This depends on your docker-compose version.

    With docker-compose 1.17.1 you will get
    ERROR: Invalid interpolation format for "environment" option in service "my-service": ...
    if you use ${TEST?"My error message"} but with
    e.g. docker-compose 1.29.2 it works as expected
    ERROR: Missing mandatory value for "environment" option interpolating ... in service "my-service": "My error message"