我有这个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我得到了输出:
Services.busybox.environment的无效插值格式。[]:“必需变量foo缺少值:强制性”。您可能需要使用另一个$来转义任何$。
错误信息有点混乱,在我看来,似乎是预期的文学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没有做任何工作,实际上运行:
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提供相同的输出:
Services.busybox.environment的无效插值格式。[]:“必需变量foo缺少值:强制性”。您可能需要使用另一个$来转义任何$。
而不移除容器。
甚至跑步:
docker-compose ps生成相同的输出错误。
根据文档
类似地,以下语法允许您指定强制变量:
${VARIABLE:?err}未设置或为空,则err将带着包含VARIABLE的错误消息退出。${VARIABLE?err},则err将带包含VARIABLE的错误消息退出。语法:
environment:
- bar=${foo:?mandatory}似乎是对的。
主机系统:
docker --versionDocker版本20.10.13,构建a224086
Docker引擎发布说明v20.10 -但是我看到了在github上发布的版本是v19.03.14
docker-compose --versionDocker撰写版本v2.3.3
码头工人撰写发行说明 -没有版本2.3.3,最高的是1.29.2,但在github版本有v2.3.3
hostnamectl | grep Kernel内核: Linux 4.15.0-171-泛型
我想这个问题可能与我最近通过Ubuntu提示符更新的Docker version 20.10.13, build a224086有关,并且没有从选择列表中删除Docker框。
没有docker版本的v20.x在相容矩阵上
对于docker-compose命令,我找不到类似的列表。
但是,我使用的版本中没有一个是用dev、alpha、beta、rc、nb标记的,所以我希望我应该运行相当稳定的版本,但也许我错了。
谁能提供解决方案-如何使用:
environment:
- bar=${foo:?mandatory}没有任何问题,如上文所述?
关于插值的问题并没有澄清我做错了什么:
docker-compose 是:问题是:打开无效的插值
如果有人能够共享一个官方文档的链接,描述docker和docker-compose的发布策略,包括他们的路线图和关于他们支持期的可能信息,再加上对稳定版本命名约定的澄清,那将是很有帮助的。
我只发现这个路线图说的不多。
发布于 2022-03-12 23:44:15
当您运行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 downhttps://stackoverflow.com/questions/71453411
复制相似问题