Kristian Glass - Do I Smell Burning?

Packaging Django with Docker

I love Docker as a packaging system - almost everything I push to production nowadays is as a Docker image.

Occasionally in the #django IRC channel, the topic of “how to Docker-ise your Django service” comes up. Here’s a template Dockerfile I use, taken from My Django Project Template:

FROM python:3

COPY requirements.txt /opt/{{ cookiecutter.org_name }}/{{ cookiecutter.app_name }}/
WORKDIR /opt/{{ cookiecutter.org_name }}/{{ cookiecutter.app_name }}/

RUN pip install --no-deps --requirement requirements.txt

COPY . /opt/{{ cookiecutter.org_name }}/{{ cookiecutter.app_name }}/

RUN python {{ cookiecutter.app_name }}/manage.py collectstatic --noinput

EXPOSE 8000

CMD gunicorn --chdir {{ cookiecutter.app_name }} --bind 0.0.0.0:8000 {{ cookiecutter.app_name }}.wsgi

It’s does relatively little; it installs dependencies, calls collectstatic, and runs. Nothing else. Most other things are the job of the platform I deploy to.

It uses gunicorn as a WSGI HTTP server, because it does exactly what I need and not much more. See my Django: An Unofficial Opinionated FAQ for more here.

It does very little about media and staticfiles, leaving them to be handled by (usually) AWS S3 and Whitenoise, respectively.

It runs as root, because this is normally a minimal concern in the environments I deploy to, and doing otherwise can get fiddly.

It does some minimal COPY splitting to avoid installing dependencies too needlessly with repeated builds and edits.

In short, it’s far from perfect and rarely what I end up with, but I find it a great place to start from.

Comments