Dockerfile RUN, CMD & ENTRYPOINT
2016-08-26 14:55
633 查看
转自: http://blog.163.com/digoal@126/blog/static/163877040201410411715832/
在使用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:
The
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
Layering
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
using a base image that does not contain
Note: To use a different shell, other than '/bin/sh', use the exec form passing in the desired shell. For example,
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,
not do variable substitution on
If you want shell processing then either use the shell form or execute a shell directly, for example:
The cache for
isn't invalidated automatically during the next build. The cache for an instruction like
4000
g the next build. The cache for
can be invalidated by using the
for example
See the
Practices guide for more information.
The cache for
can be invalidated by
See below for details.
Issue 783 is about file permissions problems that can occur
when using the AUFS file system. You might notice it during an attempt to
file, for example. The issue describes a workaround.
The
forms:
There can only be one
in a
more than one
last
The main purpose of a
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
as well.
Note: If
used to provide default arguments for the
both the
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,
not do variable substitution on
If you want shell processing then either use the shell form or execute a shell directly, for example:
When used in the shell or exec formats, the
sets the command to be executed when running the image.
If you use the shell form of the
then the
in
If you want to run your
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
additional parameters must be individually expressed as strings in the array:
If you would like your container to run the same executable every time, then you should consider using
combination with
If the user specifies arguments to
Note: don't confuse
runs a command and commits the result;
not execute anything at build time, but specifies the intended command for the image.
ENTRYPOINT has two forms:
There can only be one
a
than one
the last one in the
have an effect.
An
a container that you can run as an executable. That is, when you specify an
then the whole container runs as if it was just that executable.
Unlike the behavior of the
The
an entry command that willnot be overwritten when arguments are passed to
to the entry point.
You can specify parameters either in the
array (as in "like an exec" above), or by using a
Parameters in the
will not be overridden by the
will be overridden by
Like a
plain string for the
it will execute in
For example, that
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
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,
not do variable substitution on
If you want shell processing then either use the shell form or execute a shell directly, for example:
Note: It is preferable to use the JSON array format for specifying
在使用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
RUNinstruction 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
RUNinstructions 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
RUNcommands
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
RUNinstructions
isn't invalidated automatically during the next build. The cache for an instruction like
RUN apt-get dist-upgrade -ywill be reused durin
4000
g the next build. The cache for
RUNinstructions
can be invalidated by using the
--no-cacheflag,
for example
docker build --no-cache.
See the
DockerfileBest
Practices guide for more information.
The cache for
RUNinstructions
can be invalidated by
ADDinstructions.
See below for details.
Known Issues (RUN)
Issue 783 is about file permissions problems that can occurwhen using the AUFS file system. You might notice it during an attempt to
rma
file, for example. The issue describes a workaround.
CMD
The CMDinstruction 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
CMDinstruction
in a
Dockerfile. If you list
more than one
CMDthen only the
last
CMDwill take effect.
The main purpose of a
CMDis
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
CMDis
used to provide default arguments for the
ENTRYPOINTinstruction,
both the
CMDand
ENTRYPOINTinstructions
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
CMDinstruction
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 using
ENTRYPOINTin
combination with
CMD. See ENTRYPOINT.
If the user specifies arguments to
docker runthen they will override the default specified in
CMD.
Note: don't confuse
RUNwith
CMD.
RUNactually
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
ENTRYPOINTin
a
Dockerfile. If you have more
than one
ENTRYPOINT, then only
the last one in the
Dockerfilewill
have an effect.
An
ENTRYPOINThelps 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
CMDinstruction,
The
ENTRYPOINTinstruction 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> -dwill pass the
-dargument
to the entry point.
You can specify parameters either in the
ENTRYPOINTJSON
array (as in "like an exec" above), or by using a
CMDinstruction.
Parameters in the
ENTRYPOINTinstruction
will not be overridden by the
docker runarguments, but parameters specified via a
CMDinstruction
will be overridden by
docker runarguments.
Like a
CMD, you can specify a
plain string for the
ENTRYPOINTand
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
CMDinstruction:
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
ENTRYPOINTinstructions.
相关文章推荐
- Dockerfile RUN, CMD & ENTRYPOINT
- Dockerfile中ENTRYPOINT 和 CMD的区别以及RUN的作用
- Dockerfile中CMD和ENTRYPOINT的区别
- Dockerfile文件中的CMD和ENTRYPOINT指令差异对比
- Dockerfile中CMD和ENTRYPOINT的区别
- Dockerfile文件中CMD和ENTRYPOINT指令差异对比
- Dockerfile创建自定义Docker镜像以及CMD与ENTRYPOINT指令的比较
- Dockerfile创建自定义Docker镜像以及CMD与ENTRYPOINT指令的比较
- Dockerfile中CMD和ENTRYPOINT的用法
- Dockerfile中的ENTRYPOINT和CMD指令详解与比较
- Dockerfile中的cmd和entrypoint
- Dockerfile中CMD和ENTRYPOINT的组合用法
- Dockerfile中CMD和ENTRYPOINT命令详解
- Dockerfile 中的 CMD 与 ENTRYPOINT
- Dockerfile中RUN和CMD的区别及它们各自的作用
- Dockerfile中ENTRYPOINT 和 CMD的区别
- Dockerfile中CMD和ENTRYPOINT的区别
- Dockerfile中CMD与ENTRYPOINT的区别
- 论docker中 CMD 与 ENTRYPOINT 的区别(转)
- NIOS II IDE编译出错:no file name for '-include'。Quartus ii中的.tcl文件run不起来