docker - 什么时候必须在 Dockerfile 的 CMD 指令中使用 `/bin/sh -c ".. ."` ?

标签 docker dockerfile sh

我一直在研究几个项目的 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/

相关文章:

docker - Docker Volumes 中 -volume 和 -volumes-from 的区别

Azure应用服务容器不断重新启动

r - 使用Dockerfile进行Dockerizing Shiny-app

linux - 如何使用 Bash 读取特定行和特定字段?

ubuntu - Docker 构建 : unable to fetch archives

docker - 两个 docker 容器之间的 rsync

docker - 无法更改 Dockerfile 中 'RUN chmod +x/app-entrypoint.sh' 的权限

ubuntu - 如何使用多个基础镜像构建一个 docker 镜像

linux - Bash:执行前突出显示命令(set -x)

npm - npm 运行脚本中参数的 if-else