您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。