# Fly.io (/docs/starter/deploy/fly)

`fly.toml` deploys the production image to [Fly.io](https://fly.io).

A project with a [frontend and its API](../../cli/frontend-and-api.mdx) can't use this option yet: it deploys on [any container host](./container-host.mdx#a-frontend-and-its-api) or [Railway](./railway.mdx#a-frontend-and-its-api). Nor can a project [without a website](../../cli/without-a-website.mdx), whose API deploys on [any container host](./container-host.mdx#without-a-website) or [Railway](./railway.mdx#without-a-website): the CLI refuses this option for it.

<Callout>
  [`pnpm clubedge deploy`](../../cli/deploy/fly.mdx) does these steps for you, databases included.
</Callout>

## What fly.toml sets [#what-flytoml-sets]

- **Build:** the root `Dockerfile`. With Next.js, set `NEXT_PUBLIC_APP_URL` under `[build.args]`, since Next.js compiles it into the build.
- **Release command:** `node /app/migrate.mjs` runs in a temporary machine with the new image, before it replaces the running one. A failed migration stops the deploy.
- **HTTP service:** port 3000 with HTTPS forced, machines that stop when idle and start on the next request, and a check on `/api/health` every 30 seconds.
- **VM:** 512 MB of memory.
- **Client address:** `CLIENT_IP_HEADER = "fly-client-ip"` under `[env]`, since Fly's proxy puts each [client's address](../before-deploying.mdx#client-addresses) in `Fly-Client-IP` (Starter v0.27.0 or newer).

## Deploy by hand [#deploy-by-hand]

Install [Fly's CLI](https://fly.io/docs/flyctl/install/) and sign in with `fly auth login`, then:

```sh
fly launch --no-deploy        # creates the app; keep the existing fly.toml
fly secrets set DATABASE_URL="postgresql://..."
fly deploy
```

`fly launch` adds the app's name and region to `fly.toml`; commit it. Set the other [production variables](./index.mdx#production-variables) with `fly secrets set`. For a database on Fly, create Postgres and attach it with `fly postgres attach`, which sets `DATABASE_URL`; with `cache redis`, `fly redis create` gives you Upstash Redis.

## Background jobs [#background-jobs]

Fly runs the app as a long-lived server, so the [jobs](../modules/jobs.mdx) worker runs with it. Machines that stop when idle stop the worker too: set `min_machines_running = 1` in `fly.toml` when jobs must run on time.

## The deploy workflow [#the-deploy-workflow]

The Deploy job runs `flyctl deploy --remote-only`: Fly's builders build the image, and the release command runs the migrations before the switch. It needs the secret `FLY_API_TOKEN`, from `fly tokens create deploy`.
