gpt4 book ai didi

docker - Shell 和 Exec 形式的 Dockerfile 指令的区别

转载 作者:IT老高 更新时间:2023-10-28 12:41:51 25 4
gpt4 key购买 nike

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 形式有两个区别。根据the documentation , exec 形式是首选形式。这是两个区别:

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

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.

这里还有一些细微之处:

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.

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

CMD 还有第三种形式:

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

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

关于docker - Shell 和 Exec 形式的 Dockerfile 指令的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42805750/

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