Skip to content
/ tpl-drf Public template

Production ready Django+DRF template (Docker Compose for local development and prod)

License

Notifications You must be signed in to change notification settings

baikov/tpl-drf

Repository files navigation

Django (DRF) template for REST API backend

Based on Cookiecutter Django Black code style Ruff

Other parts:

  1. Traefik 2.10 as revers-proxy in Docker (SSL in dev and prod)
  2. Nuxt 3 production-ready template in Docker (SPA/SSR)
  3. Django/DRF backend in Docker (based on django-cookiecutter)

Features

  • Custom User model
  • Djoser registration
  • Celery + Redis + Flower
  • Sentry in prod
  • Nginx for media in prod
  • Silk profiling in dev

Improvement plan

  • add logging

Local development

Preparation

  1. Create virtual environment
    python -m venv venv
  2. Activate virtual environment
    source venv/bin/activate
  3. Install requirements
    pip install -r requirements/local.txt
  4. Install and activate all recomended VSCode extensions
  5. Install pre-commit
    pre-commit install
  6. Install Docker Desktop
  7. Make sure ruff and mypy are working
    ruff check backend/
    mypy backend/
  8. Copy .env.example and rename it to .env on local machine

Choose one of .env presets.

To use Mode 1 and Mode 2, a raised container from this repo with Traefik is required. Because an external network to which frontend and backend containers are connected is created in Traefik compose.

Mode 0: As separate dev server on custom port

  1. No need for a Traefik container
  2. Set uniqe project name
    COMPOSE_PROJECT_NAME=uniqe_name
  3. Uncomment Mode 0 block and set custom ports if needed:
    # Mode 0: As separate dev server on custom port
    COMPOSE_FILE=local.yml
    DOMAIN=localhost
    DJANGO_DOCKER_PORT=8000
    MAILHOG_DOCKER_PORT=8025
    FLOWER_DOCKER_PORT=5555
    DOCS_DOCKER_PORT=9000
  4. Run docker compose build and docker compose up -d

Mode 1: As dev server behind the Traefik with http

Avaliable paths: dj-admin/, api/, ststic/, silk/. Other paths are proxied to the frontend container

  1. The Traefik container must be running in Mode 1
  2. Set the project name same as COMPOSE_PROJECT_NAME in Traefik .env
    COMPOSE_PROJECT_NAME=example
  3. Uncomment Mode 1 block:
    # Mode 1: As dev server behind the Traefik with http
    # For Windows users: use `;` (semicolon) as separator - local.yml;local.traefik.yml
    COMPOSE_FILE=local.yml:local.traefik.yml
    DOMAIN=localhost  # or another aliace for 127.0.0.1 declared in etc/hosts, but same as DOMAIN in Traefik .env!
  4. Run Traefik container, then run Django stack with docker compose build and docker compose up -d

Mode 2: As dev server behind the Traefik + SSL and custom domain

Avaliable paths: dj-admin/, api/, ststic/, silk/. Other paths are proxied to the frontend container

  1. The Traefik container must be running in Mode 2
  2. Set the project name same as COMPOSE_PROJECT_NAME in Traefik .env
    COMPOSE_PROJECT_NAME=example
  3. Uncomment Mode 2 block:
    # Mode 2: As dev server behind the Traefik + SSL and custom domain
    # For Windows users: use `;` (semicolon) as separator - local.yml;local.traefik.yml;local.traefik.ssl.yml
    COMPOSE_FILE=local.yml:local.traefik.yml:local.traefik.ssl.yml
    DOMAIN=tpl.local  # same as DOMAIN in Traefik .env!
  4. Run Traefik container, then run Django stack with docker compose build and docker compose up -d

Usefull commands from django-cookiecutter

  1. Show Logs (e.g. Django)
    docker logs -f django
    # or
    docker compose logs -f django
  2. Migrations
    docker compose run --rm django python manage.py makemigrations
    docker compose run --rm django python manage.py migrate
  3. Django shell_plus with ipython
    docker compose run --rm --name django_shell django python manage.py shell_plus --ipython
  4. Create DB backup (placed in ./pg-backups)
    docker compose exec postgres backup
  5. Show backups list
    docker compose exec postgres backups
  6. Restore backup (only from gzip)
    docker compose exec postgres restore backup_2023_05_26T12_34_08.sql.gz
  7. Hot clean DB (close all connections)
    docker compose exec postgres restore clean

Deploy to production

Using this modes assumes that the your-domain.com is already bound to your server (A records are configured) and Traefik container raised in production mode

Mode 3: when front in SSR mode

  1. The Traefik container must be running in Mode 3 on prod server
  2. Copy .env.production.example and rename it to .env
  3. Set the project name same as COMPOSE_PROJECT_NAME in Traefik .env
    COMPOSE_PROJECT_NAME=example
  4. Uncomment Mode 3 block:
    # Mode 3: For production with SSR
    COMPOSE_FILE=production.yml
    DOMAIN=your-domain.com
  5. Change all secure variables
  6. Add Sentry DSN and Email settings (optional)
  7. Run container with docker compose build and docker compose up -d

Mode 4: when front in SPA mode

coming soon...

Contributing

I made this template for myself, but it's awesom if it helps someone else. The settings are far from ideal, so fell free to make a pull request.

About

Production ready Django+DRF template (Docker Compose for local development and prod)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages