Dockerfile RUN, CMD & ENTRYPOINT

在使用Dockerfile创建image时, 有几条指令比较容易混淆, RUN, CMD, ENTRYPOINT.

RUN是在building image时会运行的指令, 在Dockerfile中可以写多条RUN指令.

CMD和ENTRYPOINT则是在运行container 时会运行的指令, 都只能写一条, 如果写了多条, 则最后一条生效.

CMD和ENTRYPOINT的区别是: 

CMD在运行时会被command覆盖, ENTRYPOINT不会被运行时的command覆盖, 但是也可以指定.

例如 : 

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

  --entrypoint=""            Overwrite the default entrypoint of the image

docker run postgres:9.3.5 psql 

这里的psql就是command, 将覆盖Dockerfile的CMD, 但是不会覆盖ENTRYPOINT.

如果要覆盖ENTRYPOINT, 那么可以在docker run运行时输入 --entrypoint="....".

CMD和ENTRYPOINT一般用于制作具备后台服务的image, 例如apache, database等. 在使用这种image启动container时, 自动启动服务.

这几条指令的详细用法 : 

RUN

RUN has 2 forms:

  • RUN <command> (the command is run in a shell - /bin/sh -c - shell form)
  • RUN ["executable", "param1", "param2"] (exec form)

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

Layering RUN instructions and generating commits conforms to the core concepts of Docker where commits are cheap and containers can be created from any point in an image's history, much like source control.

The exec form makes it possible to avoid shell string munging, and to RUN commands using a base image that does not contain /bin/sh.

Note: To use a different shell, other than '/bin/sh', use the exec form passing in the desired shell. For example, RUN ["/bin/bash", "-c", "echo hello"]

Note: The exec form is parsed as a JSON array, which means that you must use double-quotes (") around words not single-quotes (').

Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo", "$HOME" ].

The cache for RUN instructions isn't invalidated automatically during the next build. The cache for an instruction like RUN apt-get dist-upgrade -y will be reused during the next build. The cache for RUNinstructions can be invalidated by using the --no-cache flag, for example docker build --no-cache.

See the Dockerfile Best Practices guide for more information.

The cache for RUN instructions can be invalidated by ADD instructions. See below for details.

Known Issues (RUN)

  • Issue 783 is about file permissions problems that can occur when using the AUFS file system. You might notice it during an attempt to rm a file, for example. The issue describes a workaround.

CMD

The CMD instruction has three forms:

  • CMD ["executable","param1","param2"] (exec form, this is the preferred form)
  • CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
  • CMD command param1 param2 (shell form)

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the lastCMD will take effect.

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINTinstruction as well.

Note: If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD andENTRYPOINT instructions should be specified with the JSON array format.

Note: The exec form is parsed as a JSON array, which means that you must use double-quotes (") around words not single-quotes (').

Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo", "$HOME" ].

When used in the shell or exec formats, the CMD instruction sets the command to be executed when running the image.

If you use the shell form of the CMD, then the <command> will execute in /bin/sh -c:

FROM ubuntu
CMD echo "This is a test." | wc -

If you want to run your <command> without a shell then you must express the command as a JSON array and give the full path to the executable. This array form is the preferred format of CMD. Any additional parameters must be individually expressed as strings in the array:

FROM ubuntu
CMD ["/usr/bin/wc","--help"]

If you would like your container to run the same executable every time, then you should consider usingENTRYPOINT in combination with CMD. See ENTRYPOINT.

If the user specifies arguments to docker run then they will override the default specified in CMD.

Note: don't confuse RUN with CMDRUN actually runs a command and commits the result; CMDdoes not execute anything at build time, but specifies the intended command for the image.

ENTRYPOINT

ENTRYPOINT has two forms:

  • ENTRYPOINT ["executable", "param1", "param2"] (exec form, the preferred form)
  • ENTRYPOINT command param1 param2 (shell form)

There can only be one ENTRYPOINT in a Dockerfile. If you have more than one ENTRYPOINT, then only the last one in the Dockerfile will have an effect.

An ENTRYPOINT helps you to configure a container that you can run as an executable. That is, when you specify an ENTRYPOINT, then the whole container runs as if it was just that executable.

Unlike the behavior of the CMD instruction, The ENTRYPOINT instruction adds an entry command that willnot be overwritten when arguments are passed to docker run. This allows arguments to be passed to the entry point, i.e. docker run <image> -d will pass the -d argument to the entry point.

You can specify parameters either in the ENTRYPOINT JSON array (as in "like an exec" above), or by using a CMD instruction. Parameters in the ENTRYPOINT instruction will not be overridden by the docker runarguments, but parameters specified via a CMD instruction will be overridden by docker run arguments.

Like a CMD, you can specify a plain string for the ENTRYPOINT and it will execute in /bin/sh -c:

FROM ubuntu
ENTRYPOINT ls -l

For example, that Dockerfile's image will always take a directory as an input and return a directory listing. If you wanted to make this optional but default, you could use a CMD instruction:

FROM ubuntu
CMD ["-l"]
ENTRYPOINT ["ls"]

Note: The exec form is parsed as a JSON array, which means that you must use double-quotes (") around words not single-quotes (').

Note: Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo", "$HOME" ].

Note: It is preferable to use the JSON array format for specifying ENTRYPOINT instructions.

时间: 2024-08-31 22:34:54

Dockerfile RUN, CMD & ENTRYPOINT的相关文章

Docker Dockerfile详解

如何使用 Dockerfile用来创建一个自定义的image,包含了用户指定的软件依赖等.当前目录下包含Dockerfile,使用命令build来创建新的image,并命名为edwardsbean/centos6-jdk1.7: docker build -t edwardsbean/centos6-jdk1.7 . Dockerfile关键字 如何编写一个Dockerfile,格式如下: # CommentINSTRUCTION arguments FROM 基于哪个镜像 RUN 安装软件用

docker dockerFile 参数解释,及生成jdk+tomcat镜像实例

Dockfile是一种被Docker程序解释的脚本,Dockerfile由一条一条的指令组成,每条指令对应Linux下面的一条命令.Docker程序将这些Dockerfile指令翻译真正的Linux命令.Dockerfile有自己书写格式和支持的命令,Docker程序解决这些命令间的依赖关系,类似于Makefile.Docker程序将读取Dockerfile,根据指令生成定制的image.相比image这种黑盒子,Dockerfile这种显而易见的脚本更容易被使用者接受,它明确的表明image是

如何编写最佳的Dockerfile

本文讲的是如何编写最佳的Dockerfile[编者的话]Dockerfile的语法非常简单,然而如何加快镜像构建速度,如何减少Docker镜像的大小却不是那么直观,需要积累实践经验.这篇博客可以帮助你快速掌握编写Dockerfile的技巧. [深圳站|3天烧脑式Kubernetes训练营]培训内容包括:Kubernetes概述.架构.日志和监控,部署.自动驾驶.服务发现.网络方案等核心机制分析,进阶篇--Kubernetes调度工作原理.资源管理及源码分析等. 我已经使用Docker有一段时间了

Dockerfile 常用指令 - 每天5分钟玩转 Docker 容器技术(16)

是时候系统学习 Dockerfile 了. 下面列出了 Dockerfile 中最常用的指令,完整列表和说明可参看官方文档. FROM指定 base 镜像. MAINTAINER设置镜像的作者,可以是任意字符串. COPY将文件从 build context 复制到镜像.COPY 支持两种形式: COPY src dest COPY ["src", "dest"] 注意:src 只能指定 build context 中的文件或目录. ADD与 COPY 类似,从 b

Docker简单使用

安装 照着官方文档 http://docs.docker.com/installation/ubuntulinux/ 做吧. 一般就是: 更新内核: $ sudo apt-get update $ sudo apt-get install linux-image-generic-lts-raring linux-headers-generic-lts-raring 重启系统. 添加软件源的 KEY: $ sudo apt-key adv --keyserver hkp://keyserver.u

Dockerfile指令详解_docker

什么是Dockerfile Dockerfile是由一系列命令和参数构成的脚本,这些命令应用于基础镜像并最终创建一个新的镜像.它们简化了从头到尾的流程并极大的简化了部署工作.Dockerfile从FROM命令开始,紧接着跟随者各种方法,命令和参数.其产出为一个新的可以用于创建容器的镜像. 当你在使用 Docker构建镜像的时候,每一个命令都会在前一个命令的基础上形成一个新层.这些基础镜像可以用于创建新的容器.本篇文章将手把手教您如何从基础镜像,一步一步,一层一层的从Dockerfile构建容器的

Dockerfile best practices

使用Dockerfile创建image前, 建议遵循的一些规则. 1. container是阅后即焚式的, 即运行一次后, 如果停掉了, 那么重新运行一个(全新的).  这点和虚拟机有点不一样, 虚拟机有自己的东西, 可以存在虚拟机内部, 如果虚拟机停掉, 再起来, 在虚拟机运行过程中的变更是会被保留的. 但是container不一样, container产生的变更都会"消失", 除非挂载外部的volume并将变更写入外部volume.  所以设计时应该考虑到container的特性,

Dockerfile 命令介绍及实例

基础镜像可以用于创建Docker容器.镜像可以非常基础,仅仅包含操作系统:也可以非常丰富,包含灵巧的应用栈,随时可以发布.当你在使用Docker构建镜像的时候,每一个命令都会在前一个命令的基础上形成一个新层.这些基础镜像可以用于创建新的容器.本篇文章将手把手教您如何从基础镜像,一步一步,一层一层的从Dockerfile构建容器的过程. Docker简介 Docker项目提供了构建在Linux内核功能之上,协同在一起的的高级工具.其目标是帮助开发和运维人员更容易地跨系统跨主机交付应用程序和他们的依

DockerFile的编写和注意的一些知识点

CMD,RUN,ENTRYPOINT之类的差别. VOLUMN和-V之间的差别. EXPOSE和-P的对应等. 今天上午写了一个脚本,可以传参数进IMAGE,让启动的CONTAINER具有不同的行为. 另外是一些常用的方便命令: 删除所有正在运行的容器(先停止,再删除). docker stop $(docker ps -q) && docker rm $(docker ps -a -q) 启动镜像(包括端口映射,目录挂载-如果windows,那不是挂windows目录,是虚拟机目录,命令