/cookiecutter-django-docker

An opinionated Cookiecutter template for running Django in Docker

Primary LanguageShellBSD 3-Clause Clear LicenseBSD-3-Clause-Clear

Docker for Django Cookiecutter

An opinionated Cookiecutter template for running Django in Docker. Optimized for Cookiecutter Django.

Features

  • Uses PostgreSQL as database.
  • It is Webpack ready!
  • Uses Caddy as web-server (optionally with Cloudflare plugin).
  • Serves on HTTPS.
  • Poetry as package manager.
  • Includes a development mail server.
  • Includes example dotenv files compatible with dotenver.
  • Auto runs migrations on Django image.
  • Allows for custom SSL certificates to be used.

How to use

If you are not using the recommended Cookiecutter Django template, use the following instructions as a guide on how to use this cookiecutter.

Run the following commands inside you root projects directory.

# Bake cookie!
cookiecutter gh:jmfederico/cookiecutter-django-docker
# Automatically generate dotenv files
docker run --rm -v "`pwd`:/var/lib/dotenver/" jmfederico/dotenver
# Initialize poetry project
poetry init
poetry add django
poetry add psycopg2
# Create your Django project
poetry run django-admin startproject MY-PROJECT .

Now is a good idea to modify your settings.py file to use environmental variables:

SECRET_KEY = os.environ["SECRET_KEY"]

EMAIL_HOST = os.environ["EMAIL_HOST"]
EMAIL_PORT = os.environ["EMAIL_PORT"]

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": os.environ["DATABASE_NAME"],
        "USER": os.environ["DATABASE_USER"],
        "PASSWORD": os.environ["DATABASE_PASSWORD"],
        "HOST": os.environ["DATABASE_HOST"],
        "PORT": os.environ["DATABASE_PORT"],
    }
}
# Build and run Docker images
docker-compose build
docker-compose up -d

Now you can visit https://localhost/

Not using Webpack?

If you are not using Webpack you should delete the Webpack container from the docker-compose.yml file.

Running commands

The recommended way to run commands is inside the Django container:

# Create Django migrations
docker-compose run --rm django ./manage.py makemigrations