在构建期间将环境变量传递给Dockerfile [英] Pass environment variables to Dockerfile during build time

查看:58
本文介绍了在构建期间将环境变量传递给Dockerfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,是否可以在构建时将环境变量传递给dockerfile,以便在构建期间安装一些需要身份验证才能获取的文件.

Is it possible to pass an environment variable to a dockerfile at build time, for instance, in order to install some files during the build that require authentication to fetch.

推荐答案

此方法将实现您想要的功能,但不建议用于秘密(秘密很难).

This method will achieve what you want, but is not recommended for secrets (secrets are hard).

您可以使用Build Args.这个想法是您在Dockerfile中放置一个 ARG 指令,然后在Dockerfile中对其进行引用.

You can use Build Args. The idea is that you place an ARG instruction in your Dockerfile which is then referenceable within the Dockerfile.

FROM alpine:3.3
ARG some-thing=<optional default value>

稍后您可以在Dockerfile中使用此变量,例如:

You can use this variable later in the Dockerfile like:

...
RUN echo $some-thing
...

然后,在构建映像时,可以将 ARG 传递到 docker build 中,例如:

Then when you build the image, you can pass the ARG into docker build like:

$ docker build --build-arg some-thing=some-value .

这会将"some-value"作为 ARG "something"传递给Docker构建过程.

This will pass 'some-value' into the Docker build process as the ARG 'some-thing'.

你会得到

echo $some-thing
"some-value"

您的 ARG 的值不会 not 保留在最终图像中.因此,您不必担心密码等会泄漏到最终图像中.但是,Docker仍然不建议将其作为传递秘密的方法.

The values of your ARGs are not persisted in the end image though. So you don't have to worry about passwords etc leaking into the final image. However Docker still do not recommend it as a way of passing secrets.

这篇关于在构建期间将环境变量传递给Dockerfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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