在Docker的构建时创建动态环境变量 [英] Create dynamic environment variables at build time in Docker

查看:971
本文介绍了在Docker的构建时创建动态环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的具体用例是,我想组织一些关于容器正在运行的EC2实例的数据,并使其可用作环境变量。我想在容器建成时这样做。

My specific use case is that I want to organize some data about the EC2 instance a container is running on and make i available as an environment variable. I'd like to do this when the container is built.

我希望能够像 ENV VAR_NAME $(./ script / that / gets / var)在我的Docker文件中,但不出意料的是不起作用(只需要获得字符串 $(./ script ... )。

I was hoping to be able to do something like ENV VAR_NAME $(./script/that/gets/var) in my Dockerfile, but unsurprisingly that does not work (you just get the string $(./script...).

我应该提到,我知道 docker运行--env ... 会这样做,但我特意希望将其内置到容器中。

I should mention that I know the docker run --env... will do this, but I specifically want it to be built into the container.

我是否缺少一些明显的东西?这是否可能?

Am I missing something obvious? Is this even possible?

推荐答案

Docker v1.9或更新版本

Docker v1.9 or newer

如果您使用Docker v1.9或更新的,这可以通过支持构建时间参数来实现。参数在 Dockerfile 中由使用ARG语句

If you are using Docker v1.9 or newer, this is possible via support for build time arguments. Arguments are declared in the Dockerfile by using the ARG statement.

ARG REQUIRED_ARGUMENT
ARG OPTIONAL_ARGUMENT=default_value

当您稍后使用 docker build 构建您的图像时,可以通过标志 - build-arg ,如 docker docs 所述。

When you later actually build your image using docker build you can pass arguments via the flag --build-arg as described in the docker docs.

$ docker build --build-arg REQUIRED_ARGUMENT=this-is-required .

请注意,建议将使用构建时间变量密码或秘密,如密钥或凭据。

Please note that it is not recommended to use build-time variables for passwords or secrets such as keys or credentials.

此外,构建时变量可能具有对缓存有很大的影响。因此,Dockerfile应该非常小心,以便尽可能地利用缓存,并加快构建过程。

Furthermore, build-time variables may have great impact on caching. Therefore the Dockerfile should be constructed with great care to be able to utilize caching as much as possible and therein speed up the building process.

编辑:docker从 leedm777:s answer

如果您在1.9之前使用Docker版本,则 ARG / - build-arg 方法是不可能的。您在构建期间无法解决此类信息,因此您必须将它们作为参数传递给 docker运行命令。

If you are using a Docker-version before 1.9, the ARG/--build-arg approach was not possible. You couldn't resolve this kind of info during the build so you had to pass them as parameters to the docker run command.

Docker 图像在时间上是一致的,而容器可以被调整并被视为丢弃进程。

Docker images are to be consistent over time whereas containers can be tweaked and considered as "throw away processes".

  • More info about ENV
  • A docker discussion about dynamic builds

这个问题的旧解决方案是使用模板。这不是一个整洁的解决方案,但是当时是很少的可行选择之一。 (灵感来自此讨论)。

The old solution to this problem was to use templating. This is not a neat solution but was one of very few viable options at the time. (Inspiration from this discussion).


  1. 将所有动态数据保存在json或yaml文件中

  2. 创建一个docker文件template,其中动态可以稍后扩展

  3. 编写一个脚本,使用您熟悉的模板库从配置数据创建Dockerfile

这篇关于在Docker的构建时创建动态环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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