我正在尝试通过Django配置使用不同的配置来执行名为dockerplayground的Django项目。目标是在docker-build命令期间设置配置槽环境变量。出于某种原因,当我启动容器并使用默认值时,Django项目无法找到env变量。
这是我的文件,Django项目是一个骨架,其中包含用“ python manage.py startapp example”命令制作的空app-skaffold。
Dockerfile:
#!/bin/bash
python manage.py makemigrations
python manage.py migrate
exec circusd circus.ini --log-level debug
exec "$@";
entrypoint.sh
[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
circus.ini
Django==2.0.7
django-configurations
requirements.txt
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()
dockerplayground / wsgi.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)
manage.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
dockerplayground / settings.py
docker build -t dockerplayground --build-arg DJANGO_CONF_ARG=Prod .
Docker构建命令:
docker run -p 8000:8000 -d dockerplayground:latest
Docker运行命令:
docker exec -it containerid bash
然后如果我使用以下容器:
$('#click').on('click', function() {
var elem = $('.header').filter(function() {
return ($.text([this]) === 'Tha Royal Natural Spa')
}).parent().parent();
if(elem.next().hasClass('divider')) elem.next().remove();
elem.remove();
});
和执行printenv我可以看到我的环境变量设置为Prod,但是localhost:8000显示了debug-django欢迎屏幕。我不知道出了什么问题,因为Prod根本不应该显示调试登陆页面。
答案 0 :(得分:1)
我认为类类型设置可能是个问题。无论如何,请在您的settings.py
中尝试一下,
import os
PROFILE = os.environ.get('DJANGO_CONFIGURATION', 'Dev')
if PROFILE == 'Dev':
DEBUG = True
else:
DEBUG = False
经过大量的搜索后,我找到了解决方案,
在documenetation of circus
中,
copy_env
如果设置为
true
,将在生成它们时复制本地环境变量并将其传递给工作程序。 (默认:False
)
因此,您必须在 copy_env=true
文件中设置 circus.ini
因此,您的 circus.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
copy_env = true