英文:
.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"]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论