gpt4 book ai didi

docker - 获取 Dockerfile 中的环境变量值

转载 作者:IT老高 更新时间:2023-10-28 12:32:44 31 4
gpt4 key购买 nike

我正在为一个 ruby​​ 应用程序构建一个容器。我的应用程序的配置包含在环境变量中(使用 dotenv 加载到应用程序中)。

其中一个配置变量是应用程序的公共(public) ip,它在内部用于建立链接。我需要添加一个 dnsmasq 条目,将这个 ip 指向容器内的 127.0.0.1,这样它就可以获取应用程序的链接,就好像它没有被容器化一样。

因此,我试图在我的 Dockerfile 中设置一个 ENV,它将环境变量传递给容器。

我尝试了一些东西。

ENV REQUEST_DOMAIN $REQUEST_DOMAIN
ENV REQUEST_DOMAIN `REQUEST_DOMAIN`

不过,一切都传递“REQUEST_DOMAIN”字符串而不是环境变量的值。有没有办法将环境变量值从主机传递到容器?

最佳答案

您应该使用 ARG directive在用于此目的的 Dockerfile 中。

The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg <varname>=<value> flag.

所以你的 Dockerfile 会有这一行:

ARG request_domain

或者如果您更喜欢默认值:

ARG request_domain=127.0.0.1

现在您可以在 Dockerfile 中引用此变量:

ENV request_domain=$request_domain

然后你将像这样构建你的容器:

$ docker build --build-arg request_domain=mydomain Dockerfile


注意 1: 如果您引用了 ARG,您的图像将不会构建。在您的 Dockerfile 中,但在 --build-arg 中排除它.

注意 2: 如果用户指定了未在 Dockerfile 中定义的构建参数,则构建会输出警告:

[Warning] One or more build-args [foo] were not consumed.

关于docker - 获取 Dockerfile 中的环境变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19537645/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com