> ## 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 Rails with Reliant Forge

> Deploy a Ruby on Rails app with Postgres from its own Dockerfile: Forge adopts it in place, runs bin/rails db:migrate before every rollout, and ships it to Reliant hosting.

You will deploy an existing Rails app, using the Dockerfile Rails 7.1+ generates, to [Reliant hosting](https://docs.reliantlabs.io/hosting/overview.md) with a managed Postgres. Forge reads the Dockerfile, the `Gemfile` and `db/migrate` and writes its declarations beside them; no Ruby changes. The detector output here is from 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).

## What the Dockerfile must do

The generated Rails Dockerfile is close to right: it builds in stages, precompiles assets with `SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile`, and runs as the non-root `rails` user. Check two things. Rails 8's default `CMD` starts Thruster on port 80, which a non-root user can't always bind and which hosting's TLS already covers; serve Puma directly on a high port, on all interfaces:

```dockerfile Dockerfile (end) theme={null}
USER 1000:1000
EXPOSE 3000
CMD ["./bin/rails", "server", "-b", "0.0.0.0", "-p", "3000"]
```

The detector reads the port from `EXPOSE` (or a `-p`/`-b` in the command, the compose port, or `ENV PORT`) and sets `PORT`. Remove the `ENTRYPOINT ["/rails/bin/docker-entrypoint"]` line's `db:prepare` call, or leave it knowing that every replica start runs it: the migration job below already migrated.

## Production settings

* **`RAILS_MASTER_KEY`** (or `SECRET_KEY_BASE`). Production refuses to boot without one. Deliver it as a secret, never commit `config/master.key`.
* **`force_ssl`.** TLS ends at the hosting ingress, which forwards `X-Forwarded-Proto: https`. Keep `config.force_ssl = true` and `config.assume_ssl = true`, so Rails trusts the header instead of redirecting in a loop.
* **Host authorization.** If `config.hosts` is set, add the URL hosting gives you, or every request is `Blocked hosts`. The TCP probe doesn't send a host, so it is unaffected; an HTTP probe on `/up` should be excluded with `config.host_authorization = { exclude: ->(r) { r.path == "/up" } }`.
* **Logs to stdout.** `RAILS_LOG_TO_STDOUT=1` (the default in Rails 7.1+ production) so `reliant forge obs logs` sees them.

## Postgres and migrations

With `pg` in the `Gemfile`, the detector adds a managed Postgres on staging and prod and passes its URL as `DATABASE_URL`, which Rails reads with no `database.yml` change. With `db/migrate`, it adds a `migrate` job, `bin/rails db:migrate`, run from the app's image before every rollout. Rails 8's `solid_queue`/`solid_cache` second databases need their own `DATABASE_URL`-style variables; an anonymous deploy allows only one database.

**1. Preview and adopt**

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

```text Output (trimmed) theme={null}
Detected:
  - workload api (service): builds Dockerfile, port 3000
  - Postgres: a managed Postgres on staging and prod; dev runs a forge host-native Postgres
  - migrations: bin/rails db:migrate (Rails' db/migrate) → job migrate from api's image, before every rollout
Assumed (each is a `# check:` line in deploy/kcl; review them):
  - dev runs api's Dockerfile CMD on the host (bin/rails server -b 0.0.0.0 -p $PORT): change it in deploy/kcl/dev/main.k if the app has a dev server
  - api uses Postgres (the Gemfile names pg) and reads its URL from DATABASE_URL
```

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

**2. Deliver the master key**

With no compose file, the detector can't see `RAILS_MASTER_KEY`, so add it to both workloads in `deploy/kcl/prod/main.k` (the migration job boots Rails too):

```python deploy/kcl/prod/main.k (excerpt) theme={null}
_hosted(wl.migrate) | {
    env.DATABASE_URL = _database_url
    env.RAILS_MASTER_KEY = forge.ManagedSecret {name = "RAILS_MASTER_KEY"}
}
_hosted(wl.api) | {
    env.DATABASE_URL = _database_url
    env.RAILS_MASTER_KEY = forge.ManagedSecret {name = "RAILS_MASTER_KEY"}
}
```

```bash terminal theme={null}
reliant forge env secret set --env prod RAILS_MASTER_KEY < config/master.key
```

**3. Build, render, lint, deploy**

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

Without app hosting billing the deploy is queued and exits **7** with the billing link. The default size is 250m CPU and 1 GiB per workload; set `WEB_CONCURRENCY` and `RAILS_MAX_THREADS` to fit it.

With no account, `npx -y @reliantlabs/forge@latest env deploy prod --claimable` publishes a 72-hour preview (adopt with `--kind app --claimable`, then check with `--plan-only`). It is one service at 125m CPU and **128 MiB**, which a typical Rails app exceeds at boot, and it gets no secret values, so Rails production can't find its master key. For Rails, sign in and deploy to your organization. See [Deploy without an account](https://docs.reliantlabs.io/forge/deploy-without-an-account.md).

**4. Verify**

```bash terminal theme={null}
reliant forge env status prod
curl -s -o /dev/null -w '%{http_code}\n' https://<your-url>/up
reliant forge obs check prod --wait --json
```

Rails' `/up` returns 200 once the app booted. Then hit a page that reads the database. `obs check` exits **0** proven good, **1** regressed, **2** not proven, **8** the wait ran out; without an OpenTelemetry SDK (`opentelemetry-instrumentation-all`) it is 2, `no_request_telemetry`, which means not proven good. Once `/up` is reliable, switch the probe in `workloads.k` to `tiers.Probes {readinessPath = "/up"}`.

## Rails gotchas

* **`ActiveSupport::MessageEncryptor::InvalidMessage` at boot:** the master key doesn't match `credentials.yml.enc`. Set the right one; per-environment credentials read `config/credentials/production.key`.
* **Sidekiq or Solid Queue workers** are a second workload (`kind = "worker"`, same image, its own `command`). They need billing.
* **Active Storage on `:local`** writes to scratch disk that each rollout discards. Use an S3-compatible service.

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.