Commit abb0c555 authored by Evili del Rio's avatar Evili del Rio

Initial import from G*tH*b

parents
Pipeline #1789 passed with stages
in 38 minutes and 43 seconds
.svn
*~
*.pyc
__pycache__
FROM python:alpine
#ARG dev_packages="gcc g++ libc-dev libxml2-dev libxslt-dev postgresql-dev openldap-dev \
# jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev"
ARG dev_packages="gcc g++ libc-dev postgresql-dev"
ARG lib_packages="git postgresql-libs"
ARG django_version=">2.2,<2.3"
RUN mkdir -pv /app
RUN mkdir -pv /settings
WORKDIR /app
RUN pip install --upgrade pip
RUN apk update && \
apk add bash \
${lib_packages} \
${dev_packages} && \
pip install gunicorn \
"django${django_version}" \
psycopg2-binary mysql-connector whitenoise[brotli] && \
apk del ${dev_packages} && \
apk add ${lib_packages} && \
rm -fr /root/.cache
ENV DJANGO_STATIC_ROOT=/static
ENV DJANGO_MEDIA_ROOT=/media
VOLUME ["/static", "/media", "/app"]
EXPOSE 5000/tcp
COPY environment_settings.py /
COPY entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]
# docker-django
## A Docker image to execute Django apps
This is a python:alpine image that will run a Django project using
gunicorn and listening on TCP port 5000. It will try to do its best to
run with sensible default settings for non testing environments (no `DEBUG`,
Postgres database, etc.).
How it works:
* It uses environment variables to override Django settings: any setting
present in the Django global_settings or the project default
settings module or the module designated by `DJANGO_SETTINGS_MODULE`
(normally that's project_name.settings) can be overriden by the
environment runtime variable `DJANGO_NAME_OF_THE_SETTING`. For example, to
change the default timezeone `TIME_ZONE` (None) you should set
`DJANGO_TIME_ZONE='CET'` for Central European Time.
To change the
default language set, for example, `DJANGO_LANGUAGE_CODE='ca'` to set
Catalan language.
* If the Django setting value is not a string, the environment
variable is evaluated as Python code. This way, you can change any
setting tipe (integer, tuple, list, whatever).
For example to
change the `INSTALLED_APPS` variable from the default you should
set
`DJANGO_INSTALLED_APPS="['django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','myapp',]"`,
or even: `DJANGO_INSTALLED_APPS="INSTALLED_APPS +
['my_special_app']"`
* For database settings there is a special treatment. If no
`DJANGO_DATABASES` environment variable exists, it looks for
`DJANGO_DATABASE_ENGINE` (defaulting to postgres), `DJANGO_DATABASE_HOST`,
`DJANGO_DATABASE_NAME`, `DJANGO_DATABASE_USER`, and `DJANGO_DATABASE_PASSWORD`
to set up the `'default'` database.
To
* `DEBUG` is disabled unless enabled *explicitly* via environment variable (set `DJANGO_DEBUG=True`).
How to run it:
You should mount the base directory of your Django project at the `/app` volume.
For example:
$ cd your_django_project_dir
$ docker run -t -i -e DJANGO_TIME_ZONE='CET' -v $(pwd):/app django
#!/usr/bin/env bash
#
# Exit if error occurs
#
set -e
if [[ $# -ge 1 ]]
then
cd $(dirname $1)
else
cd /app
fi
if [[ -n "${REQUIRED_PACKAGES}" ]]
then
apk update
## we add gcc, g++, and libc-dev so that we can compile.
apk add gcc g++ libc-dev ${REQUIRED_PACKAGES}
fi
# Install requirements
REQUIREMENTS_FILES=${REQUIREMENTS_FILES:-requirements.txt}
for req in ${REQUIREMENTS_FILES}
do
REQUIREMENTS="${REQUIREMENTS} -r ${req}"
done
pip install ${REQUIREMENTS}
## Remove gcc and friends but not the rest
if [[ -s "${REQUIRED_PACKAGES}" ]]
then
apk del gcc g++ libc-dev
fi
## Find settings in CWD
settings_found=$(find . -name settings -o -name settings.py | \
head |tr / . |\
cut -d . -s -f 3- | head -1)
export DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE:-${settings_found}}
## Find .wsgi
DJANGO_WSGI=${DJANGO_WSGI:-$(basename $(find . -maxdepth 2 -type f -name wsgi.py | head -1|awk -F/ '{print $2"."$3}') .py)}
DJANGO_LOG_LEVEL=${DJANGO_LOG_LEVEL:-info}
# Populate settings with default, and override them with environment variables
cat > /settings/final_settings.py <<EOF
#
# Keep default settings apart
#
import ${DJANGO_SETTINGS_MODULE} as __default_settings
#
# Default project settings
#
from ${DJANGO_SETTINGS_MODULE} import *
EOF
#
# Module to override settings with environment variables
#
cat >> /settings/final_settings.py < /environment_settings.py
export PYTHONPATH=/settings
export DJANGO_SETTINGS_MODULE=final_settings
if [[ ${DJANGO_LOG_LEVEL} == "debug" ]]
then
python3 manage.py diffsettings
fi
python3 manage.py migrate --no-input
python3 manage.py collectstatic --no-input
gunicorn --bind 0.0.0.0:5000 \
--capture-output \
--log-level=${DJANGO_LOG_LEVEL} \
--pid /var/run/gunicorn.pid \
${DJANGO_WSGI} $*
#
# Django Settings Module entirely based on defaults and environment
# First we import the default settings for the project that
# should reside in <projectname>.settings
# then we import the global_settings from django.conf
# superseeded by the default <projectname>.settings
# For each setting SETTING_NAME test if the corresponding
# environment variable DJANGO_SETTING_NAME is defined.
# If it is, we incorporate as a true Django Setting
# By default, we check if the global or default setting is a string,
# if not we evaluate directly the value as a Python expression.
#
# Special Cases:
# If DJANGO_DATABASES is not defined we look for alternate definition
# via DJANGO_DATABASE_ENGINE, DJANGO_DATABASE_HOST, DJANGO_DATABASE_NAME,
# DJANGO_DATABASE_USER, and DJANGO_DATABASE_PASSWORD; defaulting to postgres
# environment variables POSTGRES_HOST, POSTGRES_DB, etc.
#
#
# DEBUG is disabled if it is not explicitly enabled via environment
#
DEBUG = False
import os
from django.conf import global_settings as __global_settings
#
# Dictionary to hold global and default settings
#
__settings = {}
# Django global settings dict
__settings.update(vars(__global_settings))
# default settings as a dict
__settings.update(vars(__default_settings))
for st,val in __settings.items():
# skip private items
if st.startswith('_'):
continue
else:
# look for the corresponding 'DJANGO_..' environment
env_setting = os.getenv('DJANGO_'+st, None)
if env_setting is not None:
# if the former setting was a string evaluate as such
if isinstance(val, str):
exec(st+" = '"+env_setting+"'")
else:
exec(st+" = "+env_setting)
if os.getenv('DJANGO_DATABASES', None) is None:
_PROJECT_NAME = os.getenv('DJANGO_SETTINGS_MODULE').split('.')[0]
DATABASES = {
'default': {
'ENGINE': os.getenv('DJANGO_DATABASE_ENGINE','django.db.backends.postgresql'),
'HOST': os.getenv('DJANGO_DATABASE_HOST', os.getenv('POSTGRES_HOST', 'postgres')),
'NAME': os.getenv('DJANGO_DATABASE_NAME', os.getenv('POSTGRES_DB', _PROJECT_NAME)),
'USER': os.getenv('DJANGO_DATABASE_USER', os.getenv('POSTGRES_USER', _PROJECT_NAME)),
'PASSWORD' : os.getenv('DJANGO_DATABASE_PASSWORD', os.getenv('POSTGRES_PASSWORD', _PROJECT_NAME)),
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment