.env file contents not being substituted during docker compose build although in same directory

huangapple go评论60阅读模式
英文:

.env file contents not being substituted during docker compose build although in same directory

问题

我的docker-compose.yaml文件如下:

版本:“3.9”
服务:
  planview:
    构建:
      上下文:。
      Dockerfile:Dockerfile
    容器名称:planview
    环境:
      - AZURE_TENANT_ID=${AZURE_TENANT_ID}
      - AZURE_CLIENT_ID=${AZURE_CLIENT_ID}
      - AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET}

一切都在一个目录下,所以这不可能是问题所在。

在我的Python脚本中,我也用以下代码检索凭据:

load_dotenv(“.env”)
client_id = os.getenv(‘AZURE_CLIENT_ID’)
tenant_id = os.getenv(‘AZURE_TENANT_ID’)
client_secret = os.getenv(‘AZURE_CLIENT_SECRET’)

当运行docker compose up时,会创建镜像,容器运行,并执行脚本。只有在尝试使用docker compose build创建包含凭据的镜像时才会收到错误:

ValueError: client_id应为Azure Active Directory应用程序的ID

我的docker-compose文件肯定有问题,但我看不到错误在哪里或者是否遗漏了什么。我也尝试过env_file:但没有任何成功。提供的凭据肯定是有效的。

有人看到问题所在吗?

编辑:

Dockerfile是

FROM python:3.10

ADD planview.py .

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

COPY . /code/app

CMD [“python3”,“/planview.py”]
英文:

My docker-compose.yaml file is as follows:

services:
  planview:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: planview
    environment:
      - AZURE_TENANT_ID=${AZURE_TENANT_ID}
      - AZURE_CLIENT_ID=${AZURE_CLIENT_ID}
      - AZURE_CLIENT_SECRET=${AZURE_CLIENT_SECRET}

Everything is in one directory so that can't be the issue.

In my Python script I'm also retrieving the credentials with the following code

load_dotenv(".env")
client_id = os.getenv('AZURE_CLIENT_ID')
tenant_id = os.getenv('AZURE_TENANT_ID')
client_secret = os.getenv('AZURE_CLIENT_SECRET')

When running docker compose up, the image is created, the container runs and the script is executed. It's only when trying to use docker compose build to create an image that has the credentials so anyone who has it can run is when I receive the error

ValueError: client_id should be the id of an Azure Active Directory application

There has to be something wrong with my docker-compose file but I can't see where the error is or if I'm missing something. I've also tried env_file: without any success. The credentials provided are definitely valid.

Does anyone see where the issue is?

EDIT:

Dockerfile is

FROM python:3.10

ADD planview.py .

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt

RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

COPY . /code/app

CMD ["python3", "/planview.py"]

答案1

得分: 1

尝试将这些变量添加到你的Dockerfile中,以便在运行时可用。应该看起来像这样:

COPY . /code/app

# 设置环境变量
ENV AZURE_TENANT_ID=""
ENV AZURE_CLIENT_ID=""
ENV AZURE_CLIENT_SECRET=""

CMD ["python3", "/planview.py"]
英文:

Try adding those variables in your Dockerfile so it is available in runtime. Should look like this:

COPY . /code/app

# Set the environment variables
ENV AZURE_TENANT_ID=""
ENV AZURE_CLIENT_ID=""
ENV AZURE_CLIENT_SECRET=""

CMD ["python3", "/planview.py"]

huangapple
  • 本文由 发表于 2023年6月30日 03:24:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定