docker ENV与RUN导出 [英] docker ENV vs RUN export

查看:196
本文介绍了docker ENV与RUN导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要组合这些命令

RUN command_1
ENV FOO bar
RUN command_2

进入

RUN command_1 && export FOO=bar && command_2

,想知道是否使用 RUN export 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 using docker inspect, and change them using docker 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 the ENV keyword;
  • BAR doesn't persist on the next step, because of the export command;
  • BAZ is correctly displayed because the variable is used on the same container.

这篇关于docker ENV与RUN导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆