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

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

问题描述

假设我想组合这些命令

运行命令_1ENV FOO吧运行命令_2

进入

RUN command_1 &&出口 FOO=bar &&命令_2

并且想知道使用 RUN exportENV 设置变量是否等效.

换句话说,Dockerfile 中的这些命令有区别吗?

ENV FOO 栏

对比

RUN export FOO=bar

解决方案

如图所示 issue 684, export 不会跨图像持久化.(不要忘记,每个 Dockerfile 指令都会生成一个中间容器,提交到一个中间映像中:该映像不会保留导出的值)
ENV 将:

<块引用>

使用 ENV 设置的环境变量将在从生成的映像运行容器时持续存在.
您可以使用 docker inspect 查看这些值,并使用 docker run --env <key>=<value> 更改它们.

问题说明了这一点:

RUN export PATH=$PATH:/foo/bar # from 直接在构建器中

<块引用>

当我执行 docker run [img] bash -c 'echo $PATH' 时,它从不包含 /foo/bar.

试试吧

创建一个新的 dockerfile 包含:

FROM centos:6ENV FOO=foofooRUN 导出 BAR=barbar运行出口 BAZ=bazbaz &&回声$FOO $BAR $BAZ"

然后构建它.最后一步的输出是:

Step 4/4 : RUN export BAZ=bazbaz &&回声$FOO $BAR $BAZ"--->在 eb66196b238d 中运行foofoo bazbaz

你可以看到:

  • FOO 在中间容器中持续存在,这要归功于 ENV 关键字;
  • BAR 在下一步不会持久化,因为 export 命令;
  • BAZ 正确显示,因为变量用于同一个容器.

Let's say I want combine these commands

RUN command_1
ENV FOO bar
RUN command_2

into

RUN command_1 && export FOO=bar && command_2

and was wondering if setting the variable with RUN export vs ENV was equivalent.

In other words, is there a difference between these commands in a Dockerfile?

ENV FOO bar

vs

RUN export FOO=bar

解决方案

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:

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>.

The issue was illustrating that with:

RUN export PATH=$PATH:/foo/bar # from directly in builder

When I do docker run [img] bash -c 'echo $PATH' it never includes /foo/bar.

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

You can see:

  • 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天全站免登陆