如何为Docker的环境变量指定文件 [英] How to specify a file for environment variables for docker
问题描述
我在代码中需要一些环境变量。
I have a few environment variables that I need inside of my code.
当我开始构建docker容器时,我希望将这些环境变量烘烤到我的容器中
And when I starts building a docker container, I wish to bake those environment variables into my docker container.
我知道dockerfile指令'ENV'确实做到了这一点,我想知道是否有一种方法可以指向文件而不是在文件内部写入文件。 Dockerfile
I understand that the dockerfile directive 'ENV' does exactly that, I wonder if there is a way to point to an file instead of writing it inside of the Dockerfile
推荐答案
AFAIK,没有这种方法可以在使用Dockerfile的构建步骤中使用文件注入环境变量。但是,在大多数情况下,人们最终还是使用 entrypoint
脚本&在 docker运行
或 docker-compose up
期间注入变量。
AFAIK, there is no such way to inject environment variables using a file during the build step using Dockerfile. However, in most cases, people do end up using an entrypoint
script & injecting variables during the docker run
or docker-compose up
.
如果有必要,您可能需要编写一个外壳包装程序,以动态更改 Dockerfile
中的值通过将键值对文本文件作为输入或使其如下所示,但需要将ENV文件名包含在Dockerfile中,如@thaJeztah在链接-
In case it's a necessity you might need to write a shell wrapper which will change the values in the Dockerfile
dynamically by taking a key-value pair text file as an input or make it something as below but the ENV file name need to be included in Dockerfile as suggested by @thaJeztah in link -
COPY my-env-vars /
RUN export $(cat my-env-vars | xargs)
这是一个未解决的问题-
https://github.com/moby/moby/issues/28617
It's an open issue - https://github.com/moby/moby/issues/28617
这篇关于如何为Docker的环境变量指定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!