0

I'm trying to do Django-project named dockerplayground with different configurations via Django-configurations. The goal is to set the configuration trough environmental variable during docker-build command. For some reason the Django-project can't find the env-variable when I start the container and uses default-value instead.

Here are my files, the Django-project is a skeleton with empty app-skaffold made with "python manage.py startapp example" command.

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"]

entrypoint.sh

#!/bin/bash
python manage.py makemigrations
python manage.py migrate
exec circusd circus.ini --log-level debug

exec "$@";

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

requirements.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()

manage.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 build command:

docker build -t dockerplayground --build-arg DJANGO_CONF_ARG=Prod . 

Docker run command:

docker run -p 8000:8000 -d  dockerplayground:latest

and then if I go to container with:

docker exec -it containerid bash

and excecute printenv I can see my environmental variable set to Prod, but localhost:8000 shows the debug-django welcoming screen. I don't know what goes wrong because Prod should not show debug-landing page at all.

Toni Nurmi
  • 355
  • 4
  • 20

1 Answers1

1

I think the class type settings may be the issue. Anyway, try this in your settings.py,

import os

PROFILE = os.environ.get('DJANGO_CONFIGURATION', 'Dev')
if PROFILE == 'Dev':
    DEBUG = True
else:
    DEBUG = False

UPDATE

After lot's of googling I found the solution,

From the documenetation of circus,

copy_env

If set to true, the local environment variables will be copied and passed to the workers when spawning them. (Default: False)

So,you have to set copy_env=true in your circus.ini file

Hence your circus.ini will be as,

[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
Community
  • 1
  • 1
JPG
  • 82,442
  • 19
  • 127
  • 206
  • Hi, thanks for the suggestion, but that would kind of make Django-configurations package obsolete as the advantage of using it is the class-based settings module. see https://django-configurations.readthedocs.io/en/stable/ for details. – Toni Nurmi Aug 20 '18 at 11:30
  • before that, pls do try my solution and let me know the result – JPG Aug 20 '18 at 11:34
  • I tried to put your solution first to my Base-settings class, builded and started the docker-image but had the same result with Debug-startpage showing up. Then I tried to put it before the Base-class but got the same result after building and running the image. – Toni Nurmi Aug 20 '18 at 11:42
  • I tried the package and It worked as expected. Is it possible to re-produce the error? – JPG Aug 20 '18 at 12:02
  • That's strange, I will try build the docker image without cache – Toni Nurmi Aug 20 '18 at 12:06
  • add an update with how to reproduce the behavior or share your project. – JPG Aug 20 '18 at 12:07
  • 1
    project is available to clone at https://bitbucket.org/AvareaToniN/dockerenvs/src/master/ – Toni Nurmi Aug 20 '18 at 12:14
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178354/discussion-between-jerin-peter-george-and-toni-nurmi). – JPG Aug 20 '18 at 12:28