"required variable is missing a value" 的环境变量插值格式无效

Env variable invalid interpolation format for "required variable is missing a value"

我有这个docker-compose.yaml

version: "3.7"
services:
  busybox:
    image: busybox:latest
    entrypoint: ''
    environment:
      - bar=${foo:-baz}
    command: 
      - "/bin/sh" 
      - "-c" 
      - "echo $$bar"

我运行是这样的:

docker-compose up && docker-compose down

输出:

[+] Running 2/2
 ⠿ Network interpolation-poblem_default      Created
 ⠿ Container interpolation-poblem-busybox-1  Created
Attaching to interpolation-poblem-busybox-1
interpolation-poblem-busybox-1  | baz
interpolation-poblem-busybox-1 exited with code 0
[+] Running 2/2
 ⠿ Container interpolation-poblem-busybox-1  Removed
 ⠿ Network interpolation-poblem_default      Removed

它工作正常 - foo 被替换为 baz,由以下回应:/bin/sh -c "echo $bar"

interpolation-poblem-busybox-1  | baz

但是当我将 bar=${foo:-baz} 更改为 bar=${foo:?mandatory}

version: "3.7"
services:
  busybox:
    image: busybox:latest
    entrypoint: ''
    environment:
      - bar=${foo:?mandatory}
    command: 
      - "/bin/sh" 
      - "-c" 
      - "echo $$bar"

又是运行:

docker-compose up && docker-compose down

我得到输出:

invalid interpolation format for services.busybox.environment.[]: "required variable foo is missing a value: mandatory". You may need to escape any $ with another $

错误信息有点混乱,在我看来,mandatory 字符串似乎是文学作品。但即使我 运行:

foo=mandatory docker-compose up && docker-compose down

输出:

[+] Running 1/1
 ⠿ Container interpolation-poblem-busybox-1  Created
Attaching to interpolation-poblem-busybox-1
interpolation-poblem-busybox-1  | mandatory
interpolation-poblem-busybox-1 exited with code 0
invalid interpolation format for services.busybox.environment.[]: "required variable foo is missing a value: mandatory". You may need to escape any $ with another $

注意上面的输出有:

interpolation-poblem-busybox-1  | mandatory

但缺少:

[+] Running 2/2
 ⠿ Container interpolation-poblem-busybox-1  Removed
 ⠿ Network interpolation-poblem_default      Removed

当一切正常时,bar=${foo:-baz} 的示例中出现了这种情况。我看起来命令 docker-compose down 没有做任何工作,实际上 运行ning:

docker ps -a

显示输出:

CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS                     PORTS     NAMES
3b7c0a9caa63   busybox:latest   "/bin/sh -c 'echo $b…"   3 minutes ago   Exited (0) 3 minutes ago             interpolation-poblem-busybox-1

所以容器 interpolation-poblem-busybox-1 没有被删除。

尝试通过以下方式再次将其删除:

docker-compose down

给出相同的输出:

invalid interpolation format for services.busybox.environment.[]: "required variable foo is missing a value: mandatory". You may need to escape any $ with another $

没有移除容器。

连运行宁:

docker-compose ps

生成相同的输出错误。

根据documentation

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.

语法:

    environment:
      - bar=${foo:?mandatory}

似乎是正确的。

主机系统:

docker --version

Docker version 20.10.13, build a224086

Docker Engine release notes v20.10 - however I see that the release on github is v19.03.14

docker-compose --version

Docker Compose version v2.3.3

Docker Compose release notes - There is no version v2.3.3 there, the highest is 1.29.2, but at github releases has v2.3.3

hostnamectl | grep Kernel

Kernel: Linux 4.15.0-171-generic

我想这个问题可能与我最近通过 Ubuntu 提示更新的 Docker version 20.10.13, build a224086 有关,并且没有从选择列表中勾选 Docker 框。

Compatibility matrix

处没有 docker 版本 v20.x

我找不到 docker-compose 命令的类似列表。

然而,我使用的 none 版本被标记为 devalphabetarcnb,所以我希望我应该 运行 相当稳定的版本,但也许我错了。

任何人都可以提供解决方案-如何使用:

    environment:
      - bar=${foo:?mandatory}

没有上述问题?

关于插值的问题没有说明我做错了什么:

docker-composeis:issue is:open invalid interpolation

如果有人可以分享 link 到描述 dockerdocker-compose 发布政策的官方文档,包括他们的路线图和可能的信息,这将很有帮助支持期加上对稳定版本命名约定的澄清。

我只发现this roadmap说的不多

当你运行foo=mandatory docker-compose up && docker-compose down时,你是运行宁foo=mandatory docker-compose up然后docker-compose down,这意味着docker-compose down没有收到那个变量,因此在尝试读取模板时给你那个错误。

如果你 运行 foo=mandatory docker-compose up && foo=mandatory docker-compose down,它会起作用。您也可以导出变量,这样您就不需要传递它两次:

export foo=mandatory
# now foo=mandatory will be passed to all commands after the export
docker-compose up && docker-compose down