我正在尝试通过 Django 配置使用不同的配置来执行名为 dockerplayground 的 Django 项目。目标是在 docker-build 命令期间通过环境变量设置配置。由于某种原因,当我启动容器并使用默认值时,Django-project 找不到 env 变量。
这是我的文件,Django 项目是一个使用“python manage.py startapp example”命令制作的带有空 app-skaffold 的骨架。
Dockerfile:
FROM python:3.6
RUN apt-get update && apt-get upgrade -y && apt-get autoremove && apt-get autoclean
RUN apt-get install -y \
libffi-dev \
libssl-dev \
default-libmysqlclient-dev \
libxml2-dev \
libxslt-dev \
libjpeg-dev \
libfreetype6-dev \
zlib1g-dev \
net-tools \
vim
RUN echo openssl version
ARG DJANGO_CONF_ARG
ENV DJANGO_CONFIGURATION=$DJANGO_CONF_ARG
RUN pip install pip --upgrade
#https://github.com/circus-tent/circus/issues/1056
RUN pip install 'tornado==4.5.3'
RUN pip install gunicorn circus
ADD requirements.txt /
RUN pip install -r requirements.txt
ADD . /
EXPOSE 8000
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
入口点.sh
#!/bin/bash
python manage.py makemigrations
python manage.py migrate
exec circusd circus.ini --log-level debug
exec "$@";
马戏团.ini
[circus]
check_delay = 5
[watcher:gunicorn]
cmd = /usr/local/bin/gunicorn
args = -b 0.0.0.0:8000 -w 2 dockerplayground.wsgi
numprocesses = 1
autostart = true
max_retry = -1
priority = 500
要求.txt
Django==2.0.7
django-configurations
dockerplayground/wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dockerplayground.settings")
os.environ.setdefault('DJANGO_CONFIGURATION', "Dev")
from configurations.wsgi import get_wsgi_application
application = get_wsgi_application()
管理.py
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dockerplayground.settings")
os.environ.setdefault('DJANGO_CONFIGURATION', "Dev")
from configurations.management import execute_from_command_line
execute_from_command_line(sys.argv)
dockerplayground/settings.py
import os
from configurations import Configuration
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
class Base(Configuration):
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'inbt@l4iuuc0xi7qiut_*=uh3@pi8^)nq1e6o$i2#7s8mu(3#j'
# SECURITY WARNING: don't run with debug turned on in production!
# DEBUG = True
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'example.apps.ExampleConfig'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'dockerplayground.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'dockerplayground.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
class Prod(Base):
DEBUG = False
class Dev(Base):
DEBUG = True
Docker 构建命令:
docker build -t dockerplayground --build-arg DJANGO_CONF_ARG=Prod .
码头工人运行命令:
docker run -p 8000:8000 -d dockerplayground:latest
然后如果我去容器:
docker exec -it containerid bash
并执行 printenv 我可以看到我的环境变量设置为 Prod,但 localhost:8000 显示 debug-django 欢迎屏幕。我不知道出了什么问题,因为 Prod 根本不应该显示调试登录页面。