CMD/ENTRYPOINT都是指定一个容器启动时要运行的命令。但是CMD会覆盖前面的参数,而ENTRYPONT会追加组合原来的参数。
CMD
Dockfile中可以有多个CMD指令,但只有最后一个生效。
另外,CMD会被docker run之后的参数替换,举例说明:
[root@t-docker chenzx]# docker run -it -p 8080:8080 tomcat01-Sep-2018 12:52:39.808 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/manager]01-Sep-2018 12:52:39.822 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [14] ms01-Sep-2018 12:52:39.827 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]01-Sep-2018 12:52:39.834 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]01-Sep-2018 12:52:39.836 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 510 ms
[root@t-docker chenzx]# docker run -it -p 8080:8080 tomcat ls -ltotal 120-rw-r----- 1 root root 19533 Aug 12 08:23 BUILDING.txt-rw-r----- 1 root root 6090 Aug 12 08:23 CONTRIBUTING.md-rw-r----- 1 root root 57092 Aug 12 08:23 LICENSE-rw-r----- 1 root root 1726 Aug 12 08:23 NOTICE-rw-r----- 1 root root 3255 Aug 12 08:23 README.md-rw-r----- 1 root root 7140 Aug 12 08:23 RELEASE-NOTES-rw-r----- 1 root root 16262 Aug 12 08:23 RUNNING.txtdrwxr-x--- 2 root root 313 Aug 23 00:47 bindrwx--S--- 2 root root 238 Aug 12 08:23 confdrwxr-sr-x 3 root staff 19 Aug 23 00:47 includedrwxr-x--- 2 root root 4096 Aug 23 00:47 libdrwxr-x--- 2 root root 6 Aug 12 08:20 logsdrwxr-sr-x 3 root staff 151 Aug 23 00:47 native-jni-libdrwxr-x--- 2 root root 30 Aug 23 00:47 tempdrwxr-x--- 7 root root 81 Aug 12 08:21 webappsdrwxr-x--- 2 root root 6 Aug 12 08:20 work
通过上面可以看出,最后面的ls -l覆盖了启动tomcat的操作。这就是CMD会被docker run之后的参数替换。
ENTRYPOINT
docker run之后的参数会被当做参数传递给ENTRYPOINT,之后形成新的命令组合。
案例1:制作CMD版可以查询IP信息的容器
[root@t-docker CMD]# cat Dockerfile from centosRUN yum -y install curlCMD ["curl","-s","http://ip.cn"][root@t-docker CMD]# docker build -f Dockerfile -t myip .Sending build context to Docker daemon 2.048kBStep 1/3 : from centos ---> 5182e96772bfStep 2/3 : RUN yum -y install curl ---> Running in aa5e5f0a8597Loaded plugins: fastestmirror, ovlDetermining fastest mirrors * base: mirrors.huaweicloud.com * extras: mirrors.huaweicloud.com * updates: mirrors.neusoft.edu.cnPackage curl-7.29.0-46.el7.x86_64 already installed and latest versionNothing to doRemoving intermediate container aa5e5f0a8597 ---> a35f6ad9700fStep 3/3 : CMD ["curl","-s","http://ip.cn"] ---> Running in f59369e2a7dbRemoving intermediate container f59369e2a7db ---> ecf0b349d6a1Successfully built ecf0b349d6a1Successfully tagged myip:latest
[root@t-docker CMD]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmyip latest ecf0b349d6a1 About a minute ago 293MBmycentos 1.3 0c868c56748b 3 hours ago 442MBchenzx/centos latest a37b96c1c7a7 35 hours ago 200MBchenzx/tomcat02 1.2 24c3d89d4d04 2 days ago 463MBtomcat latest 690cb3b9c7d1 9 days ago 463MBcentos latest 5182e96772bf 3 weeks ago 200MB[root@t-docker CMD]# [root@t-docker CMD]# docker run -it myip当前 IP:223.72.205.1 来自:北京市 移动
[root@t-docker CMD]# curl -s http://ip.cn当前 IP:223.72.205.1 来自:北京市 移动[root@t-docker CMD]# [root@t-docker CMD]# curl -s -i http://ip.cnHTTP/1.1 200 OKDate: Sat, 01 Sep 2018 13:19:24 GMTContent-Type: text/html; charset=UTF-8Transfer-Encoding: chunkedConnection: keep-aliveSet-Cookie: __cfduid=defee6872152b4b29d3a55de1eb3f219d1535807964; expires=Sun, 01-Sep-19 13:19:24 GMT; path=/; domain=.ip.cn; HttpOnlyServer: cloudflareCF-RAY: 45380742316fb236-HKG当前 IP:223.72.205.1 来自:北京市 移动
[root@t-docker CMD]# docker run myip当前 IP:223.72.205.1 来自:北京市 移动 [root@t-docker CMD]# docker run myip -i #可以看到报错了docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"-i\": executable file not found in $PATH": unknown.ERRO[0000] error waiting for container: context canceled
我们可以看到出现了一个可执行文件找不到的报错: executable file not found in $PATH。之前我们说过,跟在镜像名后面的是command,运行时会替换CMD的默认值。
因此,这里面的-i替换了原来的CMD参数,而不是在原来的curl -s http://ip.cn后面。而-i根本就不是命令,所以自然找不到。
那我们如果希望加入-i参数,我们就必须重新完整的输入这个命令:
[root@t-docker CMD]# docker run myip curl -s http://ip.cn -iHTTP/1.1 200 OKDate: Sat, 01 Sep 2018 13:28:03 GMTContent-Type: text/html; charset=UTF-8Transfer-Encoding: chunkedConnection: keep-aliveSet-Cookie: __cfduid=d8b76a4d8c8ebb4bf69f97a92cc39216e1535808483; expires=Sun, 01-Sep-19 13:28:03 GMT; path=/; domain=.ip.cn; HttpOnlyServer: cloudflareCF-RAY: 453813eed351b11c-HKG当前 IP:223.72.205.1 来自:北京市 移动
案例2:制作ENTRYPOINT版查询ip信息的容器
[root@t-docker CMD]# cat Dockerfile2 from centosRUN yum -y install curlENTRYPOINT ["curl","-s","http://ip.cn"][root@t-docker CMD]#
[root@t-docker CMD]# docker build -f Dockerfile2 -t myip2 .Sending build context to Docker daemon 3.072kBStep 1/3 : from centos ---> 5182e96772bfStep 2/3 : RUN yum -y install curl ---> Using cache ---> a35f6ad9700fStep 3/3 : ENTRYPOINT ["curl","-s","http://ip.cn"] ---> Running in 36f7f6639933Removing intermediate container 36f7f6639933 ---> b772af142e86Successfully built b772af142e86Successfully tagged myip2:latest
[root@t-docker CMD]# docker run -it myip2当前 IP:223.72.205.1 来自:北京市 移动[root@t-docker CMD]# docker run -it myip2 -i ##正确执行HTTP/1.1 200 OKDate: Sat, 01 Sep 2018 13:57:37 GMTContent-Type: text/html; charset=UTF-8Transfer-Encoding: chunkedConnection: keep-aliveSet-Cookie: __cfduid=ded1abdaf2b8375c2fb815f8d262e4c781535810257; expires=Sun, 01-Sep-19 13:57:37 GMT; path=/; domain=.ip.cn; HttpOnlyServer: cloudflareCF-RAY: 45383f3c074bb20c-HKG当前 IP:223.72.205.1 来自:北京市 移动
可见,ENTRYPOINT没有覆盖原参数,而是追加到原参数的后面。