docker笔记15-Dockerfile案例-CMD、ENTRYPOINT案例
CMD/ENTRYPOINT都是指定一个容器启动时要运行的命令。但是CMD会覆盖前面的参数,而ENTRYPONT会追加组合原来的参数。
Dockfile中可以有多个CMD指令,但只有最后一个生效。
另外,CMD会被docker run之后的参数替换,举例说明:
[root@t-dockerchenzx]#dockerrun-it-p8080:8080tomcat01-Sep-201812:52:39.808INFO[localhost-startStop-1]org.apache.catalina.startup.HostConfig.deployDirectoryDeployingwebapplicationdirectory[/usr/local/tomcat/webapps/manager]01-Sep-201812:52:39.822INFO[localhost-startStop-1]org.apache.catalina.startup.HostConfig.deployDirectoryDeploymentofwebapplicationdirectory[/usr/local/tomcat/webapps/manager]hasfinishedin[14]ms01-Sep-201812:52:39.827INFO[main]org.apache.coyote.AbstractProtocol.startStartingProtocolHandler["http-nio-8080"]01-Sep-201812:52:39.834INFO[main]org.apache.coyote.AbstractProtocol.startStartingProtocolHandler["ajp-nio-8009"]01-Sep-201812:52:39.836INFO[main]org.apache.catalina.startup.Catalina.startServerstartupin510ms
[root@t-dockerchenzx]#dockerrun-it-p8080:8080tomcatls-ltotal120-rw-r-----1rootroot19533Aug1208:23BUILDING.txt-rw-r-----1rootroot6090Aug1208:23CONTRIBUTING.md-rw-r-----1rootroot57092Aug1208:23LICENSE-rw-r-----1rootroot1726Aug1208:23NOTICE-rw-r-----1rootroot3255Aug1208:23README.md-rw-r-----1rootroot7140Aug1208:23RELEASE-NOTES-rw-r-----1rootroot16262Aug1208:23RUNNING.txtdrwxr-x---2rootroot313Aug2300:47bindrwx--S---2rootroot238Aug1208:23confdrwxr-sr-x3rootstaff19Aug2300:47includedrwxr-x---2rootroot4096Aug2300:47libdrwxr-x---2rootroot6Aug1208:20logsdrwxr-sr-x3rootstaff151Aug2300:47native-jni-libdrwxr-x---2rootroot30Aug2300:47tempdrwxr-x---7rootroot81Aug1208:21webappsdrwxr-x---2rootroot6Aug1208:20work
通过上面可以看出,最后面的ls -l覆盖了启动tomcat的操作。这就是CMD会被docker run之后的参数替换。
ENTRYPOINTdocker run之后的参数会被当做参数传递给ENTRYPOINT,之后形成新的命令组合。
[root@t-dockerCMD]#catDockerfilefromcentosRUNyum-yinstallcurlCMD["curl","-s","http://ip.cn"][root@t-dockerCMD]#dockerbuild-fDockerfile-tmyip.SendingbuildcontexttoDockerdaemon2.048kBStep1/3:fromcentos--->5182e96772bfStep2/3:RUNyum-yinstallcurl--->Runninginaa5e5f0a8597Loadedplugins:fastestmirror,ovlDeterminingfastestmirrors*base:mirrors.huaweicloud.com*extras:mirrors.huaweicloud.com*updates:mirrors.neusoft.edu.cnPackagecurl-7.29.0-46.el7.x86_64alreadyinstalledandlatestversionNothingtodoRemovingintermediatecontaineraa5e5f0a8597--->a35f6ad9700fStep3/3:CMD["curl","-s","http://ip.cn"]--->Runninginf59369e2a7dbRemovingintermediatecontainerf59369e2a7db--->ecf0b349d6a1Successfullybuiltecf0b349d6a1Successfullytaggedmyip:latest
[root@t-dockerCMD]#dockerimagesREPOSITORYTAGIMAGEIDCREATEDSIZEmyiplatestecf0b349d6a1Aboutaminuteago293MBmycentos1.30c868c56748b3hoursago442MBchenzx/centoslatesta37b96c1c7a735hoursago200MBchenzx/tomcat021.224c3d89d4d042daysago463MBtomcatlatest690cb3b9c7d19daysago463MBcentoslatest5182e96772bf3weeksago200MB[root@t-dockerCMD]#[root@t-dockerCMD]#dockerrun-itmyip当前IP:223.72.205.1来自:北京市移动
[root@t-dockerCMD]#curl-shttp://ip.cn当前IP:223.72.205.1来自:北京市移动[root@t-dockerCMD]#[root@t-dockerCMD]#curl-s-ihttp://ip.cnHTTP/1.1200OKDate:Sat,01Sep201813:19:24GMTContent-Type:text/html;charset=UTF-8Transfer-Encoding:chunkedConnection:keep-aliveSet-Cookie:__cfduid=defee6872152b4b29d3a55de1eb3f219d1535807964;expires=Sun,01-Sep-1913:19:24GMT;path=/;domain=.ip.cn;HttpOnlyServer:cloudflareCF-RAY:45380742316fb236-HKG当前IP:223.72.205.1来自:北京市移动
[root@t-dockerCMD]#dockerrunmyip当前IP:223.72.205.1来自:北京市移动[root@t-dockerCMD]#dockerrunmyip-i#可以看到报错了docker:Errorresponsefromdaemon:OCIruntimecreatefailed:container_linux.go:348:startingcontainerprocesscaused"exec:\"-i\":executablefilenotfoundin$PATH":unknown.ERRO[0000]errorwaitingforcontainer:contextcanceled
我们可以看到出现了一个可执行文件找不到的报错:executable file not found in $PATH。之前我们说过,跟在镜像名后面的是command,运行时会替换CMD的默认值。
因此,这里面的-i替换了原来的CMD参数,而不是在原来的curl -s http://ip.cn后面。而-i根本就不是命令,所以自然找不到。
那我们如果希望加入-i参数,我们就必须重新完整的输入这个命令:
[root@t-dockerCMD]#dockerrunmyipcurl-shttp://ip.cn-iHTTP/1.1200OKDate:Sat,01Sep201813:28:03GMTContent-Type:text/html;charset=UTF-8Transfer-Encoding:chunkedConnection:keep-aliveSet-Cookie:__cfduid=d8b76a4d8c8ebb4bf69f97a92cc39216e1535808483;expires=Sun,01-Sep-1913:28:03GMT;path=/;domain=.ip.cn;HttpOnlyServer:cloudflareCF-RAY:453813eed351b11c-HKG当前IP:223.72.205.1来自:北京市移动案例2:制作ENTRYPOINT版查询ip信息的容器
[root@t-dockerCMD]#catDockerfile2fromcentosRUNyum-yinstallcurlENTRYPOINT["curl","-s","http://ip.cn"][root@t-dockerCMD]#
[root@t-dockerCMD]#dockerbuild-fDockerfile2-tmyip2.SendingbuildcontexttoDockerdaemon3.072kBStep1/3:fromcentos--->5182e96772bfStep2/3:RUNyum-yinstallcurl--->Usingcache--->a35f6ad9700fStep3/3:ENTRYPOINT["curl","-s","http://ip.cn"]--->Runningin36f7f6639933Removingintermediatecontainer36f7f6639933--->b772af142e86Successfullybuiltb772af142e86Successfullytaggedmyip2:latest
[root@t-dockerCMD]#dockerrun-itmyip2当前IP:223.72.205.1来自:北京市移动[root@t-dockerCMD]#dockerrun-itmyip2-i##正确执行HTTP/1.1200OKDate:Sat,01Sep201813:57:37GMTContent-Type:text/html;charset=UTF-8Transfer-Encoding:chunkedConnection:keep-aliveSet-Cookie:__cfduid=ded1abdaf2b8375c2fb815f8d262e4c781535810257;expires=Sun,01-Sep-1913:57:37GMT;path=/;domain=.ip.cn;HttpOnlyServer:cloudflareCF-RAY:45383f3c074bb20c-HKG当前IP:223.72.205.1来自:北京市移动
可见,ENTRYPOINT没有覆盖原参数,而是追加到原参数的后面。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。