CMD/ENTRYPOINT都是指定一个容器启动时要运行的命令。但是CMD会覆盖前面的参数,而ENTRYPONT会追加组合原来的参数。
Dockfile中可以有多个CMD指令,但只有最后一个生效。
另外,CMD会被docker run之后的参数替换,举例说明:
[root@t-docker chenzx]# docker run -it -p 8080:8080 tomcat 01-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] ms 01-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 -l total 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.txt drwxr-x--- 2 root root 313 Aug 23 00:47 bin drwx--S--- 2 root root 238 Aug 12 08:23 conf drwxr-sr-x 3 root staff 19 Aug 23 00:47 include drwxr-x--- 2 root root 4096 Aug 23 00:47 lib drwxr-x--- 2 root root 6 Aug 12 08:20 logs drwxr-sr-x 3 root staff 151 Aug 23 00:47 native-jni-lib drwxr-x--- 2 root root 30 Aug 23 00:47 temp drwxr-x--- 7 root root 81 Aug 12 08:21 webapps drwxr-x--- 2 root root 6 Aug 12 08:20 work
通过上面可以看出,最后面的ls -l覆盖了启动tomcat的操作。这就是CMD会被docker run之后的参数替换。
docker run之后的参数会被当做参数传递给ENTRYPOINT,之后形成新的命令组合。
[root@t-docker CMD]# cat Dockerfile from centos RUN yum -y install curl CMD ["curl","-s","http://ip.cn"] [root@t-docker CMD]# docker build -f Dockerfile -t myip . Sending build context to Docker daemon 2.048kB Step 1/3 : from centos ---> 5182e96772bf Step 2/3 : RUN yum -y install curl ---> Running in aa5e5f0a8597 Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: mirrors.huaweicloud.com * extras: mirrors.huaweicloud.com * updates: mirrors.neusoft.edu.cn Package curl-7.29.0-46.el7.x86_64 already installed and latest version Nothing to do Removing intermediate container aa5e5f0a8597 ---> a35f6ad9700f Step 3/3 : CMD ["curl","-s","http://ip.cn"] ---> Running in f59369e2a7db Removing intermediate container f59369e2a7db ---> ecf0b349d6a1 Successfully built ecf0b349d6a1 Successfully tagged myip:latest
[root@t-docker CMD]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE myip latest ecf0b349d6a1 About a minute ago 293MB mycentos 1.3 0c868c56748b 3 hours ago 442MB chenzx/centos latest a37b96c1c7a7 35 hours ago 200MB chenzx/tomcat02 1.2 24c3d89d4d04 2 days ago 463MB tomcat latest 690cb3b9c7d1 9 days ago 463MB centos 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.cn HTTP/1.1 200 OK Date: Sat, 01 Sep 2018 13:19:24 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Set-Cookie: __cfduid=defee6872152b4b29d3a55de1eb3f219d1535807964; expires=Sun, 01-Sep-19 13:19:24 GMT; path=/; domain=.ip.cn; HttpOnly Server: cloudflare CF-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 -i HTTP/1.1 200 OK Date: Sat, 01 Sep 2018 13:28:03 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Set-Cookie: __cfduid=d8b76a4d8c8ebb4bf69f97a92cc39216e1535808483; expires=Sun, 01-Sep-19 13:28:03 GMT; path=/; domain=.ip.cn; HttpOnly Server: cloudflare CF-RAY: 453813eed351b11c-HKG 当前 IP:223.72.205.1 来自:北京市 移动
[root@t-docker CMD]# cat Dockerfile2 from centos RUN yum -y install curl ENTRYPOINT ["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.072kB Step 1/3 : from centos ---> 5182e96772bf Step 2/3 : RUN yum -y install curl ---> Using cache ---> a35f6ad9700f Step 3/3 : ENTRYPOINT ["curl","-s","http://ip.cn"] ---> Running in 36f7f6639933 Removing intermediate container 36f7f6639933 ---> b772af142e86 Successfully built b772af142e86 Successfully 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 OK Date: Sat, 01 Sep 2018 13:57:37 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Set-Cookie: __cfduid=ded1abdaf2b8375c2fb815f8d262e4c781535810257; expires=Sun, 01-Sep-19 13:57:37 GMT; path=/; domain=.ip.cn; HttpOnly Server: cloudflare CF-RAY: 45383f3c074bb20c-HKG 当前 IP:223.72.205.1 来自:北京市 移动
可见,ENTRYPOINT没有覆盖原参数,而是追加到原参数的后面。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。