如何在 ENTRYPOINT 数组中使用 Docker 环境变量? [英] How do I use Docker environment variable in ENTRYPOINT array?

查看:132
本文介绍了如何在 ENTRYPOINT 数组中使用 Docker 环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我设置了一个环境变量,比如 ENV ADDRESSEE=world,并且我想在连接成固定字符串的入口点脚本中使用它,例如:

If I set an environment variable, say ENV ADDRESSEE=world, and I want to use it in the entry point script concatenated into a fixed string like:

ENTRYPOINT ["./greeting", "--message", "Hello, world!"]

world 是环境变量的值,我该怎么做?我尝试使用 "Hello, $ADDRESSEE" 但这似乎不起作用,因为它从字面上理解 $ADDRESSEE.

with world being the value of the environment varible, how do I do it? I tried using "Hello, $ADDRESSEE" but that doesn't seem to work, as it takes the $ADDRESSEE literally.

推荐答案

您正在使用 ENTRYPOINT 的 exec 形式.与shell 形式 不同,exec 形式 不调用命令shell.这意味着不会发生正常的 shell 处理.例如,ENTRYPOINT [ "echo", "$HOME" ] 不会对 $HOME 进行变量替换.如果你想要 shell 处理,那么要么使用 shell 形式,要么直接执行 shell,例如:ENTRYPOINT [ "sh", "-c", "echo $HOME" ].
当使用 exec 形式并直接执行 shell 时,就像 shell 形式一样,是 shell 进行环境变量扩展,而不是 docker.(来自 Dockerfile 参考)

You're using the exec form of ENTRYPOINT. Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, ENTRYPOINT [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: ENTRYPOINT [ "sh", "-c", "echo $HOME" ].
When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.(from Dockerfile reference)

在你的情况下,我会使用 shell 形式

In your case, I would use shell form

ENTRYPOINT ./greeting --message "Hello, $ADDRESSEE!"

这篇关于如何在 ENTRYPOINT 数组中使用 Docker 环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆