“所需变量缺少值”的环境变量插值格式无效

Jim*_*mix 4 environment-variables substitution string-interpolation docker docker-compose

我有这个docker-compose.yaml

\n
version: "3.7"\nservices:\n  busybox:\n    image: busybox:latest\n    entrypoint: \'\'\n    environment:\n      - bar=${foo:-baz}\n    command: \n      - "/bin/sh" \n      - "-c" \n      - "echo $$bar"\n
Run Code Online (Sandbox Code Playgroud)\n

我正在这样运行:

\n
docker-compose up && docker-compose down\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
[+] Running 2/2\n \xe2\xa0\xbf Network interpolation-poblem_default      Created\n \xe2\xa0\xbf Container interpolation-poblem-busybox-1  Created\nAttaching to interpolation-poblem-busybox-1\ninterpolation-poblem-busybox-1  | baz\ninterpolation-poblem-busybox-1 exited with code 0\n[+] Running 2/2\n \xe2\xa0\xbf Container interpolation-poblem-busybox-1  Removed\n \xe2\xa0\xbf Network interpolation-poblem_default      Removed\n
Run Code Online (Sandbox Code Playgroud)\n

它工作正常 -foo替换为baz以下内容:/bin/sh -c "echo $bar"

\n
interpolation-poblem-busybox-1  | baz\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
[+] Running 2/2\n \xe2\xa0\xbf Network interpolation-poblem_default      Created\n \xe2\xa0\xbf Container interpolation-poblem-busybox-1  Created\nAttaching to interpolation-poblem-busybox-1\ninterpolation-poblem-busybox-1  | baz\ninterpolation-poblem-busybox-1 exited with code 0\n[+] Running 2/2\n \xe2\xa0\xbf Container interpolation-poblem-busybox-1  Removed\n \xe2\xa0\xbf Network interpolation-poblem_default      Removed\n
Run Code Online (Sandbox Code Playgroud)\n

并再次运行:

\n
interpolation-poblem-busybox-1  | baz\n
Run Code Online (Sandbox Code Playgroud)\n

我得到输出:

\n
\n

services.busybox.environment.[] 的插值格式无效:“必需的变量 foo 缺少值:强制”。您可能需要用另一个 $ 来转义任何 $

\n
\n

错误信息有点令人困惑,在我看来,好像字符串 mandatory是预期的文学内容。但即使我跑:

\n
version: "3.7"\nservices:\n  busybox:\n    image: busybox:latest\n    entrypoint: \'\'\n    environment:\n      - bar=${foo:?mandatory}\n    command: \n      - "/bin/sh" \n      - "-c" \n      - "echo $$bar"\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
[+] Running 1/1\n \xe2\xa0\xbf Container interpolation-poblem-busybox-1  Created\nAttaching to interpolation-poblem-busybox-1\ninterpolation-poblem-busybox-1  | mandatory\ninterpolation-poblem-busybox-1 exited with code 0\ninvalid interpolation format for services.busybox.environment.[]: "required variable foo is missing a value: mandatory". You may need to escape any $ with another $\n
Run Code Online (Sandbox Code Playgroud)\n

请注意上面的输出有:

\n
interpolation-poblem-busybox-1  | mandatory\n
Run Code Online (Sandbox Code Playgroud)\n

但缺少:

\n
[+] Running 2/2\n \xe2\xa0\xbf Container interpolation-poblem-busybox-1  Removed\n \xe2\xa0\xbf Network interpolation-poblem_default      Removed\n
Run Code Online (Sandbox Code Playgroud)\n

这出现在bar=${foo:-baz}一切正常的例子中。我看起来该命令docker-compose down没有执行任何操作,实际上正在运行:

\n
docker-compose up && docker-compose down\n
Run Code Online (Sandbox Code Playgroud)\n

显示输出:

\n
CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS                     PORTS     NAMES\n3b7c0a9caa63   busybox:latest   "/bin/sh -c \'echo $b\xe2\x80\xa6"   3 minutes ago   Exited (0) 3 minutes ago             interpolation-poblem-busybox-1\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

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

\n
foo=mandatory docker-compose up && docker-compose down\n
Run Code Online (Sandbox Code Playgroud)\n

给出相同的输出:

\n
\n

services.busybox.environment.[] 的插值格式无效:“必需的变量 foo 缺少值:强制”。您可能需要用另一个 $ 来转义任何 $

\n
\n

无需移除容器。

\n

甚至跑步:

\n
[+] Running 1/1\n \xe2\xa0\xbf Container interpolation-poblem-busybox-1  Created\nAttaching to interpolation-poblem-busybox-1\ninterpolation-poblem-busybox-1  | mandatory\ninterpolation-poblem-busybox-1 exited with code 0\ninvalid interpolation format for services.busybox.environment.[]: "required variable foo is missing a value: mandatory". You may need to escape any $ with another $\n
Run Code Online (Sandbox Code Playgroud)\n

产生相同的输出错误。

\n

根据文档

\n
\n

同样,以下语法允许您指定强制变量:

\n
    \n
  • ${VARIABLE:?err}退出并显示一条错误消息,其中包含errifVARIABLE未设置或环境中为空。
  • \n
  • ${VARIABLE?err}退出并显示一条错误消息,其中包含errifVARIABLE未在环境中设置。
  • \n
\n
\n

语法:

\n
interpolation-poblem-busybox-1  | mandatory\n
Run Code Online (Sandbox Code Playgroud)\n

似乎是正确的。

\n

主机系统:

\n
[+] Running 2/2\n \xe2\xa0\xbf Container interpolation-poblem-busybox-1  Removed\n \xe2\xa0\xbf Network interpolation-poblem_default      Removed\n
Run Code Online (Sandbox Code Playgroud)\n
\n

Docker 版本 20.10.13,内部版本 a224086

\n
\n

Docker Engine 发行说明 v20.10 - 但是我看到github 上的版本是 v19.03.14

\n
docker ps -a\n
Run Code Online (Sandbox Code Playgroud)\n
\n

Docker Compose 版本 v2.3.3

\n
\n

Docker Compose 发行说明- 那里没有 v2.3.3 版本,最高是1.29.2,但在github 上发布有 v2.3.3

\n
CONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS                     PORTS     NAMES\n3b7c0a9caa63   busybox:latest   "/bin/sh -c \'echo $b\xe2\x80\xa6"   3 minutes ago   Exited (0) 3 minutes ago             interpolation-poblem-busybox-1\n
Run Code Online (Sandbox Code Playgroud)\n
\n

内核:Linux 4.15.0-171-generic

\n
\n

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

\n

兼容性矩阵中没有dockerv20.x 版本

\n

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

\n

然而,我使用的版本都没有标记为dev, alpha, beta, rcnb所以我希望我应该运行相当稳定的版本,但也许我错了。

\n

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

\n
docker-compose down\n
Run Code Online (Sandbox Code Playgroud)\n

没有上述任何问题吗?

\n

有关插值的问题并没有澄清我做错了什么:

\n

docker-compose 是:问题是:打开无效插值

\n

如果有人可以分享一个官方文档的链接,该文档描述了其发布政策dockerdocker-compose包括其路线图和有关其支持期限的可能信息以及对稳定版本命名约定的说明,那将会很有帮助。

\n

我只发现这个路线图并没有说太多。

\n

Leo*_*ino 5

当您运行 时foo=mandatory docker-compose up && docker-compose down,您正在运行foo=mandatory docker-compose upthen 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
Run Code Online (Sandbox Code Playgroud)