FW: Dockerfile RUN, CMD & ENTRYPOINT
Dockerfile RUN, CMD & ENTRYPOINT
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 RUN
instructions 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 ENTRYPOINT
instruction as well.
Note: If
CMD
is used to provide default arguments for theENTRYPOINT
instruction, both theCMD
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
withCMD
.RUN
actually runs a command and commits the result;CMD
does 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 run
arguments, 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.
FW: Dockerfile RUN, CMD & ENTRYPOINT的更多相关文章
- Dockerfile RUN, CMD & ENTRYPOINT
Dockerfile RUN, CMD & ENTRYPOINT 在使用Dockerfile创建image时, 有几条指令比较容易混淆, RUN, CMD, ENTRYPOINT. RUN是在 ...
- Dockerfile 中 RUN, CMD, ENTRYPOINT 的区别
RUN 指令:用于指定 docker build 过程中要运行的命令. 语法格式: RUN <command> 或 RUN ["<executeable>" ...
- Dockerfile之CMD与Entrypoint使用要点
CMD与ENTRYPOINT都可以代表容器的启动命令,单丛语义上来理解,CMD是一个命令或者口令,而ENTRYPOINT则是一个入口(相当于容器启动时的入口),那么其实就可以理解为每当我们开启一个容器 ...
- [转]【docker】CMD ENTRYPOINT 区别
本文转自:https://blog.csdn.net/u010900754/article/details/78526443 昨天用Dockerfile来启动mongodb的集群,启动参数--repl ...
- docker学习笔记11:Dockerfile 指令 CMD介绍
我们知道,通过docker run 创建并启动一个容器时,命令的最后可以指定容器启动后在容器内立即要执行的指令,如: docker run -i -t ubunu /bin/bash //表示容器 ...
- k8s 传参给docker env command、args和dockerfile中的entrypoint、cmd之间的关系
[k8s]args指令案例-彻底理解docker entrypoint 需求: 搞个镜像,可以运行java -jar xxx.jar包,xxx.jar包名称要用参数传 思路1: 打对应运行ja ...
- k8s中command、args和dockerfile中的entrypoint、cmd之间的关系
当用户同时写了command和args的时候自然是可以覆盖DockerFile中ENTRYPOINT的命令行和参数,那么对于具体情况呢,比如仅仅写了command或者args的时候呢?完整的情况分类如 ...
- Dockerfile中CMD和ENTRYPOINT的区别
当启动一个容器时,CMD和ENTRYPOINT都可以用来执行启动命令.但它们的具体用法还是有一些区别: 1. Dockerfile必须至少指定CMD或者ENTRYPOINT其中的一个. 2. ENTR ...
- docker 15 dockerfile案例-CMD、ENTRYPOINT案例
CMD.ENTRYPOINT都是指定一个容器启动时要运行的命令.但是CMD会覆盖前面的参数,而ENTRYP会追加组合原来的参数 未完待续...
随机推荐
- html页面中js判断浏览器是否是IE浏览器及IE浏览器版本
HTML里: HTML代码中,在编写网页代码时,各种浏览器的兼容性是个必须考虑的问题,有些时候无法找到适合所有浏览器的写法,就只能写根据浏览器种类区别的代码,这时就要用到判断代码了.在HTML代码中, ...
- Java中的四种引用类型,强引用,软引用,弱引用,虚引用
对于Java中的垃圾回收机制来说,对象是否被回收的标准在于该对象是否被引用.因此,引用也是JVM进行内存管理的一个重要概念. Java中对象的引用一般有以下4种类型: 1强引用 2软引用 3弱引用 ...
- (3)Smali系列学习之Smali语法详解
数据类型 Dalvik字节码只有两种格式:基本类型和引用类型.对象和数组属于引用类型 语法 含义 V void,只用于返回值类型 Z boolean B byte S short C char I i ...
- JS高程3:事件
事件是JS和HTML交互的方式. 事件流 事件流是HTML文档接收事件的顺序.分为2个流派:事件冒泡流和事件捕捉流. 事件冒泡流 由内到外 事件捕捉流 由外到内 DOM事件流 事件处理程序 跨浏览器时 ...
- 使用 Bolt 实现 GridView 表格控件
用 Bolt 实现了一个表格控件: 1. 提供 Insert,Remove,Get,Set 接口,可以为表格增删数据: 2. 通过 ItemClass, ItemSetDataFunc 属性来指定显 ...
- 如何Vue-cli开始使用在Vue.js项目中启动TDD(测试驱动开发)
通常,使用测试驱动开发(TDD)最困难的部分是开始.你必须下载带有奇怪依赖项的软件包,让测试套件与你的构建系统协同工作,然后你必须弄清楚如何编写一个测试!难怪这么多的开发者在你提起它的时候就开始跑开了 ...
- MySQL集群系列1:2台机器搭建双主集群
先配置静态IP 2台机器mysql密码一样,最好在同一局域网内,最好在mysql刚安装时就配置好,后面有数据了不好同步. 本文实现了2台机器mysql数据同步成功: 配置my.cnf 先关闭防火墙 s ...
- table与html实例
*{ margin:0; padding:0; list-style-type:none;/*手动清楚空隙*/ font-size:12px; font-family:"微软雅黑" ...
- 实现Netty服务器与CocosCreate通信
尽量采用无锁化Netty通信处理棋牌房间逻辑 一,棋牌类服务器的特点 1,棋牌类不分区不分服 一般来说,棋牌游戏都是不分区不分服的.所以棋牌类服务器要满足随着用户量的增加而扩展的需要,所以需要设计Ga ...
- landa语法
sg_orm看当前sql信息 db.IsEnableLogEvent = true; db.LogEventStarting = (sql, pa) => { var t = 0; }; //出 ...