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

# Deploy a Ruby on Rails Application

> Deploy a Ruby on Rails application with PostgreSQL on Kubernetes in your own cloud account with Qovery: production Dockerfile, Puma, the /up health check, migrations as a lifecycle job, precompiled assets.

This guide deploys a Rails application served by Puma, with a PostgreSQL database and migrations that run before each release. Qovery builds the image from your Dockerfile and runs it on a Kubernetes cluster in your own cloud account.

> [!NOTE]
>
> **No configuration file in your repository.** Qovery does not read a `qovery.yml`, `.qovery.yml` or `.qovery.toml`, and there is no `qovery init`. Projects, environments and services are created in the Console, with `qovery api`, or with [Terraform](https://www.qovery.com/docs/terraform-provider/overview.md). For the full path from a repository to a running app with commands only, see [Deploy with the CLI and API](https://www.qovery.com/docs/getting-started/quickstart/cli-and-api.md).

## Prerequisites

* A Rails 7.1 or later application in a GitHub, GitLab or Bitbucket repository, using the `pg` gem.
* A Qovery account and a cluster: [create one in your cloud account](https://www.qovery.com/docs/getting-started/quickstart/cloud.md), [connect your own cluster](https://www.qovery.com/docs/getting-started/installation/kubernetes.md), or [try it on your laptop](https://www.qovery.com/docs/getting-started/quickstart/try-with-an-agent.md).
* The `RAILS_MASTER_KEY` of the application (the content of `config/master.key`).

## Add a production Dockerfile

`rails new` generates a production Dockerfile since Rails 7.1. You can keep it (see [the port note](#port-and-health-check)), or use this shorter one at the root of the repository:

```dockerfile Dockerfile theme={null}
FROM ruby:3.4-slim AS base
WORKDIR /rails
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y libpq5 && \
    rm -rf /var/lib/apt/lists/*
ENV RAILS_ENV=production \
    BUNDLE_DEPLOYMENT=1 \
    BUNDLE_PATH=/usr/local/bundle \
    BUNDLE_WITHOUT=development:test

FROM base AS build
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential git libpq-dev libyaml-dev pkg-config && \
    rm -rf /var/lib/apt/lists/*
COPY Gemfile Gemfile.lock ./
RUN bundle install && rm -rf ~/.bundle "${BUNDLE_PATH}"/ruby/*/cache
COPY . .
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile

FROM base
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails
RUN useradd rails --uid 1000 --create-home --shell /bin/bash && \
    mkdir -p db log storage tmp && chown -R rails:rails db log storage tmp
USER 1000:1000
EXPOSE 3000
CMD ["./bin/rails", "server", "-b", "0.0.0.0", "-p", "3000"]
```

* `ruby:3.4-slim` is the official Ruby image without build tools. Match the version to your `.ruby-version`. It installs `libpq5`, the PostgreSQL client library that the `pg` gem loads at runtime.
* The `build` stage has the compilers and headers that native gems such as `pg` need. The final stage copies only the installed gems and the application, so the image stays small.
* `BUNDLE_WITHOUT=development:test` leaves out the gems you do not need in production.
* `SECRET_KEY_BASE_DUMMY=1` lets `assets:precompile` run at build time without the real secret.
* The container runs as a non-root user that owns `db`, `log`, `storage` and `tmp`, the folders Rails writes to.
* Puma binds to `0.0.0.0`, not `localhost`, so Kubernetes can reach it.

## Port and health check

The application listens on port `3000`. Rails 7.1 and later add a `GET /up` route that answers `200` when the application boots, so use it as the health check path.

The Dockerfile that Rails 8 generates starts Puma behind Thruster on port `80`. If you keep it, set the port to `80` in Qovery instead of `3000`.

If you set `config.hosts` in `config/environments/production.rb`, exclude the health check from host authorization, because Kubernetes probes call the pod by its IP address:

```ruby config/environments/production.rb theme={null}
config.hosts << ENV["APP_HOST"] if ENV["APP_HOST"].present?
config.host_authorization = { exclude: ->(request) { request.path == "/up" } }
```

## Connect PostgreSQL

The application and the migration job read the connection string from `DATABASE_URL`. Create PostgreSQL, then expose its connection string under that name at the environment scope, so every service of the environment gets it.

> [tab] Qovery database (development)
>
> 1. In the environment, click **New Service** and pick **PostgreSQL**. Keep the visibility private, then create and deploy it. See [Create a Database](https://www.qovery.com/docs/getting-started/guides/getting-started/connect-database.md).
> 2. Open the **Variables** section of the environment, find the built-in variable `QOVERY_POSTGRESQL_Z<ID>_DATABASE_URL_INTERNAL`, select **Create alias**, and name it `DATABASE_URL`.
>
> From the CLI, once the application exists:
>
> ```bash theme={null}
> # Copy the exact name of the ..._DATABASE_URL_INTERNAL variable
> qovery application env list -n my-app
>
> qovery application env alias create -n my-app \
>   -k "QOVERY_POSTGRESQL_Z1234ABCD_DATABASE_URL_INTERNAL" \
>   --alias DATABASE_URL --scope ENVIRONMENT
> ```
>
> This database runs as a container on your cluster, with no automated backups. Use it for development and testing.

> [tab] Blueprint (production)
>
> 1. In the environment, click **New Service** › **Blueprint** and pick a managed PostgreSQL, such as [Amazon RDS for PostgreSQL](https://www.qovery.com/docs/configuration/blueprints/aws-rds-postgresql.md) or [Scaleway Managed PostgreSQL](https://www.qovery.com/docs/configuration/blueprints/scaleway-managed-postgresql.md). Deploy it.
> 2. The Blueprint publishes its outputs as variables named `QOVERY_OUTPUT_TERRAFORM_Z<ID>_<OUTPUT>`, and by default Qovery also creates an alias for each output, named `<PREFIX>_<OUTPUT>` after the Blueprint name. See [Aliases Qovery creates for outputs](https://www.qovery.com/docs/configuration/environment-variables.md#aliases-qovery-creates-for-outputs) for the naming rules and the cases where no alias is created. Copy the prefix from the **Variables** section, then create an environment secret `DATABASE_URL` that composes the outputs with `{{...}}` interpolation. The output names differ per Blueprint.
>
> Amazon RDS for PostgreSQL:
>
> ```text theme={null}
> postgresql://{{QOVERY_OUTPUT_TERRAFORM_Z1234ABCD_DB_USERNAME}}:{{QOVERY_OUTPUT_TERRAFORM_Z1234ABCD_DB_PASSWORD}}@{{QOVERY_OUTPUT_TERRAFORM_Z1234ABCD_DB_ADDRESS}}:{{QOVERY_OUTPUT_TERRAFORM_Z1234ABCD_DB_PORT}}/{{QOVERY_OUTPUT_TERRAFORM_Z1234ABCD_DB_NAME}}
> ```
>
> Scaleway Managed PostgreSQL:
>
> ```text theme={null}
> postgresql://{{QOVERY_OUTPUT_TERRAFORM_Z1234ABCD_DB_USERNAME}}:{{QOVERY_OUTPUT_TERRAFORM_Z1234ABCD_DB_PASSWORD}}@{{QOVERY_OUTPUT_TERRAFORM_Z1234ABCD_ENDPOINT_IP}}:{{QOVERY_OUTPUT_TERRAFORM_Z1234ABCD_ENDPOINT_PORT}}/{{QOVERY_OUTPUT_TERRAFORM_Z1234ABCD_DB_NAME}}
> ```
>
> Interpolation inserts the values as they are, without URL encoding. Leave the Blueprint's `db_password` input empty so Qovery generates the password, or set one made of letters and digits only: characters such as `#`, `?`, `%` or `:` break the URL.
>
> Make sure the Blueprint is deployed in a [deployment stage](https://www.qovery.com/docs/configuration/deployment/pipeline.md) that runs before the migration job.

See [Aliases](https://www.qovery.com/docs/configuration/environment-variables.md#aliases) and [Variable Interpolation](https://www.qovery.com/docs/configuration/environment-variables.md#variable-interpolation) for the rules.

Rails reads `DATABASE_URL` and merges it into the `production` entry of `config/database.yml`. If that entry also declares `cache`, `queue` and `cable` databases (the Rails 8 default for Solid Cache, Solid Queue and Solid Cable), give each one a database it can reach on the same server, or remove the ones you do not use.

## Set the variables

| Variable | Value | Secret |
| - | - | - |
| `DATABASE_URL` | The alias or secret from [Connect PostgreSQL](#connect-postgresql) | Yes |
| `RAILS_MASTER_KEY` | The content of `config/master.key` | Yes |
| `APP_HOST` | The domain of the application, if you use `config.hosts` | No |

## Run the migrations

Run `db:migrate` in a [lifecycle job](https://www.qovery.com/docs/configuration/lifecycle-job.md) built from the same repository and Dockerfile, triggered on the **Deploy** event, with these CMD arguments:

```json theme={null}
["./bin/rails", "db:migrate"]
```

The job runs once per deployment. In the default [deployment pipeline](https://www.qovery.com/docs/configuration/deployment/pipeline.md), jobs run after the databases and before the applications, so the schema is up to date when the new code starts. The job needs `DATABASE_URL` and `RAILS_MASTER_KEY` too: create them at the environment scope.

The Dockerfile that Rails generates also runs `db:prepare` in `bin/docker-entrypoint` when the server starts. This works for a single instance. With several instances, each one runs it at boot, so prefer the job.

## Deploy

> [tab] Console
>
> **1. Create a project and an environment**
>
> Follow steps 1 and 2 of [Deploy Your First Application](https://www.qovery.com/docs/getting-started/guides/getting-started/deploy-your-first-application.md#step-1-create-a-project). Pick the cluster the environment runs on.
>
> **2. Add PostgreSQL**
>
> Create the database and the `DATABASE_URL` variable as described in [Connect PostgreSQL](#connect-postgresql).
>
> **3. Create the application**
>
> In the environment, click **New Service** › **Create application**, select **Git Repository**, then your repository and branch. Leave **Root Application Path** empty unless the Dockerfile is in a subfolder.
>
> **4. Set the port**
>
> Add port `3000` with the protocol **HTTP**, and turn on **Publicly Accessible**. Qovery serves it over HTTPS on port 443.
>
> **5. Set the health checks**
>
> Set the readiness and liveness probes to type **HTTP**, path `/up`, port `3000`. See [Service Health Checks](https://www.qovery.com/docs/configuration/service-health-checks.md).
>
> **6. Add the variables**
>
> Click **Create**, without deploying yet. Then open the **Variables** section of the environment and add the other variables listed in [Set the variables](#set-the-variables) at the environment scope, so the migration job gets them too. `DATABASE_URL` already exists from the previous step. Mark keys and passwords as secrets.
>
> **7. Create the migration job**
>
> Click **New Service** › **Create lifecycle job**, select the same repository, branch and Dockerfile, and trigger it on the **Deploy** event (`START`) with the CMD arguments `["./bin/rails", "db:migrate"]`. Set the number of restarts to 0. See [Run the migrations](#run-the-migrations).
>
> **8. Deploy**
>
> Deploy the environment. Qovery deploys the database, then the job, then the application, builds the image from your Dockerfile and gives the application a URL on `qovery.io`. Follow the progress in the **Deployment logs** tab.

> [tab] With a coding agent
>
> Install the Qovery skills once:
>
> ```bash theme={null}
> curl -fsSL https://skill.qovery.com/install.sh | bash
> ```
>
> Open your agent in the repository and paste:
>
> claude
>
> Copy
>
> Deploy this Rails application with Qovery, with a PostgreSQL database and a lifecycle job that runs the migrations.
>
> The agent writes the Dockerfile if there is none, creates the database, the variables, the job and the application, and gives you the URL. It asks you for the `RAILS_MASTER_KEY`. See [Agent Skills](https://www.qovery.com/docs/getting-started/quickstart/agent-skills.md).

## Troubleshooting

The CLI commands below use the organization and project selected with `qovery context set`. Run it first, or add `--organization` and `--project` to each command.

> [accordion] Deployment fails on the health check
>
> The port in Qovery does not match the server: `3000` for Puma, `80` with Thruster in the Rails 8 Dockerfile. Check that Puma binds to `0.0.0.0`. Read the error lines with `qovery environment deployment logs --environment "<env>" --service "<app>" --errors-only`.

> [accordion] Blocked hosts error or 403
>
> `config.hosts` does not include the domain. Set `APP_HOST` to the `qovery.io` domain shown in the Console, add your custom domains, and keep `/up` excluded from host authorization.

> [accordion] Missing secret\_key\_base or ActiveSupport::MessageEncryptor::InvalidMessage
>
> `RAILS_MASTER_KEY` is missing or does not match `config/credentials.yml.enc`. Set it as a secret at the environment scope, so the application and the migration job both get it.

> [accordion] PendingMigrationError or missing tables
>
> The migrations did not run against this database. Check that the job and the application read the same `DATABASE_URL`, and read the job output with `qovery log --job "<job name>"`.

> [accordion] Assets return 404
>
> `assets:precompile` did not run at build time, or `config.public_file_server.enabled` is `false` in production. Rails 7.1 and later serve `public/` by default; an application generated with an older version may still read `RAILS_SERVE_STATIC_FILES` in `config/environments/production.rb`, so set that variable to `1` or remove the condition. Check the build logs for the precompile step.

For other errors, see [Diagnose a Failed Build or Deployment](https://www.qovery.com/docs/getting-started/troubleshoot/overview.md#diagnose-a-failed-build-or-deployment).

## Next steps

> [card] [Preview environments](https://www.qovery.com/docs/getting-started/guides/use-cases/preview-environments.md)
>
> Get a full copy of the environment for each pull request

> [card] [Cron jobs](https://www.qovery.com/docs/configuration/cronjob.md)
>
> Run rake tasks on a schedule

> [card] [Auto deploy](https://www.qovery.com/docs/configuration/deployment/auto-deploy.md)
>
> Deploy on every push to the branch

> [card] [Other frameworks](https://www.qovery.com/docs/getting-started/guides/frameworks/overview.md)
>
> All framework guides


## Related topics

- [Framework Guides](https://www.qovery.com/docs/getting-started/guides/frameworks/overview.md)
- [Deploy application](https://www.qovery.com/docs/api-reference/application-actions/deploy-application.md)
- [Deploy](https://www.qovery.com/docs/getting-started/guides/qovery-101/deploy.md)
- [Application](https://www.qovery.com/docs/configuration/application.md)
- [Deploy a Django Application](https://www.qovery.com/docs/getting-started/guides/frameworks/django.md)


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