CMD和ENTRYPOINT
CMD:指定这个容器启动的时候需要执行的命令,但是只有最后一个CMD会生效
ENTRYPOINT:指定这个容器启动的时候需要执行的命令,且可以追加命令
测试CMD
1、编写使用CMD的dockerfile文件
举例:测试制作centos镜像时,输出ls -a的全部目录
FROM centos
CMD ["ls","-a"]
2、构建镜像
[root@liuyang dockerfile]# docker build -f first_dockerfile_cmd -t centos_liuyang:1.4 .
#输出日志
Sending build context to Docker daemon 3.072kB
Step 1/2 : FROM centos
---> 300e315adb2f
Step 2/2 : CMD ["ls","-a"]
---> Running in 2a3b27d08525
Removing intermediate container 2a3b27d08525
---> 67b2d498c93c
Successfully built 67b2d498c93c
Successfully tagged centos_liuyang:1.4
[root@liuyang dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos_liuyang 1.4 67b2d498c93c 7 seconds ago 209MB
centos_dockerfile 1.3 2900faf7489f 16 minutes ago 291MB
centos_dockerfile 1.1 ff7532b27bf0 31 minutes ago 291MB
centos_dockerfile 1.2 316bfc73c556 31 minutes ago 291MB
liuyang_centos/centos 1.0 6bb3a414dd02 5 hours ago 209MB
centos 1.2 c7755e562e14 2 days ago 209MB
mysql 5.7 a70d36bc331a 7 weeks ago 449MB
tomcat latest 040bdb29ab37 2 months ago 649MB
nginx latest f6d0b4767a6c 2 months ago 133MB
centos latest 300e315adb2f 3 months ago 209MB
3、查看镜像,并启动容器,查看效果(是否生成了centos 的ls -a,也就是CMD指令)
[root@liuyang dockerfile]# docker run -it 67b2d498c93c
. .dockerenv dev home lib64 media opt root sbin sys usr
.. bin etc lib lost+found mnt proc run srv tmp var
已生效!
此时我想在指令ls -a 后面追加l 变成ls -al :
[root@liuyang dockerfile]# docker run -it 67b2d498c93c -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"-l\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled
[root@liuyang dockerfile]#
发现不行,必须写全代码:
[root@liuyang dockerfile]# docker run -it 67b2d498c93c ls -al
total 56
drwxr-xr-x 1 root root 4096 Mar 14 14:14 .
drwxr-xr-x 1 root root 4096 Mar 14 14:14 ..
-rwxr-xr-x 1 root root 0 Mar 14 14:14 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 15:22 bin -> usr/bin
drwxr-xr-x 5 root root 360 Mar 14 14:14 dev
drwxr-xr-x 1 root root 4096 Mar 14 14:14 etc
drwxr-xr-x 2 root root 4096 Nov 3 15:22 home
lrwxrwxrwx 1 root root 7 Nov 3 15:22 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 3 15:22 lib64 -> usr/lib64
drwx------ 2 root root 4096 Dec 4 17:37 lost+found
drwxr-xr-x 2 root root 4096 Nov 3 15:22 media
drwxr-xr-x 2 root root 4096 Nov 3 15:22 mnt
drwxr-xr-x 2 root root 4096 Nov 3 15:22 opt
dr-xr-xr-x 145 root root 0 Mar 14 14:14 proc
dr-xr-x--- 2 root root 4096 Dec 4 17:37 root
drwxr-xr-x 11 root root 4096 Dec 4 17:37 run
lrwxrwxrwx 1 root root 8 Nov 3 15:22 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Nov 3 15:22 srv
dr-xr-xr-x 13 root root 0 Mar 1 05:43 sys
drwxrwxrwt 7 root root 4096 Dec 4 17:37 tmp
drwxr-xr-x 12 root root 4096 Dec 4 17:37 usr
drwxr-xr-x 20 root root 4096 Dec 4 17:37 var
[root@liuyang dockerfile]#
现在我们试一试ENTRYPOINT:
1、编写使用ENTRYPOINT的dockerfile文件
举例:测试制作centos镜像时,输出ls -a的全部目录
FROM centos
ENTRYPOINT ["ls","-a"]
2、构建镜像
[root@liuyang dockerfile]# docker build -f first_dockerfile_ENTRYPOINT -t centos_liuyang:1.6 .
3、查看镜像,并启动容
[root@liuyang dockerfile]# docker run -it 2c0580c11880
. .dockerenv dev home lib64 media opt root sbin sys usr
.. bin etc lib lost+found mnt proc run srv tmp var
器,查看效果(是否生成了centos 的ls -a)
4、此时我想在指令ls -a 后面追加l 变成ls -al :
[root@liuyang dockerfile]# docker run -it 2c0580c11880 -l
total 56
drwxr-xr-x 1 root root 4096 Mar 14 14:16 .
drwxr-xr-x 1 root root 4096 Mar 14 14:16 ..
-rwxr-xr-x 1 root root 0 Mar 14 14:16 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 15:22 bin -> usr/bin
drwxr-xr-x 5 root root 360 Mar 14 14:16 dev
drwxr-xr-x 1 root root 4096 Mar 14 14:16 etc
drwxr-xr-x 2 root root 4096 Nov 3 15:22 home
lrwxrwxrwx 1 root root 7 Nov 3 15:22 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 3 15:22 lib64 -> usr/lib64
drwx------ 2 root root 4096 Dec 4 17:37 lost+found
drwxr-xr-x 2 root root 4096 Nov 3 15:22 media
drwxr-xr-x 2 root root 4096 Nov 3 15:22 mnt
drwxr-xr-x 2 root root 4096 Nov 3 15:22 opt
dr-xr-xr-x 144 root root 0 Mar 14 14:16 proc
dr-xr-x--- 2 root root 4096 Dec 4 17:37 root
drwxr-xr-x 11 root root 4096 Dec 4 17:37 run
lrwxrwxrwx 1 root root 8 Nov 3 15:22 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Nov 3 15:22 srv
dr-xr-xr-x 13 root root 0 Mar 1 05:43 sys
drwxrwxrwt 7 root root 4096 Dec 4 17:37 tmp
drwxr-xr-x 12 root root 4096 Dec 4 17:37 usr
drwxr-xr-x 20 root root 4096 Dec 4 17:37 var
[root@liuyang dockerfile]#
生效!与CMD的区别显而易见,ENTRYPOINT 可以直接在run 后面追加 代码!