如何从.env文件中传递Dockerfile中的Env Value?

发布于 2025-02-03 11:04:55 字数 647 浏览 1 评论 0原文

我的目录结构看起来像这样。

|
| --- 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小草泠泠 2025-02-10 11:04:55

您首先可以将这些变量作为环境变量导出到bash中

source .env

,然后使用- build-arg flag将它们传递到docker build to

docker image build --build-arg VERSION=$VERSION --build-arg DATE=$DATE .

y dockerfile中的下一个

ARG VERSION
ARG DATE
ENV version=$VERSION
ENV date=$DATE

,例如,您可以访问中的变量。您的构建阶段为版本,在您的容器中以版本

You could firstly export these variables as environmetal variables to your bash

source .env

Then use --build-arg flag to pass them to your docker build

docker image build --build-arg VERSION=$VERSION --build-arg DATE=$DATE .

Next in your dockerfile

ARG VERSION
ARG DATE
ENV version=$VERSION
ENV date=$DATE

As a result, for example you can access the variables in your build phase as VERSION and in your container as version

感情洁癖 2025-02-10 11:04:55

我从别人那里读到的内容说,没有“ docker build-env-file ....”选项/设备。因此,这种情况很好地论证了将dockerfile的更多内容转移到dockerfile只是复制和运行的shell脚本的内容,因为您可以以这种方式来摘录.env文件。

engretings.sh

#!/bin/sh

source variables.env
echo Foo $copyFileTest

变量.env

export copyFileTest="Bar"

dockerfile

FROM alpine:latest

COPY variables.env .
RUN source variables.env && echo Foo $copyFileTest  #outputs: Foo Bar
COPY greetings.sh .
RUN chmod +x /greetings.sh
RUN /greetings.sh               #outputs: Foo Bar

RUN echo $copyFileTest          #does not work, outputs nothing

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

#!/bin/sh

source variables.env
echo Foo $copyFileTest

variables.env

export copyFileTest="Bar"

dockerfile

FROM alpine:latest

COPY variables.env .
RUN source variables.env && echo Foo $copyFileTest  #outputs: Foo Bar
COPY greetings.sh .
RUN chmod +x /greetings.sh
RUN /greetings.sh               #outputs: Foo Bar

RUN echo $copyFileTest          #does not work, outputs nothing
以为你会在 2025-02-10 11:04:55

您可以在docker-compose.dev.yml文件中指定env_file,如下所示:


# docker-compose.dev.yml

services:
  app:
    ...
    env_file:
        - .env.development

您必须具有一个.env.tevelopment文件,其中包含所有需要的环境变量传递到Docker图像。

例如:

# .env.development

REACT_APP_API_URL="https://some-api-url.com/api/graphql"

You can specify the env_file in the docker-compose.dev.yml file as follows:


# docker-compose.dev.yml

services:
  app:
    ...
    env_file:
        - .env.development

and you have to have a .env.development file containing all the environment variables you need to pass to the docker image.

e.g.:

# .env.development

REACT_APP_API_URL="https://some-api-url.com/api/graphql"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文