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 CMD. RUN 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.

目录
相关文章
|
弹性计算 中间件 大数据
什么是容器计算服务 ACS?
全球首款容器计算服务ACS(Alibaba Cloud Container Compute Service)
1613 2
|
存储 前端开发 API
基于SpringBoot+Vue的智能在线考试系统的设计与实现
基于SpringBoot+Vue的智能在线考试系统的设计与实现
4583 0
|
Kubernetes 安全 网络协议
【K8S系列】深入解析k8s网络插件—Calico
【K8S系列】深入解析k8s网络插件—Calico
6475 0
|
12月前
|
存储 弹性计算 缓存
阿里云 ECS 按量付费价格详解:1 小时收费标准与多配置报价(今年最新)
在云服务器使用场景中,临时测试、活动扩容、短期项目部署等需求,往往不需要长期锁定资源,此时 “按量付费” 模式凭借 “按需使用、按小时结算” 的灵活性,成为众多用户的首选。本文结合阿里云 今年最新计费规则(含节省停机模式、地域差价调整)与实例规格迭代信息,详细梳理不同配置(2 核 2G 至 8 核 32G)的 ECS 按量付费 1 小时价格、计费逻辑及成本优化方案,为短期资源需求提供精准成本参考。
|
人工智能 Prometheus 监控
监控vLLM等大模型推理性能
本文将深入探讨 AI 推理应用的可观测方案,并基于 Prometheus 规范提供一套完整的指标观测方案,帮助开发者构建稳定、高效的推理应用。
3256 169
监控vLLM等大模型推理性能
|
存储 安全 API
Typora介绍、安装、图床设置和使用
Typora是一款简洁高效的Markdown编辑器,支持实时预览、数学公式编辑、表格制作与多格式导出,适用于写作、学术与技术文档。本文详细介绍其安装、激活、主题设置、图床配置(PicGo+SM.MS/COS/OSS)及常用功能使用,助你高效搭建写作环境。
2642 3
Typora介绍、安装、图床设置和使用
|
缓存 人工智能 算法
Nvidia_Mellanox_CX5和6DX系列网卡_RDMA_RoCE_无损和有损_DCQCN拥塞控制等技术简介-一文入门RDMA和RoCE有损无损
Nvidia_Mellanox_CX5和6DX系列网卡_RDMA_RoCE_无损和有损_DCQCN拥塞控制等技术简介-一文入门RDMA和RoCE有损无损
5417 0
|
关系型数据库 MySQL Linux
宝塔面板BT Composer安装及使用教程
本文介绍,相信你已经掌握了在宝塔面板上安装和使用 Composer 的方法。希望这些内容能对你有所帮助,祝你在开发过程中一切顺利!
3958 2
|
Kubernetes 算法 调度
Kubernetes的灵魂核心:kube-scheduler
本文介绍了Kubernetes中关键组件kube-scheduler的工作原理,详细解释了其通过预选和优选过程为Pod选择合适节点的机制,并提供了一个简化的Python示例来模拟这一过程,帮助读者更好地理解和管理Kubernetes集群。
|
小程序 API 网络安全
小程序中的合法域名的作用及条件有哪些?
小程序中的合法域名的作用及条件有哪些?
1495 1