Skip to content

Toy project. Trying to use Django inside a Docker container for both local development and production deployment.

Notifications You must be signed in to change notification settings

mimischi/django-docker

Repository files navigation

Develop and deploy Django applications with Docker

Build Status codecov PRs Welcome

This repository is used to test a new project layout to develop Django applications within Docker containers. To be very fancy, we're also using poetry.toml instead of requirements.txt for our Python dependencies. Deployment to production is handled by a remote Dokku instance.

Details

We're using Python 3.7-slim for the base image as a trade-off of container size and build time. Further we utilize poetry to try out a new approach of Python requirement management.

Features

  • Develop inside of Docker containers! (Both Django and PostgreSQL run inside of their own containers)
  • Django runserver_plus is sequentially restarted, if the application crashes for any reason.
  • Use Makefile for common commands (docker-compose build, python manage.py makemessages, ...).
  • Uses WhiteNoise to manage static files.
  • Run continuous integration of Travis-CI.
  • Deploy to Dokku for production.
  • Use Sentry for error reporting on your production instance.

Note: In the current layout, with the Dockerfile residing under ./docker/dokku/Dockerfile, you will need to install the dokku-dockerfile plugin and set the path accordingly.**

Planned

  • It would be neat to get Celery to work.

Usage

Local development

Running make build will download all required images (python:3.7-slim and postgresql:9.6-alpine) and build the app. Next you need to run make migrate to run all database migrations, after which you can actually start using this project. Running docker-compose up will collect all staticfiles and start both services. The app will be available via localhost:8000.

Deployment to production (via Dokku)

Prepare app on Dokku host

Before deployment, one needs to set up the app and PostgreSQL database on the Dokku host. For the sake of simplicity we're going to name the Dokku app djangodocker in this example.

# Create app
$ dokku apps:create djangodocker

# Create PostgreSQL database and link it to the app
$ dokku postgres:create djangodocker-postgres
$ dokku postgres:link djangodocker-postgres djangodocker

# Set the bare minimum configuration
$ dokku config:set --no-restart djangodocker DJANGO_ADMIN_URL="/admin"
$ dokku config:set --no-restart djangodocker DJANGO_ALLOWED_HOSTS=djangodocker.example.com
$ dokku config:set --no-restart djangodocker DJANGO_SECRET_KEY=$(echo `openssl rand -base64 100` | tr -d \=+ | cut -c 1-64)
$ dokku config:set --no-restart djangodocker DJANGO_SETTINGS_MODULE=config.settings.production
$ dokku config:set --no-restart djangodocker DJANGO_SENTRY_DSN=https://your:[email protected]/1234
# Make sure the plugin `dokku-dockerfile` is installed
$ dokku dockerfile:set djangodocker docker/dokku/Dockerfile

You may also need to set the domain using dokku domains:set djangodocker djangodocker.example.com.

Setup Dokku server as git remote

To successfully push your app to the Dokku host, you need to set up the server as a git remote:

git remote add dokku [email protected]:djangodocker

Deployment

Deploying the master branch of the app is straightforward:

git push dokku master

If you want to deploy another branch (e.g. newfeature), you need to use this syntax:

git push dokku newfeature:master

More information can be found in the official Dokku documentation.

About

Toy project. Trying to use Django inside a Docker container for both local development and production deployment.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published