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 run $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屋!