[英]unable to access environment variables from docker compose env file
I have a problem with reading the.env variables from a docker-compose.yml file (version -3.7), here first I explain my folder and sample code structure我在从 docker-compose.yml 文件(版本 -3.7)中读取 the.env 变量时遇到问题,这里首先解释我的文件夹和示例代码结构
My folder structure我的文件夹结构
my_app我的应用程序
src来源
Dockerfile Dockerfile
docker-compose.yml docker-compose.yml
.env .env
.env file .env 文件
This is my sample.env file这是我的 sample.env 文件
ENVIRONMENT_NAME=DEV
DATABASE_NAME=testing
docker-compose.yml file docker-compose.yml文件
This is my sample docker-compose.yml file (version: 3.7)这是我的示例 docker-compose.yml 文件(版本:3.7)
version: "3.7"
services:
my_app_test:
env_file: ./.env
image: my-app-test
build:
context: ./src
dockerfile: Dockerfile
Dockerfile Dockerfile
This is my sample Dockerfile这是我的样本 Dockerfile
FROM python:3.7-alpine
# Install required packages
RUN apk add --update --no-cache \
build-base \
postgresql-dev \
linux-headers \
pcre-dev \
py-pip \
bash \
curl \
openssl \
nginx \
libressl-dev \
musl-dev \
libffi-dev \
rsyslog \
&& pip install Cython
# Create container's working directory
RUN mkdir -p /MyApp
# Copy all source files to the container's working directory
COPY ./ /MyApp
# Install all python dependency libs
RUN pip install -r /MyApp/requirements.txt
WORKDIR /MyApp
ENTRYPOINT ["python3"]
src/config.py源代码/配置.py
Here in the config file, I am reading all the environment variables在配置文件中,我正在读取所有环境变量
import os
from pathlib import Path
from dotenv import load_dotenv
ENV_PATH = Path('.env')
load_dotenv(dotenv_path=ENV_PATH)
ENVIRONMENT_NAME = os.getenv('ENVIRONMENT_NAME')
DATABASE_NAME = os.getenv("DATABASE_NAME")
src/app.py源代码/app.py
Here in the app.py file, I am getting the variables from config.py and use that in my project.在 app.py 文件中,我从 config.py 获取变量并在我的项目中使用它。 When I run this file after building a docker image, it is not working as expected.当我在构建 docker 图像后运行此文件时,它没有按预期工作。 All the env variables are read as None所有环境变量都被读取为 None
import config
import os
print(config.ENVIRONMENT_NAME)
print(config.DATABASE_NAME)
# Access all environment variables
print('\n')
print(os.environ)
I am trying to access env sets from the docker-compose file and not from Dockerfile.我正在尝试从 docker-compose 文件而不是 Dockerfile 访问环境集。
Docker build is working fine, After that when I try to run the docker image ( sudo docker run my-app-test app.py ) it does not print the environment variables as I expected. Docker 构建工作正常,之后当我尝试运行 docker 图像( sudo docker run my-app-test app.py )时,它不会按我的预期打印环境变量。 The output of the current code is ,当前代码的output是,
None None environ({'PATH': '/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', 'HOSTNAME': '6cfc0c912772', 'LANG': 'C.UTF-8', 'GPG_KEY': '0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D', 'PYTHON_VERSION': '3.7.5', 'PYTHON_PIP_VERSION': '19.3.1', 'PYTHON_GET_PIP_URL': 'https://github.com/pypa/get-pip/raw/ffe826207a010164265d9cc807978e3604d18ca0/get-pip.py', 'PYTHON_GET_PIP_SHA256': 'b86f36cc4345ae87bfd4f10ef6b2dbfa7a872fbff70608a1e43944d283fd0eee', 'HOME': '/root'})
Requirements am using is,我使用的要求是,
I want to fix this env reading issue.我想解决这个环境阅读问题。 I am not sure where the problem is, @anyone pls help me to resolve this issue.我不确定问题出在哪里,@任何人请帮助我解决这个问题。
You are not setting ENV at build time, so docker-compose will able to read env file
as you set configuration in the compose file, but docker run
will not read the env
as you need to specify env file
您没有在构建时设置 ENV,因此 docker-compose 将能够在您在撰写文件中设置配置时读取env file
,但是docker run
将不会读取env
,因为您需要指定env file
You need to specify env-file
in docker run command您需要在 docker 运行命令中指定env-file
docker run --env-file .env my-app-test app.py
or to just check ENV或者只检查 ENV
docker run --env-file .env --entrypoint printenv my-app-test
or run the stack或运行堆栈
docker-compose up
docker run
will not find the env
file because the env file is specified in the docker-compose.yml
file. docker run
会找不到env
文件,因为docker-compose.yml
文件中指定了env文件。
Try running your container like this instead:尝试像这样运行你的容器:
docker-compose run my_app_test app.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.