9.Dockerfile详解【3】

 

1. CMD说明

Dockerfile 中可以有多个CMD的命令,但只有最后一个生效,CMD会被docker run之后的参数替换掉

1.1查看tomcat的Dockerfile

1
2
EXPOSE 8080
CMD ["catalina.sh", "run"]

从上面的可以看出最后执行了一个catalina.sh的命令  相当于./bin/catalina.sh  

运行如下命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost ~]# docker run -it -p 9999:8080 0eed806285b5 ls -lh
total 124K
-rw-r--r--. 1 root root 19K Oct 6 17:01 BUILDING.txt
-rw-r--r--. 1 root root 5.3K Oct 6 17:01 CONTRIBUTING.md
-rw-r--r--. 1 root root 56K Oct 6 17:01 LICENSE
-rw-r--r--. 1 root root 1.7K Oct 6 17:01 NOTICE
-rw-r--r--. 1 root root 3.2K Oct 6 17:01 README.md
-rw-r--r--. 1 root root 7.0K Oct 6 17:01 RELEASE-NOTES
-rw-r--r--. 1 root root 16K Oct 6 17:01 RUNNING.txt
drwxr-xr-x. 2 root root 4.0K Nov 3 02:52 bin
drwxr-xr-x. 2 root root 238 Oct 6 17:01 conf
drwxr-xr-x. 2 root root 4.0K Nov 3 02:52 lib
drwxrwxrwx. 2 root root 6 Oct 6 16:57 logs
drwxr-xr-x. 2 root root 134 Nov 3 02:52 native-jni-lib
drwxrwxrwx. 2 root root 30 Nov 3 02:52 temp
drwxr-xr-x. 2 root root 6 Nov 3 02:52 webapps
drwxr-xr-x. 7 root root 81 Oct 6 16:58 webapps.dist
drwxrwxrwx. 2 root root 6 Oct 6 16:57 work
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#

发现这个tomcat的容器启动之后就退出了,这是因为在启动容器的时候后面加了ls -lh那么会覆盖Dockerfile里面的CMD ["catalina.sh", "run"]

2. ENTRYPOINT说明

使用ENTRYPOINT来运行命令,在run运行的参数会追加到新的命令后面

3. CMD和ENTRYPOINT的区别

编写Dockerfile:

1
2
FROM centos
CMD ["ls", "-a"]

构建镜像:

1
2
3
4
5
6
7
8
9
[root@localhost myls]# docker build -t myls:1.0 .
Sending build context to Docker daemon 2.048 kB
Step 1/2 : FROM centos
---> 0d120b6ccaa8
Step 2/2 : CMD ls -a
---> Running in d4bb812b3f22
---> 7ef84a2f1392
Removing intermediate container d4bb812b3f22
Successfully built 7ef84a2f1392

启动镜像:

1
2
[root@localhost myls]# docker run -it myls:1.0 -lh
/usr/bin/docker-current: Error response from daemon: oci runtime error: container_linux.go:235: starting container process caused "exec: \"-lh\": executable file not found in $PATH".

出现错误,这是因为Dockerfile里面使用的是CMD ["ls", "-a"],会被-lh覆盖,-lh不是完整的命令

修改Dockerfile:

1
2
FROM centos
ENTRYPOINT ["ls", "-a"]

构建镜像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost myls]# docker build -t myls:1.1 .
Sending build context to Docker daemon 2.048 kB
Step 1/2 : FROM centos
---> 0d120b6ccaa8
Step 2/2 : ENTRYPOINT ls -a
---> Running in b01a46cd4255
---> 4d66a0a91d3f
Removing intermediate container b01a46cd4255
Successfully built 4d66a0a91d3f
[root@localhost myls]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myls 1.1 4d66a0a91d3f 15 seconds ago 215 MB
myls 1.0 7ef84a2f1392 2 minutes ago 215 MB
mycentos01 2.0 f503701c7737 24 hours ago 272 MB
mycentos01 1.0 9aea850e12d7 2 days ago 215 MB
mytomcat01 1.0 b14e2098d46d 3 days ago 533 MB
docker.io/tomcat 8.5 0eed806285b5 2 weeks ago 533 MB
docker.io/nginx latest f35646e83998 5 weeks ago 133 MB
docker.io/centos latest 0d120b6ccaa8 3 months ago 215 MB
docker.io/hello-world latest bf756fb1ae65 10 months ago 13.3 kB

运行镜像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost myls]# docker run -it myls:1.1 -lh
total 0
drwxr-xr-x. 1 root root 17 Nov 17 14:30 .
drwxr-xr-x. 1 root root 17 Nov 17 14:30 ..
-rwxr-xr-x. 1 root root 0 Nov 17 14:30 .dockerenv
lrwxrwxrwx. 1 root root 7 May 11 2019 bin -> usr/bin
drwxr-xr-x. 5 root root 360 Nov 17 14:30 dev
drwxr-xr-x. 1 root root 66 Nov 17 14:30 etc
drwxr-xr-x. 2 root root 6 May 11 2019 home
lrwxrwxrwx. 1 root root 7 May 11 2019 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 May 11 2019 lib64 -> usr/lib64
drwx------. 2 root root 6 Aug 9 21:40 lost+found
drwxr-xr-x. 2 root root 6 May 11 2019 media
drwxr-xr-x. 2 root root 6 May 11 2019 mnt
drwxr-xr-x. 2 root root 6 May 11 2019 opt
dr-xr-xr-x. 134 root root 0 Nov 17 14:30 proc
dr-xr-x---. 2 root root 162 Aug 9 21:40 root
drwxr-xr-x. 1 root root 21 Nov 17 14:30 run
lrwxrwxrwx. 1 root root 8 May 11 2019 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 May 11 2019 srv
dr-xr-xr-x. 13 root root 0 Nov 17 13:46 sys
drwxrwxrwt. 7 root root 145 Aug 9 21:40 tmp
drwxr-xr-x. 12 root root 144 Aug 9 21:40 usr
drwxr-xr-x. 20 root root 262 Aug 9 21:40 var

-lh被追加到ls -a的后面了,最终为ls -a -lh

4. 总结

  1. CMD ["ls", "-a"]

docker run -it myls:1.0 -lh: 报错,因为-lh会覆盖ls -a,最终为-lh,不是完整的命令,会报错

docker run -it myls:1.0 ls -lh: 不报错,因为ls -lh会覆盖ls -a,最终为ls -lh,是完整的命令

  1. ENTRYPOINT ["ls", "-a"]

docker run -it myls:1.1 ls -lh: 报错,因为ls -lh会追加到ls -a之后,最终为ls -a ls -lh,不是正确的命令,会报错

docker run -it myls:1.1 -lh: 不报错,因为-lh会追加到ls -a之后,最终为ls -a -lh,是正确的命令