CMD/ENTRYPOINT区别
相同点:都是指定一个容器:启动时要运行的命令
不同点(重点):
CMD:
dockerfile中可以有多个CMD指令,但是只有最后一个生效,CMD会被docker run之后的参数替换
例子:docker run -it -p 8888:8080 tomcat ls -l
[root@tzh tzh]# docker run -it -p 8888:8080 tomcat ls -l
total 152
-rw-r--r-- 1 root root 18982 Jun 3 17:13 BUILDING.txt
-rw-r--r-- 1 root root 5409 Jun 3 17:13 CONTRIBUTING.md
-rw-r--r-- 1 root root 57092 Jun 3 17:13 LICENSE
-rw-r--r-- 1 root root 2333 Jun 3 17:13 NOTICE
-rw-r--r-- 1 root root 3255 Jun 3 17:13 README.md
-rw-r--r-- 1 root root 6898 Jun 3 17:13 RELEASE-NOTES
-rw-r--r-- 1 root root 16262 Jun 3 17:13 RUNNING.txt
drwxr-xr-x 2 root root 4096 Jun 10 08:17 bin
drwxr-xr-x 2 root root 4096 Jun 3 17:13 conf
drwxr-xr-x 2 root root 4096 Jun 10 08:17 lib
drwxrwxrwx 2 root root 4096 Jun 3 17:10 logs
drwxr-xr-x 2 root root 4096 Jun 10 08:17 native-jni-lib
drwxrwxrwx 2 root root 4096 Jun 10 08:17 temp
drwxr-xr-x 2 root root 4096 Jun 10 08:17 webapps
drwxr-xr-x 7 root root 4096 Jun 3 17:11 webapps.dist
drwxrwxrwx 2 root root 4096 Jun 3 17:10 work
[root@tzh tzh]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@tzh tzh]#tomcat 默认的dockfile里面最后的是CMD["catalina.sh","run"],可是当我们执行了docker run -it -p 8888:8080 tomcat ls -l 以后,由于CMD只保留最后一个CMD,所以最后就变成了ls -l /usr/local/tomcat,所以就如上代码所示,看到的是/usr/local/tomcat的内容。而且通过docker ps发现tomcat并没运行起来。
ENTROYPOINT:
docker run之后的参数会被当做参数传递给 ENTRYPOINT,之后形成新的命令组合。就是更加强大!
给他用上ENTRPOINT演示:
1、制作一个ENTRPOINT版可以查询IP信息的容器
[root@tzh tzh]# cat test2
FROM centos
RUN yum install -y curl
CMD [ "curl", "-s", "http://ip.cn" ]2、构建并启动它
docker build -f test2 -t myip .
docker run -it myip #运行起来就可以看到自己的ip、地址
docker run -it myip -i #发现报错
[root@tzh tzh]# docker run -it myip -i
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"-i\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled解决办法:
cp test2 test3 #拷贝一份dockerfile
[root@tzh tzh]# cat test3
FROM centos
RUN yum install -y curl
ENTRYPOINT [ "curl", "-s", "http://ip.cn" ]
再次构建启动一个新的容器
docker build -f test3 -t myip2 .
docker run -it myip2 -i