> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reliantlabs.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy FastAPI with Reliant Forge

> Deploy a FastAPI app with Postgres and Alembic migrations from its own Dockerfile: Forge adopts it in place, runs alembic upgrade head before every rollout, and ships it to Reliant hosting.

You will take a FastAPI app that already has a Dockerfile, a `docker-compose.yml` with Postgres and Alembic migrations, and deploy it to [Reliant hosting](https://docs.reliantlabs.io/hosting/overview.md) with a managed Postgres. Forge reads the Dockerfile and the compose file and writes its declarations beside them; none of your Python changes. This guide was run on a minimal notes API at Forge `v1.10.1-0.20261123064811-54313eead62f`.

## Before you begin

* Docker, and the app in a git repository with a root `Dockerfile` that builds on its own.
* Forge: `reliant forge` inside Reliant, or `npx -y @reliantlabs/forge@latest` anywhere else.
* For a deploy that lasts, a Reliant account with [app hosting billing](https://docs.reliantlabs.io/hosting/overview.md#billing). With no account you can still publish a 72-hour preview (step 6).

## What the Dockerfile must do

Forge never builds your Python itself; a deploy runs `docker build` on the Dockerfile and its context, nothing else. The detector reads the port from the compose service's published port, else `EXPOSE`, else a `--port` in the start command, else `ENV PORT`, and sets `PORT` in the container. So listen on `0.0.0.0` and on `$PORT`:

```dockerfile Dockerfile theme={null}
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN useradd -r -u 10001 app
USER 10001
ENV PORT=8000
EXPOSE 8000
CMD ["sh", "-c", "exec uvicorn app.main:app --host 0.0.0.0 --port ${PORT}"]
```

Uvicorn's default host is `127.0.0.1`, which nothing outside the container can reach: the probe fails and the rollout never becomes ready. Keep `alembic` and `alembic.ini` in the final image, because the migration job runs this same image. Add `deploy`, `.forge` and `secrets` to `.dockerignore`, so a deploy-config edit leaves the image digest alone.

## Postgres and migrations

Read the database URL from `DATABASE_URL`. Hosting injects a Postgres URL there. SQLAlchemy with psycopg 3 wants the `postgresql+psycopg://` scheme, so rewrite `postgres://` or `postgresql://` to it in `migrations/env.py` and wherever you create the engine. The detector turns any of these into a `migrate` job that runs before every rollout: a compose service that migrates, or an `alembic.ini`. In the test app the compose file has one:

```yaml docker-compose.yml (excerpt) theme={null}
  migrate:
    build: .
    command: ["alembic", "upgrade", "head"]
    environment:
      DATABASE_URL: postgresql://postgres:postgres@db:5432/notes
    depends_on:
      db: {condition: service_healthy}
  api:
    depends_on:
      migrate: {condition: service_completed_successfully}
```

Use `service_completed_successfully`: with a plain `depends_on`, `forge env up dev` treats the migration container's exit as a failed service. Don't run `alembic upgrade` inside the app's startup as well: the job already ran it, and every replica racing to migrate is how half-migrated schemas happen.

**1. Preview what Forge would write**

```bash terminal theme={null}
reliant forge project new --in-place --kind app --dry-run
```

```text Output (trimmed) theme={null}
Detected:
  - Postgres: compose service "db" (postgres:16) → a managed Postgres on staging and prod; dev keeps the compose service
  - workload api (service): compose service "api", builds Dockerfile, port 8000, reads DATABASE_URL
  - migrations: alembic upgrade head (compose service "migrate") → job migrate, before every rollout
Where it runs:
  - dev: docker-compose.yml, through docker compose, as before
  - staging and prod: Reliant hosting (forge.OnHosted), built once and promoted by digest
```

**2. Adopt the app**

```bash terminal theme={null}
reliant forge project new --in-place --kind app --name notes-api
git add -A && git commit -m "Adopt with forge"
```

It writes `forge.yaml` and `deploy/kcl/` and appends a block to `.gitignore`. Open `deploy/kcl/workloads.k` and read every `# check:` line. Each compose variable that looks like a credential (`APP_SECRET: ${APP_SECRET}` here) becomes a `forge.ManagedSecret` on staging and prod.

**3. Build, render and lint**

```bash terminal theme={null}
docker build .
reliant forge env render prod
reliant forge lint
```

```text Output (trimmed) theme={null}
hosted (published to the control plane, not applied):
  migrate (job)
  api (service)
  notes (database)
✅ 13 gating linter(s) passed; 2 skipped (contract linter, migration safety lint)
```

`env render` is the dry run of a deploy. Run it locally with `reliant forge env up dev`, which runs your compose file.

**4. Set secrets**

The value is read from stdin, never from the command line:

```bash terminal theme={null}
openssl rand -hex 32 | reliant forge env secret set --env prod APP_SECRET
```

Then read the app the way the detector can't: `os.environ.get("APP_SECRET", "dev")` silently runs prod with the dev value if the secret is never delivered. Use `os.environ["APP_SECRET"]` so a missing secret fails at boot.

**5. Deploy**

Signed in, to your organization:

```bash terminal theme={null}
reliant forge env deploy prod --yes
```

A hosted service and database need app hosting billing. Without it, the deploy is queued and exits **7** with the link to set billing up; nothing needs re-running afterwards. The default size is 250m CPU and 1 GiB of memory per workload.

With no account, adopt with `--claimable` instead, so prod fits the anonymous plan, preview it, then publish:

```bash terminal theme={null}
npx -y @reliantlabs/forge@latest project new --in-place --kind app --claimable
npx -y @reliantlabs/forge@latest env deploy prod --claimable --plan-only
npx -y @reliantlabs/forge@latest env deploy prod --claimable
```

An unclaimed deploy is one service at 125m CPU and 128 MiB, its migration job and one Postgres of 256 MiB of data. It gets no secret values and **no outbound internet** until someone opens the claim link, so a FastAPI app that calls an outside API at startup won't come up. It stays online for 72 hours. The test app idled at about 50 MiB under a 128 MiB limit with one uvicorn worker; each extra `--workers` process adds roughly that again. Inside Reliant you are signed in, so `--claimable` is refused: drop it. Details: [Deploy without an account](https://docs.reliantlabs.io/forge/deploy-without-an-account.md).

**6. Verify**

```bash terminal theme={null}
reliant forge env status prod
curl -s -X POST -H 'Content-Type: application/json' -d '{"body":"hi"}' https://<your-url>/notes
reliant forge obs check prod --wait --json
```

`env status` prints the URL. Hit an endpoint that reads and writes the database, not just `/docs`. Then branch on `obs check`'s exit code: **0** the release is proven good, **1** it regressed (run the `suggested[]` commands), **2** not proven, **8** the wait ran out before the after-window closed (check again later). Until the app sends OpenTelemetry traces it is exit 2, `reason_code: no_request_telemetry`: say "not proven good", never "passed". [Send OpenTelemetry from any app](https://docs.reliantlabs.io/guides/observability/send-opentelemetry-from-any-app.md) fixes that (`opentelemetry-instrumentation-fastapi`).

## FastAPI gotchas

* **Probe.** The scaffold probes TCP. Serve a `/healthz` and switch to `probes = tiers.Probes {readinessPath = "/healthz"}` in `workloads.k` once it checks the database.
* **Proxy headers.** TLS ends at the hosting ingress. If you build absolute URLs or redirects, run uvicorn with `--proxy-headers --forwarded-allow-ips='*'` so `request.url` says `https`.
* **Gunicorn.** `gunicorn -k uvicorn.workers.UvicornWorker -b 0.0.0.0:$PORT` works the same; size workers to the memory you declare.

## Troubleshooting

> [accordion] this content was refused by the platform's safety scan
>
> An unclaimed deploy's image is scanned before it goes live, and the refusal (`reason: claimable_content_refused`) doesn't name what was found. It happened to our minimal Express test image; the release was built and recorded, but nothing was published. Deploy signed in instead, with `reliant forge env deploy prod`.

> [accordion] has a Dockerfile and no Go module: an app in another language
>
> You ran `project new --in-place` without `--kind app`. Nothing was written.

See also: [Bring an existing app](https://docs.reliantlabs.io/guides/forge/bring-existing-app-any-language.md), [Managed Postgres](https://docs.reliantlabs.io/hosting/managed-postgres.md), [Secrets](https://docs.reliantlabs.io/hosting/secrets.md), [Hosting limits](https://docs.reliantlabs.io/hosting/limits.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.