如何在ENTRYPOINT数组中使用Docker环境变量? [英] How do I use Docker environment variable in ENTRYPOINT array?
问题描述
如果我设置一个环境变量,说 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处理。例如,$ code> ENTRYPOINT [echo,$ HOME] 不会在$ HOME上执行变量替换。如果你想要shell处理,那么可以使用 shell窗体或直接执行shell,例如: ENTRYPOINT [sh,-c,echo,$ HOME]
。
当使用exec窗体并直接执行一个shell时,就像shell窗体一样,它是正在进行环境变量扩展的shell,不是码头(从 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屋!