如何从.env文件中传递Dockerfile中的Env Value?
我的目录结构看起来像这样。
|
| --- dockerfile
| ---- .ENV
.env
文件的内容如下。
VERSION=1.2.0
DATE=2022-05-10
我想访问版本
和日期
作为构建时间和运行时间的环境变量。因此,env
是我应该使用的。我知道。 我该怎么做?
我尝试在dockerfile
中使用run
命令喜欢
RUN export $(cat .env)
,但是,只能在运行时访问它而不能构建时间。 那么,如何使用env
来实现这一目标?
我可以手动这样做
ENV VERSION 1.2.0
ENV DATE 2022-05-10
,但是当我有很多环境变量时,它效率低下。
ps i不能使用docker-compose
,因为kubernetes Pods将使用图像,所以。
My directory structure looks like this.
|
|
--- Dockerfile
| --- .env
Content of .env
file looks like this.
VERSION=1.2.0
DATE=2022-05-10
I want to access VERSION
and DATE
as environment variable both during build time and run time. So ENV
is the one I should use. I know that.
How exactly can I do that ?
I tried using RUN
command in Dockerfile
like
RUN export $(cat .env)
But, it can only be accessed during runtime and not build time.
So, how can this be achieved with ENV
?
I can do it manually like
ENV VERSION 1.2.0
ENV DATE 2022-05-10
But, it is inefficient when I have many environment variables.
P.S. I cannot use docker-compose
because the image is going to be used by kubernetes pods, so.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您首先可以将这些变量作为环境变量导出到bash中
,然后使用
- build-arg
flag将它们传递到docker build toy dockerfile中的下一个
,例如,您可以访问中的变量。您的构建阶段为
版本
,在您的容器中以版本
You could firstly export these variables as environmetal variables to your bash
Then use
--build-arg
flag to pass them to your docker buildNext in your dockerfile
As a result, for example you can access the variables in your build phase as
VERSION
and in your container asversion
我从别人那里读到的内容说,没有“ docker build-env-file ....”选项/设备。因此,这种情况很好地论证了将dockerfile的更多内容转移到dockerfile只是复制和运行的shell脚本的内容,因为您可以以这种方式来摘录.env文件。
engretings.sh
变量.env
dockerfile
What I've read from others says that there is no "docker build --env-file...." option/apparatus. As such, this situation makes a good argument for shifting more of the content of the dockerfile to a shell script that the dockerfile simply copies and runs, as you can source the .env file that way.
greetings.sh
variables.env
dockerfile
您可以在
docker-compose.dev.yml
文件中指定env_file
,如下所示:您必须具有一个.env.tevelopment文件,其中包含所有需要的环境变量传递到Docker图像。
例如:
You can specify the
env_file
in thedocker-compose.dev.yml
file as follows:and you have to have a .env.development file containing all the environment variables you need to pass to the docker image.
e.g.: