Dockerfile中RUN CD和WORKDIR之间的区别 [英] difference between RUN cd and WORKDIR in Dockerfile
问题描述
就Docker的工作方式而言, RUN cd /
和 WORKDIR /
之间是否有区别?
In terms of the way Docker works, is there any difference between RUN cd /
and WORKDIR /
inside a Dockerfile?
推荐答案
RUN cd /
绝对没有作用。 WORKDIR /
更改工作目录以供将来使用。
RUN cd /
does absolutely nothing. WORKDIR /
changes the working directory for future commands.
每个 RUN
命令在新的外壳和新的环境(以及技术上是新的容器,尽管您通常不会注意到这一点)中运行。 ENV
和 WORKDIR
指令会影响它的启动方式。如果您有一个 RUN
步骤仅更改目录,则在shell退出时会丢失,下一步将在最新的 WORKDIR中启动
图片。
Each RUN
command runs in a new shell and a new environment (and technically a new container, though you won't usually notice this). The ENV
and WORKDIR
directives before it affect how it starts up. If you have a RUN
step that just changes directories, that will get lost when the shell exits, and the next step will start in the most recent WORKDIR
of the image.
FROM busybox
WORKDIR /tmp
RUN pwd # /tmp
RUN cd / # no effect, resets after end of RUN line
RUN pwd # still /tmp
WORKDIR /
RUN pwd # /
RUN cd /tmp && pwd # /tmp
RUN pwd # /
(出于相同的原因, RUN export
并没有执行超过当前Dockerfile指令和 RUN。
或非标准 RUN源
不会设置环境变量。)
(For the same reason, RUN export
doesn't do anything that outlives the current Dockerfile instructions, and RUN .
or the non-standard RUN source
won't cause environment variables to be set.)
这篇关于Dockerfile中RUN CD和WORKDIR之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!