3 min read

Running a simple Django website in Docker

Using Docker to run a simple production and development environments with a few extras thrown in. Easily customized to your preferred language or framework.

Django Dockerfile
Author
Isaac Bythewood Isaac Bythewood
May 14, 2022

Docker is a near perfect solution for having project portability across a wide variety of host platforms. I use docker to run my websites on both development computers and servers. For Django my go to Dockerfile looks a little something like this.

A few notes on my choices:

  • This assumes you are just using an sqlite database but this can easily scale into using PostgreSQL in coordination with docker-compose
  • I use webpack to build all of my static files on all of my sites hence why nodejs and yarn are included
  • Chromium is used in most of my projects for generating PDFs and screenshots, I've found it the most reliable and consistent way of handling that functionality
  • You'll need both gunicorn and uvicorn installed

You could remove Chromium and save ~400MB of space on a roughly ~450MB image if you have no use for it. It is by far the largest dependency here. I also often use docker-compose in conjunction with this Dockerfile.

This can be used directly in production pretty well however I do put most of my websites behind Caddy using a reverse proxy. If you'd like to see my most up-to-date alpine-docker files you can check them out on my overshard/dockerfiles GitHub project.


Related posts

Some posts in similar tags to this one.

Caddy website
Caddy configuration for Django with some sensible defaults
Caddy is a great web server with sensible defaults but there a few things that I need to configure to have perfect synergy with Django.
Isaac Bythewood Isaac Bythewood
June 04, 2022
CodeMirror website
Using CodeMirror to show formatted code in Wagtail
Going through all the steps to use CodeMirror with Wagtail to show formatted code on the frontend of your site.
Isaac Bythewood Isaac Bythewood
June 11, 2022
Lighthouse PWA optimized check
Creating a PWA with Next.js and next-pwa to improve your websites UX
Turning your website into a PWA, especially if your website doesn't rely on an internet connection at all, can greatly improve it's user experience by allowing them to access your web app anywhere.
Isaac Bythewood Isaac Bythewood
June 18, 2022
© 2024 Isaac Bythewood. Some rights reserved.