docker ENV与RUN导出 [英] docker ENV vs RUN export
问题描述
假设我要组合这些命令
RUN command_1
ENV FOO bar
RUN command_2
进入
RUN command_1 && export FOO=bar && command_2
,想知道是否使用 RUN export $ c设置变量$ c>与
ENV
是等效的。
and was wondering if setting the variable with RUN export
vs ENV
was equivalent.
换句话说,Dockerfile中的这些命令之间有区别吗?
In other words, is there a difference between these commands in a Dockerfile?
ENV FOO bar
vs
RUN export FOO=bar
推荐答案
如问题684 ,导出
将不会在图像中持久存在。 (不要忘了每个Dockerfile指令都会生成一个中间容器,并提交到中间映像中:该映像不会保留导出的值)。
ENV
将:
As illustrated by issue 684, export
won't persist across images. (Don't forget that each Dockerfile directive will generate an intermediate container, committed into an intermediate image: that image won't preserve the exported value)
ENV
will:
从结果映像运行容器时,使用
ENV
设置的环境变量将保留。 >
您可以使用docker inspect
查看值,并使用docker run --env< key> =< value> ;
。
The environment variables set using
ENV
will persist when a container is run from the resulting image.
You can view the values usingdocker inspect
, and change them usingdocker run --env <key>=<value>
.
该问题说明了这一点:
RUN export PATH=$PATH:/foo/bar # from directly in builder
$ b $中运行export PATH = $ PATH:/ foo / bar# b
当我执行
docker run [img] bash -c'echo $ PATH'
时,它永远不会包含/ foo / bar
。
When I do
docker run [img] bash -c 'echo $PATH'
it never includes/foo/bar
.
尝试一下
创建一个新dockerfile包含:
Try it
Create a new dockerfile containing:
FROM centos:6
ENV FOO=foofoo
RUN export BAR=barbar
RUN export BAZ=bazbaz && echo "$FOO $BAR $BAZ"
然后构建它。最后一步的输出是:
Then build it. The output of the last step is:
Step 4/4 : RUN export BAZ=bazbaz && echo "$FOO $BAR $BAZ"
---> Running in eb66196b238d
foofoo bazbaz
您可以看到:
-
FOO
通过中间容器得以保留,这要归功于ENV
关键字; -
BAR
不会继续进行下一步,因为export
命令; -
BAZ
正确显示,因为该变量在同一容器上使用。
FOO
persists through intermediate containers, thanks to theENV
keyword;BAR
doesn't persist on the next step, because of theexport
command;BAZ
is correctly displayed because the variable is used on the same container.
这篇关于docker ENV与RUN导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!