简体   繁体   English

在docker python中缺少环境变量:3使用docker-compose

[英]Missing Environment Vars in docker python:3 with docker-compose

Though my configuration looks good, my python:3 image does not seem to have the expected DJANGO_SECRET_KEY set, at least at the point that the Dockerfile attempts to run migrations 虽然我的配置看起来不错,但我的python:3图像似乎没有设置预期的DJANGO_SECRET_KEY ,至少在Dockerfile尝试运行迁移时

$ docker-compose config
services:
  api:
    build:
      context: /Users/ben/Projects/falcon/falcon-backend
      dockerfile: Dockerfile
    depends_on:
    - db
    - redis
    environment:
      DJANGO_SECRET_KEY: 'some-secret-that-works-elsewhere'
$
$ docker-compose up --build api
[...]
 Step 6/7 : RUN echo `$DJANGO_SECRET_KEY`
 ---> Running in fbfb569c0191

[...]
django.core.exceptions.ImproperlyConfigured: Set the DJANGO_SECRET_KEY env variable
ERROR: Service 'api' failed to build: The command '/bin/sh -c python manage.py migrate' returned a non-zero code: 1

however, the final line, CMD python manage.py runserver 0.0.0.0:8001 --settings=falcon.settings.dev-microservice does start up as desired, with the necessary env vars set. 但是,最后一行, CMD python manage.py runserver 0.0.0.0:8001 --settings=falcon.settings.dev-microservice确实启动了所需的env变量。

# Dockerfile -- api

FROM python:3

RUN pip3 -q install -r requirements.txt
RUN echo `$DJANGO_SECRET_KEY`
RUN python manage.py migrate --settings=falcon.settings.dev-microservice # <-- why does this not work
CMD python manage.py runserver 0.0.0.0:8001 --settings=falcon.settings.dev-microservice

Why does the penultimate line of the Dockerfile fail due to an unset environment variable while the final one works as expected? 为什么Dockerfile的倒数第二行由于未设置环境变量而失败,而最后一行按预期工作?

The environment variables not declared inside the Dockerfile are not visible to the container when building the image. 在构建映像时,容器中看不到Dockerfile未声明的环境变量。 They are only passed to the container at runtime. 它们仅在运行时传递给容器。 Since the RUN instruction executes on build, the environment variable DJANGO_SECRET_KEY which is declared outside the Dockerfile won't be visible to the RUN command. 由于RUN指令在构建时执行,因此在RUN命令下将看不到在Dockerfile外声明的环境变量DJANGO_SECRET_KEY

To solve the issue you can declare the env variable inside the Dockerfile and set it via a build argument: 要解决此问题,您可以在Dockerfile中声明env变量并通过构建参数设置它:

FROM python:3

RUN pip3 -q install -r requirements.txt
ARG key
ENV DJANGO_SECRET_KEY=$key
RUN echo `$DJANGO_SECRET_KEY`
RUN python manage.py migrate --settings=falcon.settings.dev-microservice
CMD python manage.py runserver 0.0.0.0:8001 --settings=falcon.settings.dev-microservice

Then, you should modify the composefile as such: 然后,你应该修改composefile:

build:
  context: /Users/ben/Projects/falcon/falcon-backend
  dockerfile: Dockerfile
  args:
    - key='secrete-key'

The RUN is used only when building image. RUN仅在构建图像时使用。 The CMD is the command that is started when you start container from your image. CMD是从映像启动容器时启动的命令。 If you run migrate when building image it is wrong, migrate is building your database and you want to run it each time before runserver 如果在构建映像时运行迁移是错误的,则迁移正在构建数据库,并且您希望每次在runserver之前运行它

# Dockerfile -- api

FROM python:3

RUN pip3 -q install -r requirements.txt
RUN echo `$DJANGO_SECRET_KEY`
CMD /bin/bash -c "python manage.py migrate --settings=falcon.settings.dev-microservice && python manage.py runserver 0.0.0.0:8001 --settings=falcon.settings.dev-microservice"

This is the proper way how to start django in docker, because you want to run the migrations on production when starting server. 这是如何在docker中启动django的正确方法,因为您希望在启动服务器时运行生产中的迁移。 Not on your PC when building image... 构建图像时不在PC上...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM