简体   繁体   English

为什么我的 docker 容器没有获取在 docker-compose 中设置的环境变量?

[英]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.

相关问题 容器不停留在docker-compose up上 - Container not staying up on docker-compose up docker-compose 不使用烧瓶设置环境变量 - docker-compose not setting environment variables with flask 使用docker-compose up与docker-compose run从容器内部执行容器中的命令 - executing commands in containers from within a container using docker-compose up vs docker-compose run docker-compose exec导致[Errno 2]没有此类文件或目录:docker容器中的'docker-compose':'docker-compose' - docker-compose exec causes [Errno 2] No such file or directory: 'docker-compose': 'docker-compose' in docker container 为什么 docker-compose 会在 `docker-compose up` 上构建一个新镜像? - Why does docker-compose build a new image on `docker-compose up`? 为什么 docker-compose 不将数据从容器保存到我的本地机器? - Why docker-compose don't save data from container to my local machine? 为什么“ docker-compose run”可以在容器外部创建文件? - Why can “docker-compose run” create files outside the container? 如何在 docker-compose 中设置和调用“awslogs”? - How to set up and call 'awslogs' in docker-compose? 运行 docker-compose 时如何将参数传递给 docker 容器 - How to pass argument to docker container when running docker-compose up 无法从 docker-compose 的另一个容器访问 api 的 docker 容器 - docker container of api not accessible from another container of docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM