Shell 和 Exec 形式的 Dockerfile 指令的区别 [英] Differences Between Dockerfile Instructions in Shell and Exec Form

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

问题描述

shellexec表单有什么区别

CMD:

CMD python my_script.py arg

对比

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

入口点:

ENTRYPOINT ./bin/main

对比

ENTRYPOINT ["./bin/main"]

运行:

RUN npm start

对比

RUN ["npm", "start"]

Dockerfile 指令?

推荐答案

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 数组,这意味着您必须在单词周围使用双引号 () 而不是单引号 (‘).

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 进行变量替换.如果你想要 shell 处理,那么要么使用 shell 形式,要么直接执行 shell,例如: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:

There is also a third form for CMD:

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

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

此外,如果您将 CMD 用作要被覆盖的 ENTRYPOINT 的参数/参数,则需要 exec 形式.

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

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

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