Dockerfile CMD Shell与exec形式 [英] Dockerfile CMD shell versus exec form

查看:654
本文介绍了Dockerfile CMD Shell与exec形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

docker RUN和CMD语句的shell形式和exec形式之间有什么区别。

What's the difference between the shell form and exec form of docker RUN and CMD statements.

例如:

RUN [ "npm", "start" ]

vs:

RUN npm start 

eg2

CMD ["python","my_script.py","argument"]

vs:

CMD python my_script.py argument 


推荐答案

shell窗体和exec窗体之间有两个区别。根据文档,exec表单是首选形式。这是两个区别:

There are two differences between the shell form and the exec form. According to the documentation, the exec form is the preferred form. These are the two differences:


exec表单被解析为JSON数组,这意味着您必须在非单字周围使用双引号() -quotes(')。

The exec form is parsed as a JSON array, which means that you must use double-quotes (") around words not single-quotes (‘).

与shell表单不同,exec表单不会调用命令shell。这意味着正常的外壳处理不会发生。例如,CMD [ echo, $ HOME ]不会在$ HOME上进行变量替换。如果要外壳处理,则可以使用外壳形式,也可以直接执行外壳,例如:CMD [ sh,-c, echo $ HOME)。 ]。在使用exec表单并直接执行shell时(例如在shell表单中),是由shell进行环境变量扩展,而不是docker。

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.

这里还有其他一些细微之处:

Some additional subtleties here are:


exec形式可以避免shell字符串混乱,并可以使用执行以下操作的基本映像运行命令不包含指定的shell可执行文件。

The exec form makes it possible to avoid shell string munging, and to RUN commands using a base image that does not contain the specified shell executable.

在shell形式中,您可以使用\(反斜杠)将一条RUN指令继续到下一行。

In the shell form you can use a \ (backslash) to continue a single RUN instruction onto the next line.

对于 CMD


CMD [ param1, param2](默认ENTRYPOINT的参数)

CMD ["param1","param2"] (as default parameters to ENTRYPOINT)

此外,如果使用 CMD ,则需要exec表单作为 ENTRYPOINT 的参数/自变量被覆盖。

Additionally, the exec form is required for CMD if you are using it as parameters/arguments to ENTRYPOINT that are intended to be overwritten.

这篇关于Dockerfile CMD Shell与exec形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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