Dockerfile中RUN和CMD之间的区别 [英] Difference between RUN and CMD in a Dockerfile
问题描述
对于何时应使用 CMD
和 RUN
感到困惑。例如,要执行bash / shell命令(即 ls -la
),我将始终使用 CMD
或是否存在我将使用 RUN
的情况?试图了解有关这两个类似的 Dockerfile
指令的最佳实践。
I'm confused about when should I use CMD
vs RUN
. For example, to execute bash/shell commands (i.e. ls -la
) I would always use CMD
or is there a situation where I would use RUN
? Trying to understand the best practices about these two similar Dockerfile
directives.
推荐答案
RUN 是映像构建步骤,即 RUN
命令将被提交到容器映像。一个Dockerfile可以具有许多 RUN
步骤,这些步骤彼此叠加以构建映像。
RUN is an image build step, the state of the container after a RUN
command will be committed to the container image. A Dockerfile can have many RUN
steps that layer on top of one another to build the image.
CMD 是启动构建映像时容器默认执行的命令。 Dockerfile将仅使用最后定义的 CMD
。使用 docker启动$ image $ other_command
启动容器时,可以覆盖 CMD
。
CMD is the command the container executes by default when you launch the built image. A Dockerfile will only use the final CMD
defined. The CMD
can be overridden when starting a container with docker run $image $other_command
.
ENTRYPOINT 与 CMD
也密切相关,并且可以修改容器启动图像的方式。
ENTRYPOINT is also closely related to CMD
and can modify the way a container starts an image.
这篇关于Dockerfile中RUN和CMD之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!