# Free Django hosting

Reverhost runs Django, a batteries-included Python web framework, in an always-on container built from your Dockerfile, for $0. No credit card is needed, and your agent can deploy it with one instruction.

## What you get

- An always-on container with 256 MB of RAM with no swap, 0.5 CPU and images up to 1 GB
- A URL at `<app-slug>.easyhost.fun`, plus custom domains with HTTPS
- One Postgres 16 database and one S3 bucket per project
- 2 apps per project, with a 128 MB database and 256 MB of storage

## How Django builds and runs

| Step | What your Dockerfile runs |
| --- | --- |
| Build | `pip install -r requirements.txt`, then `python manage.py collectstatic` |
| Start | `gunicorn mysite.wsgi:application --bind 0.0.0.0:$PORT` |
| Runtime env | `PORT=3000` and `HOSTNAME=0.0.0.0` from Reverhost, plus your own variables |

Add `django`, `gunicorn`, `whitenoise`, `dj-database-url` and `psycopg[binary]` to `requirements.txt`. Read `DATABASE_URL` with dj-database-url, serve static files with WhiteNoise, and set `ALLOWED_HOSTS` from an env var. Replace `mysite` with your project module.

```mysite/settings.py
import os

import dj_database_url

SECRET_KEY = os.environ["SECRET_KEY"]
DEBUG = False
ALLOWED_HOSTS = os.environ["ALLOWED_HOSTS"].split(",")
DATABASES = {"default": dj_database_url.parse(os.environ["DATABASE_URL"])}
STATIC_ROOT = "staticfiles"
```

## The Dockerfile

```Dockerfile
FROM python:3.12-slim
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 PORT=3000
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN --mount=type=secret,id=appenv,target=/run/secrets/appenv sh -c 'set -a; . /run/secrets/appenv; set +a; python manage.py collectstatic --noinput'
EXPOSE 3000
CMD ["sh", "-c", "exec gunicorn mysite.wsgi:application --bind 0.0.0.0:$PORT --workers 2"]
```

Save it as `Dockerfile` in your project folder. It is based on the [Python template](/languages/python).

## Deploy it

```
npx ezhst projects create my-project --link
npx ezhst apps create my-app
npx ezhst env set SECRET_KEY=<secret> ALLOWED_HOSTS=<app-slug>.easyhost.fun
npx ezhst build dockerfile set -f Dockerfile
npx ezhst env import .env
npx ezhst deploy
```

## Connect Postgres

```
npx ezhst db create main
npx ezhst db info --json
npx ezhst env set DATABASE_URL="postgresql://..."
npx ezhst redeploy
```

Use psycopg, asyncpg or SQLAlchemy with `os.environ["DATABASE_URL"]`. Add the driver to `requirements.txt`.

## Limits and caveats

Two gunicorn workers fit in 256 MB of RAM for a typical app. Run `python manage.py migrate` from your machine with `DATABASE_URL` set to the Reverhost connection string.

Apps run in one region, with no edge network, preview deployments or Git integration. See [all frameworks](/frameworks) and [Python hosting](/languages/python).

## Frequently asked questions

### Is Django hosting on Reverhost really free?

Yes. It costs $0 per month with no credit card and no trial period, within the published limits.

### Does my Django app sleep when idle?

No. Reverhost apps run in an always-on container and do not sleep when idle.
