Jim*_*mix 4 environment-variables substitution string-interpolation docker docker-compose
我有这个docker-compose.yaml
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"\nRun Code Online (Sandbox Code Playgroud)\n我正在这样运行:
\ndocker-compose up && docker-compose down\nRun 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\nRun Code Online (Sandbox Code Playgroud)\n它工作正常 -foo替换为baz以下内容:/bin/sh -c "echo $bar"
interpolation-poblem-busybox-1 | baz\nRun Code Online (Sandbox Code Playgroud)\n但是当我改变bar=${foo:-baz}为bar=${foo:?mandatory}
[+] 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\nRun Code Online (Sandbox Code Playgroud)\n并再次运行:
\ninterpolation-poblem-busybox-1 | baz\nRun Code Online (Sandbox Code Playgroud)\n我得到输出:
\n\n\nservices.busybox.environment.[] 的插值格式无效:“必需的变量 foo 缺少值:强制”。您可能需要用另一个 $ 来转义任何 $
\n
错误信息有点令人困惑,在我看来,好像字符串 mandatory是预期的文学内容。但即使我跑:
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"\nRun 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 $\nRun Code Online (Sandbox Code Playgroud)\n请注意上面的输出有:
\ninterpolation-poblem-busybox-1 | mandatory\nRun 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\nRun Code Online (Sandbox Code Playgroud)\n这出现在bar=${foo:-baz}一切正常的例子中。我看起来该命令docker-compose down没有执行任何操作,实际上正在运行:
docker-compose up && docker-compose down\nRun Code Online (Sandbox Code Playgroud)\n显示输出:
\nCONTAINER 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\nRun Code Online (Sandbox Code Playgroud)\n所以容器interpolation-poblem-busybox-1没有被移除。
尝试通过以下方式再次将其取下:
\nfoo=mandatory docker-compose up && docker-compose down\nRun Code Online (Sandbox Code Playgroud)\n给出相同的输出:
\n\n\nservices.busybox.environment.[] 的插值格式无效:“必需的变量 foo 缺少值:强制”。您可能需要用另一个 $ 来转义任何 $
\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 $\nRun Code Online (Sandbox Code Playgroud)\n产生相同的输出错误。
\n根据文档
\n\n\n同样,以下语法允许您指定强制变量:
\n\n
\n- \n
${VARIABLE:?err}退出并显示一条错误消息,其中包含errifVARIABLE未设置或环境中为空。- \n
${VARIABLE?err}退出并显示一条错误消息,其中包含errifVARIABLE未在环境中设置。
语法:
\ninterpolation-poblem-busybox-1 | mandatory\nRun 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\nRun Code Online (Sandbox Code Playgroud)\n\n\nDocker 版本 20.10.13,内部版本 a224086
\n
Docker Engine 发行说明 v20.10 - 但是我看到github 上的版本是 v19.03.14
\ndocker ps -a\nRun Code Online (Sandbox Code Playgroud)\n\n\nDocker Compose 版本 v2.3.3
\n
Docker Compose 发行说明- 那里没有 v2.3.3 版本,最高是1.29.2,但在github 上发布有 v2.3.3
\nCONTAINER 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\nRun Code Online (Sandbox Code Playgroud)\n\n\n内核:Linux 4.15.0-171-generic
\n
我想这个问题可能与Docker version 20.10.13, build a224086我最近通过 Ubuntu 提示更新并且没有从选择列表中勾选 Docker 框有关。
兼容性矩阵中没有dockerv20.x 版本
我找不到该docker-compose命令的类似列表。
然而,我使用的版本都没有标记为dev, alpha, beta, rc,nb所以我希望我应该运行相当稳定的版本,但也许我错了。
任何人都可以提供解决方案 - 如何使用:
\ndocker-compose down\nRun Code Online (Sandbox Code Playgroud)\n没有上述任何问题吗?
\n有关插值的问题并没有澄清我做错了什么:
\ndocker-compose 是:问题是:打开无效插值
如果有人可以分享一个官方文档的链接,该文档描述了其发布政策docker并docker-compose包括其路线图和有关其支持期限的可能信息以及对稳定版本命名约定的说明,那将会很有帮助。
我只发现这个路线图并没有说太多。
\n当您运行 时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)
| 归档时间: |
|
| 查看次数: |
3645 次 |
| 最近记录: |