[英]Why is my docker container not picking up environment variables set in docker-compose?
I have a containerised app我有一个容器化的应用程序
|-- project
|-- app
|-- main.py
|-- settings.py
|-- Dockerfile
|-- requirements.txt
|-- .env
|-- docker-compose-dev.yaml
I have set environment variables in .env
.我在.env
中设置了环境变量。 I use the 'environment:' section in docker-compose-dev.yaml
to pass them to the app container, I access them in settings.py
and then import them into main.py
.我使用docker-compose-dev.yaml
中的 'environment:' 部分将它们传递给应用程序容器,我在settings.py
中访问它们,然后将它们导入main.py
。 They do not seem to be being picked up by the app, but docker-compose docker-compose-dev.yaml config
shows them being pulled from .env
.它们似乎没有被应用程序拾取,但docker-compose docker-compose-dev.yaml config
显示它们是从.env
中提取的。
.env .env
REDIS_HOST=redis
REDIS_PORT=6379
docker-compose-dev.yaml docker-compose-dev.yaml
version: "3.2"
services:
app:
build: ./app
container_name: app
environment:
- REDIS_HOST:$REDIS_HOST
- REDIS_PORT:$REDIS_PORT
Running docker-compose -f docker-compose-dev.yaml config
gives:运行docker-compose -f docker-compose-dev.yaml config
给出:
services:
collector:
build:
context: /project/app
container_name: app
environment:
REDIS_HOST:redis: null
REDIS_PORT:6379: null
app/Dockerfile应用程序/Dockerfile
FROM python:3.8.7-buster
WORKDIR /app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV USE_DOCKER=true
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . .
CMD ["python", "main.py"]
app/settings.py应用程序/settings.py
import os
import dotenv
BASE_DIR = os.path.dirname(
os.path.dirname(
os.path.abspath(__file__)
)
)
dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file):
print(dotenv_file)
dotenv.load_dotenv(dotenv_file)
#load_dotenv()
REDIS_HOST = os.environ.get('REDIS_HOST', None)
REDIS_PORT = os.getenv('REDIS_PORT', None)
print(REDIS_HOST)
print(REDIS_PORT)
app/main.py应用程序/main.py
import logging
import settings
import redis
def main():
logging.info("redis host: %s", settings.REDIS_HOST)
logging.info("redis port: %s", settings.REDIS_PORT)
r = redis.Redis(host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
db=0,
)
if __name__ == "__main__":
main()
If I run this locally, outside of Docker, then main.py
does what I expect: print()
and logging.info()
both show REDIS_HOST = redis, REDIS_PORT = 6379.如果我在 Docker 之外在本地运行它,那么main.py
会按照我的预期运行: print()
和logging.info()
都显示 REDIS_HOST = redis,REDIS_PORT = 6379。
If I run this using docker-compose -f docker-compose-dev.yaml up --build
then main.py
logs REDIS_HOST and REDIS_PORT as None.如果我使用docker-compose -f docker-compose-dev.yaml up --build
运行它,那么main.py
将 REDIS_HOST 和 REDIS_PORT 记录为 None。
If I include print(os.environ)
in main.py
and then spin it up with docker-compose, it shows the environment variables declared in the Dockerfile (PYTHONBUFFERED, USE_DOCKER), but not those referenced in docker-compose-dev.yaml (REDIS_HOST, REDIS_PORT). If I include print(os.environ)
in main.py
and then spin it up with docker-compose, it shows the environment variables declared in the Dockerfile (PYTHONBUFFERED, USE_DOCKER), but not those referenced in docker-compose-dev.yaml (REDIS_HOST , REDIS_PORT)。
The 'null's after the variables in the config output give me pause for thought, but I cannot find an explanation for them anywhere.配置 output 中的变量之后的'null'让我停下来思考,但我无法在任何地方找到它们的解释。
Any suggestions for what I'm overlooking would be welcome.欢迎对我忽略的内容提出任何建议。 I have a very similar set-up on another project which works smoothly, so I'm stumped about what's causing this.我在另一个运行顺利的项目上有一个非常相似的设置,所以我很难理解是什么原因造成的。
The reason is that the compose yaml
lacks spaces between the name and the value from the.env file and also take out the dashes:原因是 compose yaml
在 .env 文件中的名称和值之间缺少空格,并且还去掉了破折号:
environment:
REDIS_HOST: $REDIS_HOST
REDIS_PORT: $REDIS_PORT
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.