dockerfile 中 ENTRYPOINT 和 CMD的区别

大家好,在dockerfile当中, ENTRYPOINT 和CMD的作用于效果是很类似的

我们今天就来说一下它们两者的区别

ENTRYPOINT:指定这个启动的时候启动的时候要运行的命令,可以被替代

CMD : 指定这个启动的时候启动的时候要运行的命令,可以追加命令,只有最后一个cmd命令可以被执行

查看镜像:(使用centos 7.9的这个镜像,默认最新的centos8的网络源有问题)
在这里插入图片描述

些cmd 相关的一个dockerfile的测试文件

[root@localhost ~]# cd /home/dockerfile/
[root@localhost dockerfile]# vim test-dockerfile-cmd 

这样写

FROM centos:7.9.2009
#这个dockerfile文件的基础镜像(依托点)
CMD ["ls","-a"]
#run了这个容器之后,就会去执行的命令
#  这个例子当中,只有一个的情况下,就执行这一个
# 如果有多条cmd    ,则执行最后一个
#默认就是执行最后一个

构建成新的镜像,命名为testcmd

[root@localhost dockerfile]# docker build -f test-dockerfile-cmd  -t testcmd .

构建当中

Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM centos:7.9.2009
 ---> eeb6ee3f44bd
Step 2/2 : CMD ["ls","-a"]
 ---> Running in 40acaf9ea820
Removing intermediate container 40acaf9ea820
 ---> 89b0bf9382de
Successfully built 89b0bf9382de
Successfully tagged testcmd:latest
                

查看镜像

[root@localhost dockerfile]# docker images
REPOSITORY       TAG        IMAGE ID       CREATED         SIZE
testcmd          latest     89b0bf9382de   6 minutes ago   204MB
mycentos         0.1        6e65267278ae   16 hours ago    580MB
<none>           <none>     79f0605371e9   17 hours ago    231MB
<none>           <none>     2db23636a1fb   18 hours ago    231MB
shuaige/centos   2.0        d8e0f44b8da3   40 hours ago    231MB
tomcat04         1.0        12b32b1a124d   2 days ago      685MB
nginx            latest     605c77e624dd   2 months ago    141MB
tomcat           9.0        b8e65a4d736d   2 months ago    680MB
tomcat           latest     fb5657adc892   2 months ago    680MB
mysql            5.7        c20987f18b13   2 months ago    448MB
hello-world      latest     feb5d9fea6a5   5 months ago    13.3kB
centos           7.9.2009   eeb6ee3f44bd   5 months ago    204MB
centos           latest     5d0da3dc9764   5 months ago    231MB

注意看,testcmd 这个镜像是我们刚刚使用dockerfile文件构建出来的新镜像

在这里插入图片描述

查看镜像(现在看centos 7.9.2009,这是刚刚dockerfile文件当中的基础镜像)

在这里插入图片描述

运行一下我们7.9 的centos原生镜像

[root@localhost dockerfile]# docker run -it centos:7.9.2009  /bin/bash
[root@323f1a9afdec /]# 

这个就是原生镜像进行容器的效果

[root@localhost dockerfile]# docker run  centos:7.9.2009  
[root@localhost dockerfile]# 

注意看testcmd 这个镜像
在这里插入图片描述

运行这个testcmd(这就是它的效果)

[root@localhost dockerfile]# docker run  testcmd
.
..
.dockerenv
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

这样做会报错的

[root@localhost dockerfile]# docker run  testcmd -l
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown.
[root@localhost dockerfile]# 

(因为在使用cmd的情况下,-l 替换了cmd的命令操作) -l 在这种情况下不是命令

换一句简单的话来讲cmd 命令写完之后,执行这条命令

[root@localhost dockerfile]# docker run  testcmd

等效于进入容器之后,就执行了CMD [“ls”,"-a"]: ls -a 的操作

所以就被替换了

这样做,才可以避免上面的替换

[root@localhost dockerfile]# docker run  testcmd ls -al
total 12
drwxr-xr-x.   1 root root     6 Mar 10 06:33 .
drwxr-xr-x.   1 root root     6 Mar 10 06:33 ..
-rwxr-xr-x.   1 root root     0 Mar 10 06:33 .dockerenv
-rw-r--r--.   1 root root 12114 Nov 13  2020 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Nov 13  2020 bin -> usr/bin
drwxr-xr-x.   5 root root   340 Mar 10 06:33 dev
drwxr-xr-x.   1 root root    66 Mar 10 06:33 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Nov 13  2020 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Nov 13  2020 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 253 root root     0 Mar 10 06:33 proc
dr-xr-x---.   2 root root   114 Nov 13  2020 root
drwxr-xr-x.  11 root root   148 Nov 13  2020 run
lrwxrwxrwx.   1 root root     8 Nov 13  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 Mar  4 07:14 sys
drwxrwxrwt.   7 root root   132 Nov 13  2020 tmp
drwxr-xr-x.  13 root root   155 Nov 13  2020 usr
drwxr-xr-x.  18 root root   238 Nov 13  2020 var

我们现在编辑 ENTRYPOINT这个dockerfile文件

root@localhost dockerfile]# vim test-dockerfile-point

写内容:

FROM centos:7.9.2009
ENTRYPOINT ["ls","-a"]

构建新的镜像 命名为testpoint

 [root@localhost dockerfile]# docker build -f test-dockerfile-point  -t testpoint .

 正在构建中

     ending build context to Docker daemon   5.12kB
Step 1/2 : FROM centos:7.9.2009
 ---> eeb6ee3f44bd
Step 2/2 : ENTRYPOINT ["ls","-a"]
 ---> Running in 6687492316fd
Removing intermediate container 6687492316fd
 ---> 43854fb82409
Successfully built 43854fb82409
Successfully tagged testpoint:latest
                                                                                                

这是我们构建的新镜像
在这里插入图片描述

这个地方与我们上面的CMD 执行结果是一样的

[root@localhost dockerfile]# docker run 43854fb82409  
.
..
.dockerenv
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var

刚才CMD 方式,加上-l 是有错的,而这里是没有错的

可以看到,追加的命令是可以在ENTRYPOINT的后面

[root@localhost dockerfile]# docker run 43854fb82409  -l
total 12
drwxr-xr-x.   1 root root     6 Mar 10 09:21 .
drwxr-xr-x.   1 root root     6 Mar 10 09:21 ..
-rwxr-xr-x.   1 root root     0 Mar 10 09:21 .dockerenv
-rw-r--r--.   1 root root 12114 Nov 13  2020 anaconda-post.log
lrwxrwxrwx.   1 root root     7 Nov 13  2020 bin -> usr/bin
drwxr-xr-x.   5 root root   340 Mar 10 09:21 dev
drwxr-xr-x.   1 root root    66 Mar 10 09:21 etc
drwxr-xr-x.   2 root root     6 Apr 11  2018 home
lrwxrwxrwx.   1 root root     7 Nov 13  2020 lib -> usr/lib
lrwxrwxrwx.   1 root root     9 Nov 13  2020 lib64 -> usr/lib64
drwxr-xr-x.   2 root root     6 Apr 11  2018 media
drwxr-xr-x.   2 root root     6 Apr 11  2018 mnt
drwxr-xr-x.   2 root root     6 Apr 11  2018 opt
dr-xr-xr-x. 250 root root     0 Mar 10 09:21 proc
dr-xr-x---.   2 root root   114 Nov 13  2020 root
drwxr-xr-x.  11 root root   148 Nov 13  2020 run
lrwxrwxrwx.   1 root root     8 Nov 13  2020 sbin -> usr/sbin
drwxr-xr-x.   2 root root     6 Apr 11  2018 srv
dr-xr-xr-x.  13 root root     0 Mar  4 07:14 sys
drwxrwxrwt.   7 root root   132 Nov 13  2020 tmp
drwxr-xr-x.  13 root root   155 Nov 13  2020 usr
drwxr-xr-x.  18 root root   238 Nov 13  2020 var
                                            

这就是这两者之间的区别:

好了,有关于dockerfile 中 ENTRYPOINT 和 CMD的区别就到这里了,谢谢大家

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

思诚代码块

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值