docker筆記15-Dockerfile案例-CMD、ENTRYPOINT案例
CMD/ENTRYPOINT都是指定一個容器啟動時要執行的命令。但是CMD會覆蓋前面的引數,而ENTRYPONT會追加組合原來的引數。
CMD
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之後的引數替換。
ENTRYPOINT
docker run之後的引數會被當做引數傳遞給ENTRYPOINT,之後形成新的命令組合。
案例1:製作CMD版可以查詢IP資訊的容器
[root@t-docker CMD]# cat Dockerfile from centos RUN yum -y install curl CMD ["curl","-s","] [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","] ---> 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 當前 IP:223.72.205.1 來自:北京市 移動 [root@t-docker CMD]# [root@t-docker CMD]# curl -s -i 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
那我們如果希望加入-i引數,我們就必須重新完整的輸入這個命令:
[root@t-docker CMD]# docker run myip curl -s -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 來自:北京市 移動
案例2:製作ENTRYPOINT版查詢ip資訊的容器
[root@t-docker CMD]# cat Dockerfile2 from centos RUN yum -y install curl ENTRYPOINT ["curl","-s","] [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","] ---> 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沒有覆蓋原引數,而是追加到原引數的後面。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28916011/viewspace-2213396/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- docker筆記16-Dockerfile案例-ONBUILD命令案例Docker筆記UI
- CMD和ENTRYPOINT命令
- docker筆記18-案例-安裝mysqlDocker筆記MySql
- docker筆記14-Dockerfile案例-自定義mycentosDocker筆記CentOS
- docker筆記17-Dockerfile案例-自定義tomcat映象Docker筆記Tomcat
- Dockerfile之CMD與ENTRYPOINT使用要點Docker
- dockerfile中ENTRYPOINT與CMD的結合Docker
- Dockerfile 中 RUN, CMD, ENTRYPOINT 的區別Docker
- Docker Compose 案例Docker
- 『現學現忘』Docker基礎 — 36、CMD指令和ENTRYPOINT指令的區別Docker
- 映象的製作CMD與ENTRYPOINT區別
- 017、RUN、CMD、ENTRYPOINT (2019-01-08 週二)
- [轉帖]Dockerfile中CMD與ENTRYPOINT命令的區別Docker
- 《網路爬蟲開發實戰案例》筆記爬蟲筆記
- 《大型網站技術架構:核心原理與案例分析》讀書筆記 - 第3篇 案例網站架構筆記
- 【PB案例學習筆記】-04檔案瀏覽器筆記瀏覽器
- 【PB案例學習筆記】-05 圖片瀏覽器筆記瀏覽器
- 【PB案例學習筆記】-02 目錄瀏覽器筆記瀏覽器
- Wireshark學習筆記(一)常用功能案例和技巧筆記
- 《python專案開發案例集錦》讀書筆記Python筆記
- docker 筆記Docker筆記
- Docker筆記Docker筆記
- 筆記:Docker筆記Docker
- Ansibleyaml的語法、playbook檔案例子(學習筆記十七)YAML筆記
- docker和docker compose安裝使用、入門進階案例Docker
- SpringMVC學習筆記---依賴配置和簡單案例實現SpringMVC筆記
- HTML5與CSS3權威指南筆記案例1HTMLCSSS3筆記
- 白日夢的Elasticsearch實戰筆記,32個查詢案例、15個聚合案例、7個查詢優化技巧。Elasticsearch筆記優化
- Docker Stack 筆記Docker筆記
- docker筆記(一)Docker筆記
- docker使用筆記Docker筆記
- docker 筆記2Docker筆記
- docker 筆記4Docker筆記
- python爬蟲學習筆記 4.2 (Scrapy入門案例(建立專案))Python爬蟲筆記
- 案例
- Docker學習筆記Docker筆記
- Docker 學習筆記Docker筆記
- docker簡單筆記Docker筆記