本文主要是介绍理解CMD 和 ENTRYPOINT 是如何互相作用的,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
官方文档: Dockerfile reference - Understand how CMD and ENTRYPOINT interact
目录
- Understand how CMD and ENTRYPOINT interact
- 官方文档原文
- Docker run 覆写设置说明
- 准备验证镜像 nginx:latest
- 情景1: ENTRYPOINT 与 CMD均使用默认值
- 情景2: ENTRYPOINT 与 CMD同时空值
- 情景3: ENTRYPOINT有值 与 CMD为空值
- 情景4: ENTRYPOINT 与 CMD 同时有值
- 情景5: ENTRYPOINT为空值 与 CMD 有值
- 情景6: ENTRYPOINT默认值 与 CMD 有值
Understand how CMD and ENTRYPOINT interact
理解CMD 和 ENTRYPOINT 是如何互相作用的
官方文档原文
Both CMD and ENTRYPOINT instructions define what command gets executed when running a container. There are few rules that describe their co-operation.
CMD和ENTRYPOINT指令都定义了运行容器时执行的命令。几乎没什么规则可以描述他们的合作关系。
-
Dockerfile should specify at least one of CMD or ENTRYPOINT commands.
-
ENTRYPOINT should be defined when using the container as an executable.
-
CMD should be used as a way of defining default arguments for an ENTRYPOINT command or for executing an ad-hoc command in a container.
-
CMD will be overridden when running the container with alternative arguments.
1. Dockerfile要求 CMD 与 ENTRYPOINT 两个命令中至少声明一个。
2.当容器是可执行时,应该定义 ENTRYPOINT。
3.CMD可被定义作为ENTRYPOINT命令的缺省参数或者在容器中执行的一个临时命令。
4.C当使用替代参数运行容器时,CMD将被覆盖
The table below shows what command is executed for different ENTRYPOINT / CMD combinations:
下表显示了针对不同ENTRYPOINT/CMD组合执行的命令:
说明:上表中定义方式参见官方文档:Dockerfile reference - Shell and exec form
Note
If CMD is defined from the base image, setting ENTRYPOINT will reset CMD to an empty value. In this scenario, CMD must be defined in the current image to have a value.
如果基础镜像中定义了 CMD,设置了 ENTRYPOINT,会把 CMD 重置成空值。这种情况下,CMD必须在当前镜像定义一个值:
Docker run 覆写设置说明
上表中是个矩阵表,从两个维度组合CMD 和 ENTRYPOINT的命令。对应上述4条规则,验证docker run命令下使用情况。
官方文档: Running containers - Overriding image defaults
准备验证镜像 nginx:latest
- 查看官方nginx:latest镜像的ENTRYPOINT 和 CMD 参数信息
- “Entrypoint”: [
“/docker-entrypoint.sh”
], - “Cmd”: [ “/bin/sh”,
“-c”,
"#(nop) ",
“CMD [“nginx” “-g” “daemon off;”]”
],
- “Entrypoint”: [
// nginx镜像已提前拉到本地
[root@centos7-10 ~]# docker image inspect nginx:latest
[{"Id": "sha256:605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85","RepoTags": ["nginx:latest"],"RepoDigests": ["nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31"],"Parent": "","Comment": "","Created": "2021-12-29T19:28:29.892199479Z","Container": "ca3e48389f7160bc9d9a892d316fcbba459344ee3679998739b1c3cd8e56f7da","ContainerConfig": {
......"Cmd": ["/bin/sh","-c","#(nop) ","CMD [\"nginx\" \"-g\" \"daemon off;\"]"],"Image": "sha256:82941edee2f4d17c55563bb926387c3ae39fa1a99777f088bc9d3db885192209","Volumes": null,"WorkingDir": "","Entrypoint": ["/docker-entrypoint.sh"],......}}
]
情景1: ENTRYPOINT 与 CMD均使用默认值
- 运行一个容器,ENTRYPOINT 和 CMD 均使用默认值
- docker run运行nginx:latest,
- 使用镜像 ENTRYPOINT 和 CMD 的默认值
- 查看容器的COMMAND,组合ENTRYPOINT 与 CMD 的默认值: “/docker-entrypoint.sh nginx -g ‘daemon off;’” ,CMD值作为ENTRYPOINT的命令参数。
[root@centos7-10 ~]# docker run -itd --rm nginx:latest
514842c73f365f38f803252b9ee6b92d0665d7448c5a782dab7d1002671997b7
[root@centos7-10 ~]# docker ps -a --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
514842c73f365f38f803252b9ee6b92d0665d7448c5a782dab7d1002671997b7 nginx:latest "/docker-entrypoint.sh nginx -g 'daemon off;'" 28 seconds ago Up 26 seconds 80/tcp stupefied_booth
情景2: ENTRYPOINT 与 CMD同时空值
- 运行一个容器,ENTRYPOINT 和 CMD 同时空值的情况
- docker run运行nginx:latest,
- 覆写设置Entrypoint值为空。该设置操作会清空镜像默认CMD的值
- CMD为空值
- 容器运行禁止ENTRYPOINT 与 CMD 同时为空值,给出错误提示!
[root@centos7-10 ~]# docker run -it --entrypoint="" nginx:latest
docker: Error response from daemon: No command specified.
See 'docker run --help'.
情景3: ENTRYPOINT有值 与 CMD为空值
- 运行一个容器,ENTRYPOINT有值,CMD 为空值的情况
- docker run运行nginx:latest,
- 覆写设置Entrypoint=/bin/bash。该设置操作会清空镜像默认CMD的值
- CMD为空值
- 查看容器的COMMAND,显示ENTRYPOINT的值:“/bin/bash”
// 运行容器
[root@centos7-10 ~]# docker run -it --entrypoint /bin/bash nginx:latest
root@d8f22cc19655:/#
// 容器的COMMAND,显示 /bin/bash
[root@centos7-10 ~]# docker ps -a --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d8f22cc19655647798a7578dc9d07fd928388e3f575e18b0c4864d747f68c67d nginx:latest "/bin/bash" 36 seconds ago Up 35 seconds 80/tcp wonderful_shaw
[root@centos7-10 ~]#
情景4: ENTRYPOINT 与 CMD 同时有值
- 运行一个容器,ENTRYPOINT 与 CMD 均有值的情况
- docker run运行nginx:latest,
- 覆写设置Entrypoint=/bin/bash。该设置操作会清空镜像默认CMD的值
- 覆写设置CMD=“-c ls -a”
- 查看容器的COMMAND,组合ENTRYPOINT 与 CMD 的值: “/bin/bash -c ls -a” ,CMD值作为ENTRYPOINT的命令参数。
// 运行容器,--entrypoint /bin/bash,CMD -c ls -a
[root@centos7-10 ~]# docker run -it --entrypoint /bin/bash nginx:latest -c ls -a
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
// COMMAND /bin/bash -c ls -a
[root@centos7-10 ~]# docker ps -a --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c3a03c567fac4133b803d66e330adb7bea31bdfdc1153eb8b9d0d5b2cbf6c15d nginx:latest "/bin/bash -c ls -a" 10 seconds ago Exited (0) 9 seconds ago clever_wilson
情景5: ENTRYPOINT为空值 与 CMD 有值
- 运行一个容器,ENTRYPOINT为空值 与 CMD 有值的情况
- docker run运行nginx:latest,
- 覆写设置Entrypoint=“”。该设置操作会清空镜像默认CMD的值
- 覆写设置CMD=“/bin/bash -c ls -a”
- 查看容器的COMMAND,显示CMD的值: “/bin/bash -c ls -a” 。
// 运行容器,Entrypoint="",CMD /bin/bash -c ls -a
[root@centos7-10 ~]# docker run -it --entrypoint="" nginx:latest /bin/bash -c ls -a
bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
// COMMAND /bin/bash -c ls -a
[root@centos7-10 ~]# docker ps -a --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0261ca0bf5b1d802092da12f0d9339300e47e7cffd24b6e29b724a510adaec10 nginx:latest "/bin/bash -c ls -a" About a minute ago Exited (0) About a minute ago hopeful_hofstadter
情景6: ENTRYPOINT默认值 与 CMD 有值
- 运行一个容器,ENTRYPOINT使用默认值 与 CMD 有值的情况
- docker run运行nginx:latest,
- 忽略设置Entrypoint,使用默认值为 “/docker-entrypoint.sh”
- 覆写设置CMD="nginx -v "
- 查看容器的COMMAND,组合ENTRYPOINT 与 CMD 的值: ** “/docker-entrypoint.sh nginx -v”** ,CMD值作为ENTRYPOINT的命令参数。
// 运行容器,CMD nginx -v
[root@centos7-10 ~]# docker run -itd nginx:latest nginx -v
94498d179a78b70ba8375b9a5348a7f68b3071991e418fc36f2eb3d0517e61c0
// COMMAND "/docker-entrypoint.sh nginx -v"
[root@centos7-10 ~]# docker ps -a --no-trunc
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
94498d179a78b70ba8375b9a5348a7f68b3071991e418fc36f2eb3d0517e61c0 nginx:latest "/docker-entrypoint.sh nginx -v" 39 seconds ago Exited (0) 38 seconds ago kind_leavitt
这篇关于理解CMD 和 ENTRYPOINT 是如何互相作用的的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!