DockerFile----CMD和ENTRYPOINT区别案例
CMD和ENTRYPOINT区别案例
1.使用CMD指令,实现运行容器查当前IP的功能
FROM centos:7
RUN yum -y install curl
CMD ["curl","-s","https://www.cip.cc/"]
[root@aaa ~/mydocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos 02 520e36902f2a 31 seconds ago 290 MB
[root@aaa ~/mydocker]# docker run -it mycentos:02
IP : 39.90.13.119
地址 : 中国 山东 聊城
运营商 : 联通
如果加个参数“-i”,就会报错
[root@aaa ~/mydocker]# docker run -it mycentos:02 -i
/usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:235: starting container process caused "exec: \"-i\": executable file not found in $PATH".
原因:“-i”参数替换了原来的CMD,而不是添加到原来的curl -s http://www.cip.cc 后面
如果希望加入-i这个参数,就必须输入完整的命令
[root@aaa ~/mydocker]# docker run -it 520e36902f2a curl -s http://www.cip.cc -i
HTTP/1.1 200 OK
Server: openresty
Date: Thu, 06 Aug 2020 13:31:46 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-cip-c: H
IP : 39.90.13.119
地址 : 中国 山东 聊城
运营商 : 联通
2.使用ENTRYPOINT指令,实现运行容器可以添加参数
#Dockerfile05
FROM centos:7
RUN yum -y install curl
ENTRYPOINT ["curl","-s","https://www.cip.cc/"]
[root@aaa ~/mydocker]# docker build -f Dockerfile05 -t mycentos:03 .
Sending build context to Docker daemon 6.144 kB
Step 1/3 : FROM centos:7
---> b5b4d78bc90c
Step 2/3 : RUN yum -y install curl
---> Using cache
---> db8ecada6a2d
Step 3/3 : ENTRYPOINT curl -s https://www.cip.cc/
---> Running in e7f282a2281d
---> f79f5093d0ce
Removing intermediate container e7f282a2281d
Successfully built f79f5093d0ce
追加“-i”参数
[root@aaa ~/mydocker]# docker run -it mycentos:03 -i
HTTP/1.1 200 OK
Server: openresty
Date: Thu, 06 Aug 2020 13:36:40 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-cip-c: H
IP : 39.90.13.119
地址 : 中国 山东 聊城
运营商 : 联通
结论:CMD指令运行容器添加参数会替换CMD的命令;而ENTRYPOINT指令相当于追加参数
作者:ccku
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如有问题或建议,请多多赐教,非常感谢。