Django 在 Docker 中加载错误的设置模块
Django Loading Wrong Settings Module in Docker
我将我的 Django 应用程序配置为在基本设置文件中使用 sqlite,我覆盖了生产和开发设置文件中的设置。
我在 Dockerfile 中将 DJANGO_SETTINGS_MODULE 设置为正确的值。
但是,当我 运行 它时,它使用基本设置文件中定义的 sqlite。如果我将其注释掉,它会抱怨 database.ENGINE 未设置。
为什么它从 base.py 设置文件而不是其他文件读取数据库配置?我在环境变量中指定了另一个,它从那里读取其他设置,但对于数据库,它从基本文件中读取它。
我对这种行为有些困惑,如果有人能给我一些解决这个问题的指导,我将不胜感激。
如果您需要更多信息,请告诉我。
Docker 文件:
FROM python:3.6
LABEL maintainer xxx@xx.com
ARG requirements=requirements/production.txt
ENV DJANGO_SETTINGS_MODULE=sasite.settings.production_test
WORKDIR /app
COPY manage.py /app/
COPY requirements/ /app/requirements/
RUN pip install -r $requirements
COPY config config
COPY sasite sasite
COPY templates templates
COPY logs logs
COPY scripts scripts
EXPOSE 8001
CMD ["/usr/local/bin/gunicorn", "--config", "config/gunicorn.conf", "--log-config", "config/logging.conf", "-w", "4", "-b", "0.0.0.0:8001", "sasite.wsgi:application"]
gunicorn 可能被守护进程,并且它可能不会按照您期望的方式继承 docker 环境变量。幸运的是,gunicorn 确实有一种方法,您可以使用 -e
标志为其工作人员指定环境值。因此,您只需将以下两个参数添加到 CMD
数组中,它就可以工作了:
-e
DJANGO_SETTINGS_MODULE=sasite.settings.production_test
CMD ["/usr/local/bin/gunicorn", "--config", "config/gunicorn.conf", "--log-config", "config/logging.conf", "-w", "4", "-b", "0.0.0.0:8001", <b>"-e", "DJANGO_SETTINGS_MODULE=sasite.settings.production_test", </b>"sasite.wsgi:application"]
我将我的 Django 应用程序配置为在基本设置文件中使用 sqlite,我覆盖了生产和开发设置文件中的设置。
我在 Dockerfile 中将 DJANGO_SETTINGS_MODULE 设置为正确的值。
但是,当我 运行 它时,它使用基本设置文件中定义的 sqlite。如果我将其注释掉,它会抱怨 database.ENGINE 未设置。
为什么它从 base.py 设置文件而不是其他文件读取数据库配置?我在环境变量中指定了另一个,它从那里读取其他设置,但对于数据库,它从基本文件中读取它。
我对这种行为有些困惑,如果有人能给我一些解决这个问题的指导,我将不胜感激。
如果您需要更多信息,请告诉我。
Docker 文件:
FROM python:3.6
LABEL maintainer xxx@xx.com
ARG requirements=requirements/production.txt
ENV DJANGO_SETTINGS_MODULE=sasite.settings.production_test
WORKDIR /app
COPY manage.py /app/
COPY requirements/ /app/requirements/
RUN pip install -r $requirements
COPY config config
COPY sasite sasite
COPY templates templates
COPY logs logs
COPY scripts scripts
EXPOSE 8001
CMD ["/usr/local/bin/gunicorn", "--config", "config/gunicorn.conf", "--log-config", "config/logging.conf", "-w", "4", "-b", "0.0.0.0:8001", "sasite.wsgi:application"]
gunicorn 可能被守护进程,并且它可能不会按照您期望的方式继承 docker 环境变量。幸运的是,gunicorn 确实有一种方法,您可以使用 -e
标志为其工作人员指定环境值。因此,您只需将以下两个参数添加到 CMD
数组中,它就可以工作了:
-e
DJANGO_SETTINGS_MODULE=sasite.settings.production_test
CMD ["/usr/local/bin/gunicorn", "--config", "config/gunicorn.conf", "--log-config", "config/logging.conf", "-w", "4", "-b", "0.0.0.0:8001", <b>"-e", "DJANGO_SETTINGS_MODULE=sasite.settings.production_test", </b>"sasite.wsgi:application"]