Live Docker NocoDB Django Postgres Project:
3. Django Backend Introduction
Company administration Django backend made by team members (repo/company_backend/README.md)
Production PowerPC (ppc64le) server:
- https://BRANCHNAME-www.launch.rentals for
presentation_frontendservice - https://BRANCHNAME-www.launch.rentals/api for
presentation_backendservice - https://BRANCHNAME-company.launch.rentals for
company_admin_frontendservice - https://BRANCHNAME-company.launch.rentals/api for
company_admin_backendservice - alternative access using IP: 147.175.146.249, includes HTTP redirect and HTTPS (let's encrypt with manual rotation for now)
First time setup inside Docker (Recommended)
Just do what is in ../README.md
Explanation: Docker uses the Dockerfile to set all of this up for you including running the uvicorn company_backend.asgi:application --host 0.0.0.0 --port 8000 --reload command internally and even reroutes traffic through nginx instead of directly accessing this server. It also sets up database (required), presentation_frontend for fullstack development and proper URL routing for the "/api" route. If you need to use python3.12 manage.py ... for anything within the container access it using docker exec -it CONTAINER_ID where CONTAINER_ID is the backend container ID from docker container ls -a.
First time setup OUTSIDE Docker
DO NOT DO THIS ON WINDOWS YOU WILL REGRET IT, python tends to fail in finding mariadb connector way too often, etc.
- Install Python 3.12 (e.g.
sudo apt install python3.12on Linux/macOS or https://python.org/downloads/windows if running using Powershell) - Try to install Django Admin globally:
python3.12 -m pip install Django==5.1.2(if pip isn't working:sudo apt install pip) (if it breaks because "externally managed dependency" error, just continue with step 3) - Use venv:
python3.12 -m venv .venv - Source the venv:
- Linux, macOS:
source .venv/bin/activate - Windows Powershell:
.\venv\Scripts\Activate.ps1(Please use PowerShell (Windows Explorer inpresentation_backendfolder -> File -> PowerShell as Admin) or WSL if you know how to use WSL already, don't use Git Bash or cmd.exe)
- Make sure python is running under venv:
which python3.12orwhere python3.12should show the venv directory's python - Install dependencies:
python3.12 -m pip install -r requirements.txt
- If installing MariaDB fails on Windows:
- Check error logs how MariaDB gets built, you may need to install
MariaDB Connector C 3.3.14or similar:
https://mariadb.com/docs/server/connect/programming-languages/c/install/ - After installation rename the
C:\Program Files\MariaDB\MariaDB Connector C 64-bittoC:\Program Files\MariaDB\MariaDB Connector C - Make sure PATH is properly setup and pointing to the
MariaDB Connector C\binfolder
- Check error logs how MariaDB gets built, you may need to install
- Launch two MariaDB servers (or change db to sqlite - not recommended)
- Go to project root folder where
docker-compose.ymlis located withcd .. - Ideally use
docker-compose-backend-light-dev.ymlusingdocker compose -f docker-compose-backend-light-dev.yml up -din root directory if you only want the databases in Docker - Services will each run on different ports: if you get error on the ports
3306and/or3307then you have to use different ports that are available on your machine!nginx-random-ports.shhas some logic used in workflows for randomizing ports, but isn't intended for this, usenetstat -lntuon Linux to check busy ports andnetstat -anoon Windows; ports that worked for one of us due to conflicts after multiple tries were all in the 50k range:53306and53307 - Go back to the backend project:
cd presentation_backend
- Set your enviroment variables:
cp .env.example-backend-light-dev .envand edit them in your favorite editor except the prefilled ones - MAKE SURE ALL VARIABLES ARE SET! - Activate your enviroment variables:
source .envor on windows PowerShell.\loadenv.ps1 - Run the server:
uvicorn company_backend.asgi:application --host 0.0.0.0 --port 8000 --reload
Subsequent runs
- Source the venv:
- Linux, macOS:
source .venv/bin/activate - Windows Powershell:
.\venv\Scripts\Activate.ps1
- Launch two MariaDB servers as before
- Activate your enviroment variables:
source .envor on windows.\loadenv.ps1 - Run the server:
uvicorn company_backend.asgi:application --host 0.0.0.0 --port 8000 --reload
How the boilerplate was created
django-admin startproject company_backend && mv company_backend/* .
Docker Compose Snippet
# Django backend for Public Presentation
presentation_backend:
build:
dockerfile: Dockerfile
context: presentation_backend
environment:
- PRESENTATION_DJANGO_SECRET_KEY=...
- ...
volumes:
- ./presentation_backend:...
depends_on:
presentation_db:
condition: service_healthy
healthcheck:
test:
["CMD-SHELL", "wget ..."]
start_period: 20s
interval: 10s
timeout: 5s
retries: 10
networks:
- common_network
restart: unless-stopped
logging:
driver: ...
options:
syslog-address: ...
tag: ...
# Django backend for Company Administration
company_admin_backend:
build:
dockerfile: Dockerfile
context: company_backend
environment:
- COMPANY_DJANGO_SECRET_KEY=...
- ...
volumes:
- ./company_backend:...
depends_on:
company_admin_db:
condition: service_healthy
healthcheck:
test:
["CMD-SHELL", "wget ..."]
start_period: 20s
interval: 10s
timeout: 5s
retries: 10
networks:
- common_network
restart: unless-stopped
logging:
driver: ...
options:
syslog-address: ...
tag: ...