Docker-组成变量-替换强制变量

Docker-compose variable-substitution mandatory variables

我有一个 docker-compose 文件,它使用变量替换一些秘密,如果它们没有提供或为空,我想得到一个错误,为此我试过这个:

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}

但是,它给我错误:

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

根据 https://docs.docker.com/compose/compose-file/#variable-substitution 这应该有效,因为它有这个片段:

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.

我的 docker-compose 中也有 version: "3.4",所以这应该不是问题。

已经用 ${MY_VAR?MY_ERROR} 试过了,但也没用。 我什至查看了源代码,但没有找到任何帮助。

编辑:

我尝试制作最小尺寸的复制品:

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}

这取决于您的 docker-compose 版本。

使用 docker-compose 1.17.1 你会得到
ERROR: Invalid interpolation format for "environment" option in service "my-service": ...
如果您使用 ${TEST?"My error message"} 但使用
例如docker-compose 1.29.2 它按预期工作
ERROR: Missing mandatory value for "environment" option interpolating ... in service "my-service": "My error message"