This is a boilerplate repo intended for quickly starting a new Django project with PostgreSQL and Redis support, all running within Docker containers. A Nginx service is also defined to enable immediate access to the site over port 80, with production hosting over HTTPS made possible via Cloudflare Tunnel.
- Docker
- Pipenv
- Make sure Python3 is available
- Enables
pipenv install
to set up libraries locally for the editor to crawl. The Django container also uses Pipenv to install dependencies to encourage use of this new Python package management tool.
- Clone this repo
- Delete the .git folder
rm -rf .git/
- Create a new git repo
git init
git add .
git commit -m "Initial Commit"
- Install Python dependencies in a Python3 virtual environment
pipenv install --three
- Create a new Django project
pipenv run django-admin startproject appname _app/
- Make the following changes to your Django project's settings.py:
# appname/settings.py
import os
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.getenv('DEBUG', False) == 'true'
ALLOWED_HOSTS = [
'localhost',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.getenv('POSTGRES_USER'),
'USER': os.getenv('POSTGRES_USER'),
'PASSWORD': os.getenv('POSTGRES_PASSWORD'),
'HOST': 'db',
'PORT': 5432,
}
}
STATIC_ROOT = 'static'
# Redis cache support
# https://docs.djangoproject.com/en/4.0/topics/cache/#redis-1
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.redis.RedisCache',
'LOCATION': 'redis://redis:6379/1',
}
}
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
SESSION_CACHE_ALIAS = 'default'
- Update the .env file to specify values for the environment variables defined within
- Do a global search of all files here for "appname" and replace it with the actual name of your app
- Start Django for development at http://localhost:8000
docker compose up
Builds the Django container. The container is built from a standard python Docker image and will run Django's colletstatic
when being built.
Tasked with spinning up three containers: the above container for Django, one for PostgreSQL, and one for Redis.
By default an Nginx container is also created to reverse-proxy requests to Django and serve static files. In this configuration, the Django server will be available on port 80 during production.
If the Nginx container is removed, Docker can be accessed directly on port 8000. Static files can then be served from the static_files_volume Docker volume.
Loads automatically when running a standard docker-compose up
. These values are intended for development purposes as they tweak the container configurations in the following way:
- Make the Postgres container accessible externally on port 5432
- Make the site available through Nginx at http://localhost:8000
- Start gunicorn to reload when it detects a file change
- Set an environmental flag telling Django that it is running in debug mode
Includes Python packages needed to make Django, Postgre, and Redis work together.
Contains environment variables for the containers. Several variables are included for configuring Postgres and Django secrets.
Defines files that Docker should never include when building the Django image.
Defines some common settings to help ensure consistency of styling across files.
Configures the flake8 Python linter. Includes a few common settings to my personal preferences.
Helps configure the Python plugin to lint with flake8. A placeholder Python interpreter setting is left in to simplify pointing to the local virtual environment created with Pipenv.
Defines settings for gunicorn, including a port binding, workers, and a gunicorn-specific error log.
Establishes a reverse-proxy to Django, and serves Django static files. The default template leverages the nginx Docker image's ability to reference environment variables via envsubst; this is how the PROD_HOST_NAME
environment variable is referenced for production access.
An executable script for starting the server in development mode.
An executable script for starting the server in production mode.
An executable script for rebuilding and restarting production Django whenever there's a change. This script also restarts Nginx to ensure no traffic hits the server while the update occurs.
The cloudflaretunnel
service in docker-compose.yml can be used to set up HTTPS access to Django via Cloudflare Tunnel. To get started, follow these steps:
- Log into the Cloudflare Zero Trust dashboard
- Click Access > Tunnels
- Click Create a tunnel
- Specify a Tunnel name
- Click Docker on the Install connector step
- Save the value of the
--token
flag in the page'sdocker
command to this project's .env file as theCLOUDFLARE_TUNNEL_TOKEN
environment variable - Run start-prod.sh to start the tunnel and display an entry under Connectors
- Click Next
- Set up a Public hostname
- For the Service select "HTTP" and then enter "nginx"
- Click Save <tunnel name> tunnel to complete setup
- Set the
PROD_HOST_NAME
variable in the .env file to the tunnel's configured Public hostname
You will also need to make the following change to Django's _app/appname/settings.py to add PROD_HOST_NAME
as an allowed hostname:
ALLOWED_HOSTS = [
os.getenv("PROD_HOST_NAME", ""),
"localhost",
]
When these steps are complete, running start-prod.sh should make Django available on the public internet at https://$PROD_HOST_NAME
. No firewall ports need to be opened on the production host, and in fact you may wish to set up the firewall to block all incoming traffic for good measure.
You probably forgot to replace the string "appname" with the actual name you passed to django-admin startproject
. Check _app/gunicorn_appname.log to see if Gunicorn is erroring out with something like this:
ModuleNotFoundError: No module named 'appname'