Skip to content

Python · deploy guide

Host a Python web app (FastAPI or Django) with PostgreSQL

Run a FastAPI or Django app on Bashrack hosting from your GitHub repository, with a managed PostgreSQL database.

about 15 minutes of hands-on time

Who it’s for

Developers with a FastAPI or Django project in GitHub.

You’ll need

  • A GitHub repository with requirements.txt
  • Python 3.11 or newer

Steps

  1. step 1

    Dockerfile for FastAPI

    Uvicorn serves the app on the port we pass in PORT, on all interfaces.

    dockerfile
    FROM python:3.12-slim
    WORKDIR /app
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt
    COPY . .
    EXPOSE 8000
    CMD ["sh", "-c", "uvicorn main:app --host 0.0.0.0 --port ${PORT:-8000}"]
  2. step 2

    Django instead

    Use gunicorn. Add your app's address (for example my-app.bashrack.net) to ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS.

    dockerfile
    CMD ["sh", "-c", "gunicorn mysite.wsgi --bind 0.0.0.0:${PORT:-8000} --workers 2"]
  3. step 3

    Read DATABASE_URL

    psycopg reads the URL directly, sslmode=require included. For Django, dj-database-url turns it into the DATABASES setting.

    python
    import os, psycopg
    conn = psycopg.connect(os.environ["DATABASE_URL"])
    
    # Django settings.py
    import dj_database_url
    DATABASES = {"default": dj_database_url.config(conn_max_age=60)}
  4. step 4

    Create the app

    In the console, open Hosting → New app, connect GitHub and pick the repository and branch. Leave the port empty: after the first build we use the port your Dockerfile EXPOSEs. If you need a database, tick Add a PostgreSQL database in the same dialog.

  5. step 5

    Connect the database

    If the database was ordered separately, open it and choose Use in app: pick the app and the variable name. The app restarts with the variable set. Never commit the connection string to the repository.

Security and upkeep

  • Keep SECRET_KEY and other secrets in the app's environment variables
  • Set DEBUG off in production
  • Pin dependency versions in requirements.txt

Official documentation