Skip to content

Conversation

Mischback
Copy link
Owner

@Mischback Mischback commented Feb 16, 2020

Integrating Docker

Architecture

  • Docker-based containerization
  • Container 1: Django
    • runs the actual Django project
    • requires Python, Django (and used app packages) and Gunicorn to actually expose Django
  • Container 2: PostgreSQL
    • Postgre is as good a choice as any DBMS, but it's like the first recommendation for Django
  • Container 3: NGINX
    • NGINX is used to serve the static files
    • acts as reverse proxy and enables horizontal scaling (see 12-factor app)
    • is used to terminate HTTPS connections

Django

  • Debian-based Python image
    • I prefer Debian stable (as of now: Buster), because I'm quite familiar with it. The provided Docker image is considered slim. If image size is a limiting fact, it would be possible to use the Alpine-based Python image.
  • isolate the project's dependencies in a virtual environment
    • this might be overkill, but it keeps everything nice and clean.
    • following Python best practices aswell as 12-factor app guidance
  • use Gunicorn to actually expose the project
    • possible alternatives:
      • uWSGI
  • update Django's settings to enable passing configuration values by environment variables
  • Gunicorn is configured with a config file
    • enable configuration by environment variables (see above)
  • Logging
    • re-configure Django's logging to log to stdout
    • TODO: determine, if sending of eMail (AdminEmailHandler) is desirable
    • TODO: check Gunicorn's default logging configuration
    • enable JSON logging (at least an optional formatter)

Postgre

NGINX

Documentation

  • Quickstart
    • how to actually start a project with this template
    • describe make init including a documentation (which files get overwritten and why)
    • available workflows (based on tox, native Django in a virtual environment, the concept of Docker-based staging and production deployments
  • Docker-related files
  • how to manage requirements
  • explain the settings module (common.py and development.py vs. production.py)

This is just applied for the util environment. It is changed to use the
provided requirements/development.txt
The project's requirements are automatically resolved during build step.
The build process is further refined, using a single 'builder' (base) to
set up the virtual environment, that is then copied over into the final image.
Actually, this project will have more than one Makefile, reflecting different
stages of development and usage of django-project-skeleton.

1) The Makefile, that is included in the main directory when you download/use
the repo as Django's startproject template:
This Makefile is used during development and primarily includes receipts to
support development.
However, this will be the Makefile just after downloading the skeleton. So it
has to include a receipt to prepare the project for further usage.

2) The Makefile, that is used during the lifespan of the project:
This Makefile should support project-specific development, testing and
deployment until production usage.
This requires 'tput' to be installed on your system (most probably it is!),
but will gracefully provide monochrome text, if it is not.
The actual deployment Makefile received a lot of love, including
- comments, comments, documentation
- switched from 'echo' to '/usr/bin/printf' for max compatibility
- some minor manual testing of Makefile commands/targets

docker-compose.yml was updated to include the new naming scheme for images
built through the Makefile interface.
The tagging may be controlled by Makefile variables, that also are set up to
support templating by Django's startproject.
To tag every build with an unique identifier, a custom shell script was added
in the new ./bin directory. This script needs documentation and comments.
Success! The build process can now be launched directly from within this repo,
no more need to use a seperate test bed.

The current structure is deployed into a temporary tox environment,
initialized and then the Docker build process is triggered.

Images will be tagged with the sha1 tags of this repo.
@Mischback Mischback mentioned this pull request Apr 3, 2020
Mischback added 10 commits April 3, 2020 19:43
a) use the correct settings module
b) default to use development settings, when executing 'manage.py'
c) do not explicitly set the settings module in 'tox.ini', rely on the
   provided default value in 'manage.py'
The docker-specific settings were included into the production settings, with
some documentation, why and how Django's logging is deactivated.

It also includes a fix for ALLOWED_HOSTS, to work nicely in our Docker-based
deployment, but should be working fine in other setups aswell.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant