如何取消设置“ ENV”在dockerfile中? [英] How to unset "ENV" in dockerfile?

查看:95
本文介绍了如何取消设置“ ENV”在dockerfile中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我必须在我的dockerfile中设置 http_proxy和 https_proxy ENV 。我现在想取消设置它们,因为还有一些构建过程无法通过代理完成。

For some certain reasons, I have to set "http_proxy" and "https_proxy" ENV in my dockerfile. I would like to now unset them because there are also some building process can't be done through the proxy.

# dockerfile

# ... some process

ENV http_proxy=http://...
ENV https_proxy=http://...

# ... some process that needs the proxy to finish

UNSET ENV http_proxy # how to I unset the proxy ENV here?
UNSET ENV https_proxy

# ... some process that can't use the proxy 


推荐答案

这取决于您要达到的效果。

It depends on what effect you are trying to achieve.

请注意,从务实的角度(即开发人员的实际讲话方式)而言,取消设置变量可能意味着两件事:将其从环境中删除或将变量设置为空值。从技术上讲,这是两个不同的操作。在实践中,尽管我还没有遇到过我要控制的软件区分环境中不存在的变量和环境中存在但设置为空值的变量的情况。我通常可以使用任何一种方法来获得相同的结果。

Note that, as a matter of pragmatics (i.e. how developers actually speak), "unsetting a variable" can mean two things: removing it from the environment, or setting the variable to an empty value. Technically, these are two different operations. In practice though I have not run into a case where the software I'm trying to control differentiates between the variable being absent from the environment, and the variable being present in the environment but set to an empty value. I generally can use either method to get the same result.

在这种情况下,您可以在 ENV VAR_NAME = 指向您要从中取消设置变量的Dockerfile中。语法说明:Docker允许 ENV 使用两种语法:此 ENV VAR = 1 ENV VAR 1 。您可以使用空格或等号将变量名与值分开。如果要通过将变量设置为空值来取消设置变量,则必须使用等号语法,否则在生成时会出错。

For this case, you can use ENV VAR_NAME= at the point in your Dockerfile from which you want to unset the variable. Syntactic note: Docker allows two syntaxes for ENV: this ENV VAR=1 is the same as ENV VAR 1. You can separate the variable name from the value with a space or an equal sign. When you want to "unset" a variable by setting it to an empty value you must use the equal sign syntax or you get an error at build time.

例如,您可以这样做:

ENV NOT_SENSITIVE some_value
RUN something

ENV NOT_SENSITIVE=
RUN something_else

某物运行时, NOT_SENSITIVE 设置为 some_value 。当 something_else 运行时, NOT_SENSITIVE 设置为空字符串。

When something runs, NOT_SENSITIVE is set to some_value. When something_else runs, NOT_SENSITIVE is set to the empty string.

重要的是要注意,将设置为NOT_SENSITIVE 作为shell命令将不会影响此shell中执行的内容。示例:

It is important to note that doing unset NOT_SENSITIVE as a shell command will not affect anything else than what executes in this shell. Here's an example:

ENV NOT_SENSITIVE some_value
RUN unset NOT_SENSITIVE && printenv NOT_SENSITIVE || echo "does not exist"

RUN printenv NOT_SENSITIVE

第一个 RUN 将打印不存在,因为当<$时未设置 NOT_SENSITIVE c $ c> printenv 会执行,因为未设置 printenv 返回一个非零的退出代码,该代码导致 echo 执行。第二个 RUN 不受第一个 RUN 未设置的影响c>。它将在屏幕上显示 some_value

The first RUN will print does not exist because NOT_SENSITIVE is unset when printenv executes and because it is unset printenv returns a non-zero exit code which causes the echo to execute. The second RUN is not affected by the unset in the first RUN. It will print some_value to the screen.


但是如果我需要从环境中删除变量,而不只是将其设置为空值?

But what if I need to remove the variable from the environment, not just set it to an empty value?

在这种情况下,使用 ENV VAR_NAME = 不起作用。我不知道有什么办法告诉Docker从现在开始,您必须从环境中删除此变量,而不仅仅是将其设置为空值。

In this case using ENV VAR_NAME= won't work. I don't know of any way to tell Docker "from this point on, you must remove this variable from the environment, not just set it to an empty value".

如果您仍然想使用 ENV 设置变量,则必须在每个 RUN 中启动您希望使用 unset VAR_NAME 取消设置变量,这将为特定于 RUN 取消设置

If you still want to use ENV to set your variable, then you'll have to start each RUN in which you want the variable to be unset with unset VAR_NAME, which will unset it for that specific RUN only.

假设变量包含一个秘密,并且该层可能落入不应拥有该秘密的人们的手中。在这种情况下,您不能使用 ENV 设置变量。使用 ENV 设置的变量将被烘焙到其应用的层中,并且不能从这些层中删除。特别是(假设变量名为 SENSITIVE )正在运行

Suppose that variable contains a secret and the layer could fall into the hands of people who should not have the secret. In this case you CANNOT use ENV to set the variable. A variable set with ENV is baked into the layers to which it applies and cannot be removed from those layers. In particular, (assuming the variable is named SENSITIVE) running

RUN unset SENSITIVE

不执行任何操作将其从上面的取消设置命令仅从的shell进程中删除 SENSITIVE 。 RUN 开始。它仅影响该外壳。它不会影响 CMD ENTRYPOINT 或通过运行提供的任何命令生成的外壳docker在命令行运行

does not do anything to remove it from the layer. The unset command above only removes SENSITIVE from the shell process that RUN starts. It affects only that shell. It won't affect shells spawned by CMD, ENTRYPOINT, or any command provided through running docker run at the command line.

为了防止图层包含秘密,我将使用 docker build --secret = RUN --mount = type = secret ... 。例如,假设我已将机密存储在名为敏感的文件中,那么我可以像这样运行 RUN

In order to prevent the layers from containing the secret, I would use docker build --secret= and RUN --mount=type=secret.... For instance, assuming that I've stored my secret in a file named sensitive, I could have a RUN like this:

RUN --mount=type=secret,id=sensitive,target=/root/sensitive \
 export SENSITIVE=$(cat /root/sensitive) \
 && [[... do stuff that requires SENSITIVE ]] \

请注意,该命令赋予 RUN 不必以 unset SENSITIVE 结尾。由于过程及其环境的方式在管理过程中,在 RUN 产生的外壳中设置 SENSITIVE 对该外壳本身产生的作用没有任何影响。此外壳程序中的环境更改不会影响未来的外壳程序,也不会影响Docker将其烘焙到其创建的层中。

Note that the command given to RUN does not need to end with unset SENSITIVE. Due to the way processes and their environments are managed, setting SENSITIVE in the shell spawned by RUN does not have any effect beyond what that shell itself spawns. Environment changes in this shell won't affect future shells nor will it affect what Docker bakes into the layers it creates.

然后可以使用以下版本运行构建:

Then the build can be run with:

$ DOCKER_BUILDKIT=1 docker build --secret id=secret,src=path/to/sensitive [...]

docker build 命令的环境需要 DOCKER_BUILDKIT = 1 使用BuildKit,因为只有在Docker使用BuildKit生成映像的情况下,这种传递秘密的方法才可用。

The environment for the docker build command needs DOCKER_BUILDKIT=1 to use BuildKit because this method of passing secrets is only available if Docker uses BuildKit to build the images.

这篇关于如何取消设置“ ENV”在dockerfile中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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