gpt4 book ai didi

docker - 何时必须在Dockerfile的CMD指令中使用 `/bin/sh -c “…”`?

转载 作者:行者123 更新时间:2023-12-02 00:51:04 24 4
gpt4 key购买 nike

我一直在研究几个项目的Dockerfile实现,我注意到其中一些以以下方式启动应用程序:

CMD executable

而其他人则这样做:
CMD /bin/sh -c "executable"

我已经为每个项目尝试了两个版本,但并没有真正注意到它们之间的差异。

有区别吗?如果是这样,什么时候首选哪个版本?

最佳答案

从Docker文档(https://docs.docker.com/engine/reference/builder/):

  • CMD ["executable","param1","param2"] (exec form, this is the preferred form)
  • CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
  • CMD command param1 param2 (shell form)


...

Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "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: CMD [ "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.



所以
CMD executable

使用shell形式的容器已经隐式调用了shell。因此,没有理由显式调用一个。

关于docker - 何时必须在Dockerfile的CMD指令中使用 `/bin/sh -c “…”`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57608145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com