CMD/ENTRYPOINT鏡像案例
- 都是指定一個容器啓動時要運行的命令
- CMD
- ENTRYPOINT
- docker run 以後的參數會被看成參數傳遞給 ENTRYPOINT,以後造成新的命令
- case
- 製做CMD版能夠查詢IP信息的容器
DockerFilehtml
FROM centos RUN yum install -y curl CMD ["curl","-s","http://ip.cn" ]
建立查詢ip的鏡像linux
[root@localhost mydocker]# ll 總用量 8 -rw-r--r--. 1 root root 144 10月 11 15:01 Dockerfile -rw-r--r--. 1 root root 238 10月 12 15:05 Dockerfile2 [root@localhost mydocker]# vim Dockerfile3 [root@localhost mydocker]# cat Dockerfile3 FROM centos RUN yum install -y curl CMD ["curl","-s","http://ip.cn" ] [root@localhost mydocker]# docker build -f /mydocker/Dockerfile3 -t myip . Sending build context to Docker daemon 4.096kB Step 1/3 : FROM centos ---> 75835a67d134 Step 2/3 : RUN yum install -y curl ---> Running in 1e3f94b33032 Loaded plugins: fastestmirror, ovl Determining fastest mirrors * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.cn99.com Package curl-7.29.0-46.el7.x86_64 already installed and latest version Nothing to do Removing intermediate container 1e3f94b33032 ---> 1bd5b74f7407 Step 3/3 : CMD ["curl","-s","http://ip.cn" ] ---> Running in 3a3c44f38737 Removing intermediate container 3a3c44f38737 ---> 9e3c14f76b1d Successfully built 9e3c14f76b1d Successfully tagged myip:latest
運行容器docker
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE myip latest 9e3c14f76b1d About a minute ago 299MB mycentos 1.0 c2d4f6acb9af 22 hours ago 455MB zhaoyoung/nodocstomcat 1.0 84498728984a 2 days ago 463MB centos latest 75835a67d134 3 days ago 200MB tomcat latest 41a54fe1f79d 4 weeks ago 463MB hello-world latest 4ab4c602aa5e 5 weeks ago 1.84kB [root@localhost ~]# docker run -it myip 當前 IP:112.16.153.216 來自:浙江省杭州市 移動
在宿主機終端執行curl -s -i http://ip.cnvim
[root@localhost /]# curl -s http://ip.cn 當前 IP:112.16.153.216 來自:浙江省杭州市 移動 [root@localhost /]# curl -s -i http://ip.cn HTTP/1.1 200 OK Date: Sat, 13 Oct 2018 07:15:59 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Set-Cookie: __cfduid=de9797f2bb3b7b4cbf7e271a857dad0e01539414958; expires=Sun, 13-Oct-19 07:15:58 GMT; path=/; domain=.ip.cn; HttpOnly Server: cloudflare CF-RAY: 469004a530f3b22a-HKG 當前 IP:112.16.153.216 來自:浙江省杭州市 移動
在容器終端執行centos
[root@localhost ~]# 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.
也就是說本來DockerFile中的CMD命令被 -i 替換了,若是要加入 -i 這個命令,則 docker run myip curl -s -i http://ip.cntomcat
- 製做ENTRYPOINT版能夠查詢IP信息的容器
DockerFiledom
FROM centos RUN yum install -y curl ENTRYPOINT ["curl","-s","http://ip.cn" ]
建立鏡像curl
[root@localhost mydocker]# vim Dockerfile4 [root@localhost mydocker]# cat Dockerfile4 FROM centos RUN yum install -y curl ENTRYPOINT ["curl","-s","http://ip.cn" ] [root@localhost mydocker]# docker build -f /mydocker/Dockerfile4 -t myip2 . Sending build context to Docker daemon 5.12kB Step 1/3 : FROM centos ---> 75835a67d134 Step 2/3 : RUN yum install -y curl ---> Using cache ---> 1bd5b74f7407 Step 3/3 : ENTRYPOINT ["curl","-s","http://ip.cn" ] ---> Running in 3b769d41c963 Removing intermediate container 3b769d41c963 ---> dcbb4656e640 Successfully built dcbb4656e640 Successfully tagged myip2:latest
運行容器ui
[root@localhost ~]# docker run myip 當前 IP:112.16.153.216 來自:浙江省杭州市 移動
對比下CMD 和 ENTRYPOINT,就能夠很清楚的區分兩個命令的不一樣點:前者會被新加的命令覆蓋,後者則是在原有的命令後加上新的命令url
[root@localhost ~]# docker run myip2 當前 IP:112.16.153.216 來自:浙江省杭州市 移動 [root@localhost ~]# docker run myip2 -i HTTP/1.1 200 OK Date: Sat, 13 Oct 2018 07:31:14 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked Connection: keep-alive Set-Cookie: __cfduid=d26cb3e44761590c2df6178cfd18fc6ee1539415874; expires=Sun, 13-Oct-19 07:31:14 GMT; path=/; domain=.ip.cn; HttpOnly Server: cloudflare CF-RAY: 46901afe67e9b1c4-HKG 當前 IP:112.16.153.216 來自:浙江省杭州市 移動