【Docker实战之入门】Dockerfile详细分析(5):ENTRYPOINT和CMD命令的区别

前端之家收集整理的这篇文章主要介绍了【Docker实战之入门】Dockerfile详细分析(5):ENTRYPOINT和CMD命令的区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

ENTRYPOINT用法
clipboard.png

entrypoint在dockerfile当中只有最后一条生效,其他entrypoint都不生效
第一种写法类似数组形式,推荐使用。使用这种方法启动的进程的pid1
第二章方法执行命令启动的进程,该进程的pid为执行完这个shell的pid

CMD用法
clipboard.png

第一种用法,也是数组格式。
第二种用法,为entrypoint指定参数。比如,entrypoint执行命令 usrs/bin/Nginx,cmd可以提供参数如start,结合起来即为usrs/bin/Nginx start.
第三种写法,pid号也是shell执行的pid号。是Linux下 /bin/sh -c用法

构建镜像需要基于一个基础镜像。下面基于centos7这个基础镜像构建一个镜像。

[root@Optimus /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
wordpress           latest              b027afd30886        33 hours ago        409.2 MB
csphere/wordpress   4.2                 6d90342cc99d        2 days ago          747.3 MB
csphere/MysqL       5.5                 e147ac2a588e        6 days ago          752.7 MB
csphere/PHP-fpm     5.4                 1b08c94ce801        8 days ago          709.7 MB
csphere/centos      7.1                 fd1f7619e63e        9 days ago          613 MB
centos              centos7.1.1503      879c6d07c60e        7 weeks ago         212.1 MB
[root@Optimus /]# 

-----
使用cmd

创建dockerfile

[root@Optimus /]# vim Dockerfile

FROM centos:centos7.1.1503

CMD ["/bin/echo","This is test cmd"]

build的时候出错,提示没有权限

[root@Optimus /]# docker build -t csphere/cmd:0.1 .
Error checking context is accessible: 'no permission to read from 'proc/sys/net/ipv4/route/flush''. Please check permissions and try again.

删除Dockerfile

rm -rf 目录名字
-r 就是向下递归,不管有多少级目录,一并删除
-f 就是直接强行删除,不作任何提示的意思~    

rm -rf Dockerfile

进入docker-training目录下面,重新创建Dockerfile,构建镜像

[root@Optimus docker-training]# docker build -t csphere/cmd:0.1
docker: "build" requires 1 argument.
See 'docker build --help'.

Usage: docker build [OPTIONS] PATH | URL | -

Build a new image from the source code at PATH
[root@Optimus docker-training]# docker build -t csphere/cmd:0.1 .
Sending build context to Docker daemon 27.55 MB
Sending build context to Docker daemon
Step 0 : FROM centos:centos7.1.1503
---> 879c6d07c60e
Step 1 : CMD /bin/echo This is test cmd
---> Running in 3dce9e75bdc8
---> f32bb73e8383
Removing intermediate container 3dce9e75bdc8
Successfully built f32bb73e8383
[root@Optimus docker-training]#

接下来可以通过csphere/cmd:0.1这个镜像来启动一个容器

docker run -it --rm 
--rm参数的意识是,只要容器一退出,则删除容器

[root@Optimus docker-training]# docker run -it csphere/cmd:0.1
This is test cmd

可以看到,/bin/echo This is test cmd 这条命令成功执行。

直接进入container里面,而不执行echo指令,可以在docker run 后面直接加上/bin/bash命令,/bin/bash命令会覆盖掉cmd后面的命令。

[root@Optimus docker-training]# docker run -it csphere/cmd:0.1 /bin/bash
[root@cd32d71fb9c5 /]# 

/bin/bash 命令覆盖掉了dockerfile中的cmd命令,直接进入到了container中。

---

使用entrypoint

[root@Optimus docker-training]# vim Dockerfile 

FROM centos:centos7.1.1503

ENTRYPOINT ["/bin/echo","This is test entrypoint"]
~

构建镜像

[root@Optimus docker-training]# docker build -t csphere/ent:0.1 .
Sending build context to Docker daemon 27.55 MB
Sending build context to Docker daemon 
Step 0 : FROM centos:centos7.1.1503
          ---> 879c6d07c60e
          

直接在docker run 后面加参数/bin/bash,entrypoint会把/bin/bash当成一个echo的字符串参数,不会进入到容器中。

[root@Optimus docker-training]# docker run -it csphere/ent:0.1 /bin/bash
This is test entrypoint /bin/bash

如果想覆盖dockerfile中entrypoint指令,可以在docker run命令中加--entrypoint参数来指定。

[root@Optimus docker-training]# docker run -it --entrypoint=/bin/bash csphere/ent:0.1
[root@bc7378b9ca83 /]# 

Step 1 : ENTRYPOINT /bin/echo This is test entrypoint
---> Running in 40b01fac38af
---> 9714a8b5bb85
Removing intermediate container 40b01fac38af
Successfully built 9714a8b5bb85
[root@Optimus docker-training]#

启动容器

[root@Optimus docker-training]# docker run -it csphere/ent:0.1
This is test entrypoint

----
其他

删除镜像

docker rmi 镜像
如果有启动中的容器正在使用该镜像,不能删除该镜像。删除镜像前,先停止container。-f 参数强制删除镜像。

如果对文件修改,可以使用一下命令,更新git 仓库

git add *
git commit -M ""
git push origin master  提交到自己的仓库中

猜你在找的Docker相关文章