/udjango

An experiment in all docker development for Django microservices

Primary LanguagePython

µDjango

A template project for Django microservices and testing ground for a fully dockerised build environment

Prerequisites

This project uses a fully dockerised environment. As such you will need Docker and Docker Compose installed. There are also a number of helpers within the Makefile so it would be useful, but not essential to have a copy of Make installed.

You should also have rapyd installed somewhere on the path

Getting Started

Assuming you have Docker Compose installed the entire build environment can be started with:

docker-compose up

This will start the PostgreSQL database server, a Django application server and a Sphinx documentation service.

The Django application service will reload automatically (after 3 seconds) when changes are made within /app and you can access the server using localhost:8000.

The Sphinx documentation service will also automatically rebuild the documentation on change and is available on localhost:8080

Development Tasks

To make running everything inside a Docker container slightly less painful the project contains a Makefile with a number of targets, described below.

Running Tests

Tests are run with py.test. We are using the pytest-isort and pytest-flake8 plugins for static analysis. You can run the full test suite with:

make test

Updating the Virtual Environment

The virtual environment resides within the Django app Docker container, so when you add or change requirements (found in /app/requirements/*.txt) you need to do so inside the container:

make update

Format Python

This project provides yapf to automatically format Python code to our (pep8 derived) standard and isort to ensure that imports are sorted nicely. To automatically apply formatting changes:

make format

Static Analysis

MyPy has been included to do static type checking. To run mypy:

make static

Behavioural Tests

Behavioural testing is supported with behave

make behave

Building a Docker Image

make build

Brain Dump Log

2017-12-19:
The Makefile approach to running stuff in the container is a bit cumbersome. In particular, where I want to pass through command line options. e.g. having a Django management command target is not really usable unless I specify each one separately. Not a good solution. Could something else be used which is more flexible but still doesn't require Python to be installed. Perhaps a simple cli compiled with nuitka?
2017-12-19:

I tried writing something and compiling with Nuitka, however the standalone compilation does not statically compile in libraries, you would need to copy them all around as well which is not ideal.

So I wrote something in Go. This does compile to a single, statically linked executable and allows cross compiling, which is nice. rapyd (Run A Python in Docker) is the tool and it basically takes all command line args and runs them inside the docker container instead. It is a seriously simplistic but appears to work.

I also managed (with a little tweaking) to get the whole setup running in PyCharm, including the debugger. Which does remind me that we probably need a way of running a debugger when not using PyCharm.

2018-01-03:

Using docker compose for the web server I think it means I can just use pudb for example, as long as I assign an interactive terminal to the web service.

I also wonder if I should start doing type annotations and add a mypy target. I don't think that Django yet has annotation or that any have been added to typeshed, which is a bit of a shame.