当前位置: 首页 > article >正文

学技术学英文:docker中RUN, CMD, and ENTRYPOINT用法和区别

RUN

在构建镜像时执行命令,生成新的镜像层

构建镜像时

安装软件包、设置环境等

不适用于容器启动时

RUN apt-get update && apt-get install -y curl
RUN mkdir -p /app

CMD

定义容器启动时的默认命令和参数

容器启动时

提供默认命令和参数

可以被 docker run 命令行中的其他命令覆盖

CMD ["echo", "Hello, World!"]
CMD param1 param2

ENTRYPOINT

配置容器启动时总是会执行的命令

容器启动时

确保容器总是执行特定程序

不会被 docker run 命令行中的其他命令覆盖

ENTRYPOINT ["echo"]
ENTRYPOINT ["executable", "param1", "param2"]

组合使用

利用 ENTRYPOINT 确定主要执行程序,CMD 提供默认参数

容器启动时

确保执行主要程序并提供默认参数

CMD 默认参数可被覆盖,ENTRYPOINT 始终执行

ENTRYPOINT ["echo"]
CMD ["Hello, World!"]
docker run myimage "Goodbye"

Docker’s flexibility and robustness as a containerization tool come with a complexity that can be daunting. Multiple methods are available to accomplish similar tasks, and users must understand the pros and cons of the available options to choose the best approach for their projects.

One confusing area concerns the RUNCMD, and ENTRYPOINT Dockerfile instructions. In this article, we will discuss the differences between these instructions and describe use cases for each.

RUN

The RUN instruction is used in Dockerfiles to execute commands that build and configure the Docker image. These commands are executed during the image build process, and each RUN instruction creates a new layer in the Docker image. For example, if you create an image that requires specific software or libraries installed, you would use RUN to execute the necessary installation commands.

The following example shows how to instruct the Docker build process to update the apt cache and install Apache during an image build:

RUN apt update && apt -y install apache2

RUN instructions should be used judiciously to keep the image layers to a minimum, combining related commands into a single RUN instruction where possible to reduce image size.

CMD

The CMD instruction specifies the default command to run when a container is started from the Docker image. If no command is specified during the container startup (i.e., in the docker run command), this default is used. CMD can be overridden by supplying command-line arguments to docker run.

CMD is useful for setting default commands and easily overridden parameters. It is often used in images as a way of defining default run parameters and can be overridden from the command line when the container is run. 

For example, by default, you might want a web server to start, but users could override this to run a shell instead:

CMD ["apache2ctl", "-DFOREGROUND"]

Users can start the container with docker run -it <image> /bin/bash to get a Bash shell instead of starting Apache.  

ENTRYPOINT

The ENTRYPOINT instruction sets the default executable for the container. Any arguments supplied to the docker run command are appended to the ENTRYPOINT command.

Note: Use ENTRYPOINT when you need your container to always run the same base command, and you want to allow users to append additional commands at the end. One caveat is that ENTRYPOINT can be overridden on the docker run command line by supplying the --entrypoint flag.

ENTRYPOINT is particularly useful for turning a container into a standalone executable. For example, suppose you are packaging a custom script that requires arguments (e.g., “my_script extra_args”). In that case, you can use ENTRYPOINT to always run the script process (“my_script”) and then allow the image users to specify the “extra_args” on the docker run command line. You can do the following:

ENTRYPOINT ["my_script"]

Combining CMD and ENTRYPOINT

The CMD instruction can be used to provide default arguments to an ENTRYPOINT if it is specified in the exec form. This setup allows the entry point to be the main executable and CMD to specify additional arguments that can be overridden by the user.

For example, you might have a container that runs a Python application where you always want to use the same application file but allow users to specify different command-line arguments:

ENTRYPOINT ["python", "/app/my_script.py"]
CMD ["--default-arg"]

Running docker run myimage --user-arg executes python /app/my_script.py --user-arg.

The following table provides an overview of these commands and use cases.

Command description and use cases

Command

Description

Use Case

CMD

Defines the default executable of a Docker image. It can be overridden by docker run arguments.

Utility images allow users to pass different executables and arguments on the command line.

ENTRYPOINT

Defines the default executable. It can be overridden by the “--entrypoint”  docker run arguments.

Images built for a specific purpose where overriding the default executable is not desired.

RUN

Executes commands to build layers.

Building an image


http://www.kler.cn/a/466113.html

相关文章:

  • 推荐系统的三道菜
  • ES6中定义私有属性详解
  • 算法题(25):只出现一次的数字(三)
  • Rockect基于Dledger的Broker主从同步原理
  • 如何使用Termux 通过 SSH 连接到远程服务器
  • OWASP ZAP之API 请求基础知识
  • 大数据-263 实时数仓 - Canal 工作原理 工作流程 MySQL Binglog基本介绍
  • 基于PHP的智能健康管理系统设计与实现
  • XIAO ESP32 S3网络摄像头——2视频获取
  • SQL-【DQL+DCL】
  • 第13章 汇编语言--- 实践项目:简单的计算器
  • 壁纸样机神器,可以导出高清图片吗?
  • react native 屏幕适配方案,设计图750px
  • MagicMirror 1.0.0 | 基于AI的面部替换、发型和服装搭配应用,无需GPU支持
  • C语言指针-冒泡排序之旅
  • vue cli更新遇到的问题(vue -V查询版本号不变的问题)
  • Appium 2.0:移动自动化测试的革新之旅
  • OkHttp接口自动化测试
  • 比Qt更适合小公司的C++界面开发框架wxWidgets
  • Large-Vision-Language-Models-LVLMs--info:deepseek-vl模型
  • K8s集群平滑升级(Smooth Upgrade of K8S Cluster)
  • Geoserver修行记-后端调用WMS/WMTS服务无找不到图层Could not find layer
  • 【每日学点鸿蒙知识】自定义键盘光标、Cavas绘制、XComponent触发键盘抬起等
  • 三维扫描技术如何让历史文物‘活’起来
  • 如何使用axios设置响应拦截器和请求拦截器
  • Web 开发入门:从前端到后端的全栈开发探索