CMD和ENTRYPOINT的区别
CMD测试
编写dockerfile文件
[root@iZbp1htnoa70qlle394f3wZ dockerfile]# cat CMDos
FROM centos:7
CMD ["ls","-a"]
构建镜像
[root@iZbp1htnoa70qlle394f3wZ dockerfile]# docker build -f CMDos -t cmdos:1.0 .
启动镜像
[root@iZbp1htnoa70qlle394f3wZ dockerfile]# docker run -it 7083385c1517
. .dockerenv bin etc lib media opt root sbin sys usr
.. anaconda-post.log dev home lib64 mnt proc run srv tmp var
发现启动镜像时,执行了CMD命令
我们尝试在启动镜像时追加命令 发现报错
[root@iZbp1htnoa70qlle394f3wZ dockerfile]# docker run 7083385c1517 -l
docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
ENTRYPOINT测试编写
dockerfile文件
[root@iZbp1htnoa70qlle394f3wZ dockerfile]# cat ENTRYPOINTos
FROM centos
ENTRYPOINT ["ls","-a"]
构建镜像
[root@iZbp1htnoa70qlle394f3wZ dockerfile]# docker build -f ENTRYPOINTos . # 没有指定镜像名和版本号(建议设置tag)
Sending build context to Docker daemon 4.096kB
Step 1/2 : FROM centos
---> 5d0da3dc9764
Step 2/2 : ENTRYPOINT ["ls","-a"]
---> Running in a9e6cee88acf
Removing intermediate container a9e6cee88acf
---> c7f77b087635
Successfully built c7f77b087635
运行镜像
[root@iZbp1htnoa70qlle394f3wZ dockerfile]# docker run -it c7f77b087635
. .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
发现在运行镜像后,执行了ENTRYPOINT命令
到这里好像和CMD命令没有区别
在运行ENTRYPOINT镜像时,追加命令
[root@iZbp1htnoa70qlle394f3wZ dockerfile]# docker run c7f77b087635 -l
total 56
drwxr-xr-x 1 root root 4096 Apr 23 01:59 .
drwxr-xr-x 1 root root 4096 Apr 23 01:59 ..
-rwxr-xr-x 1 root root 0 Apr 23 01:59 .dockerenv
lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin
drwxr-xr-x 5 root root 340 Apr 23 01:59 dev
drwxr-xr-x 1 root root 4096 Apr 23 01:59 etc
drwxr-xr-x 2 root root 4096 Nov 3 2020 home
lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64
drwx------ 2 root root 4096 Sep 15 2021 lost+found
drwxr-xr-x 2 root root 4096 Nov 3 2020 media
drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt
drwxr-xr-x 2 root root 4096 Nov 3 2020 opt
dr-xr-xr-x 103 root root 0 Apr 23 01:59 proc
dr-xr-x--- 2 root root 4096 Sep 15 2021 root
drwxr-xr-x 11 root root 4096 Sep 15 2021 run
lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin
drwxr-xr-x 2 root root 4096 Nov 3 2020 srv
dr-xr-xr-x 13 root root 0 Apr 21 15:07 sys
drwxrwxrwt 7 root root 4096 Sep 15 2021 tmp
drwxr-xr-x 12 root root 4096 Sep 15 2021 usr
drwxr-xr-x 20 root root 4096 Sep 15 2021 var
这时区别就出来了,ENTRYPOINT可以在运行时追加参数,而CMD不行

浙公网安备 33010602011771号