# Kamal for Rails 8: Deploy to Your Own Server with PostgreSQL, Jobs and Secrets

> Kamal Rails guide: deploy Rails 8 to your own server with PostgreSQL, Solid Queue, HTTPS and secrets, plus the rollback and zero-downtime pitfalls to know.

- URL: https://railsdev.io/blog/kamal-rails
- Published: 2026-11-05
- Updated: 2026-11-11
- Author: Hendra Nicholas (Rails Full-Stack Developer & AI Engineer)

---

Kamal Rails deployment is the default way to ship a Rails 8 app: Kamal is the deployment tool that comes with Rails 8. It builds your app as a Docker image, copies it to your servers over SSH, starts the new container behind a small proxy, and only then stops the old one, so users see no downtime. You need a Linux server, a domain name and a Dockerfile (Rails generates one). You do not need Kubernetes, a platform subscription or a deploy server.

This guide shows how a Rails 8.1 app goes from `kamal init` to a production setup with HTTPS, PostgreSQL, a separate jobs container and secrets that stay out of git. It also covers the parts that tutorials skip: what the generated app already gives you, how rollbacks work, and what breaks when you add a second server. If you are still deciding what to host on, my guide to [scaling a Ruby on Rails application](/blog/scaling-ruby-on-rails) explains when one server stops being enough.

> I read Kamal 2.12.0 from its source repository and generated a fresh Rails 8.1.4.1 app to check what Rails provides. I did not deploy to a live server for this article, so the `deploy.yml` examples are checked against Kamal's own documentation and template, not run end to end.

## What Kamal is and what it replaces

[Kamal](https://github.com/basecamp/kamal) is an open-source (MIT) deployment tool from 37signals, the company behind Rails. It started as "MRSK" and became Kamal. Version 2 arrived alongside Rails 8, and as of November 2026 the latest release on RubyGems is 2.12.0 (published June 18, 2026).

It sits between two common choices:

- **A platform like Heroku**: you push code and the platform handles the rest, but you pay for convenience and have less control.
- **A hand-built server**: you set up Nginx, systemd, Capistrano scripts and certificates yourself. It works, but every team ends up with its own fragile version of it.

Kamal gives you the second option's control with a repeatable workflow. It runs your app in Docker containers, so the same image you tested in CI is what runs in production.

## How a Kamal deploy works

When you run `kamal deploy`, Kamal does roughly this:

1. Builds the Docker image from your `Dockerfile` and pushes it to a registry.
2. Connects to each server over SSH and pulls the image.
3. Starts a new container and waits until it passes a health check.
4. Tells [kamal-proxy](https://github.com/basecamp/kamal-proxy) to send new traffic to the new container, then stops the old one.

kamal-proxy replaced Traefik in Kamal 2. It listens on ports 80 and 443, can request Let's Encrypt certificates for you, and handles the switch between containers. The key idea is that your app never has to bind to the public ports. The proxy does that, and your container only needs to answer on one internal port.

Because the proxy's health check hits `/up` by default, it matches the health endpoint that Rails has generated in `config/routes.rb` since Rails 7.1 (`get "up" => "rails/health#show"`). A new Rails app needs no health check setup.

## What Rails 8.1 already gives you

I generated a new app with `rails new demo --database=postgresql` on Rails 8.1.4.1 and looked at the result. These parts matter for deploys:

- **A production Dockerfile** with a multi-stage build, jemalloc, and `bundle` installed without development gems. It exposes port 80 and starts the app with [Thruster](https://github.com/basecamp/thruster) (`CMD ["./bin/thrust", "./bin/rails", "server"]`), which handles asset caching and compression in front of Puma.
- **`bin/docker-entrypoint`**, which runs `bin/rails db:prepare` before the server starts. Your first deploy creates and migrates the database without extra steps.
- **The `kamal` and `thruster` gems** in the Gemfile, plus `solid_queue`.
- **Four production databases** in `config/database.yml`: `primary`, `cache`, `queue` and `cable`. Solid Cache, Solid Queue and Solid Cable use the last three, so a typical Rails 8 app needs PostgreSQL and nothing else.
- **A Puma line** `plugin :solid_queue if ENV["SOLID_QUEUE_IN_PUMA"]`, which runs the job supervisor inside the web process when you set that variable.

In my run, `--skip-bundle` meant `config/deploy.yml` was not generated, because Rails creates it when it installs Kamal after `bundle install`. In a normal `rails new` you get it automatically, and `kamal init` creates it otherwise.

## The minimal `config/deploy.yml`

Here is a configuration for one server running the web app and a PostgreSQL container. It follows the structure of the template that `kamal init` writes. Replace the names, the IP address and the domain.

```yaml
# config/deploy.yml
service: my_app
image: my-user/my_app

servers:
  web:
    - 203.0.113.10
  job:
    hosts:
      - 203.0.113.10
    cmd: bin/jobs

proxy:
  ssl: true
  host: app.example.com

registry:
  server: ghcr.io
  username: my-user
  password:
    - KAMAL_REGISTRY_PASSWORD

builder:
  arch: amd64

env:
  clear:
    DB_HOST: my_app-db
  secret:
    - RAILS_MASTER_KEY
    - MY_APP_DATABASE_PASSWORD

volumes:
  - "my_app_storage:/rails/storage"

accessories:
  db:
    image: postgres:17
    host: 203.0.113.10
    env:
      clear:
        POSTGRES_USER: my_app
        POSTGRES_DB: my_app_production
      secret:
        - POSTGRES_PASSWORD
    directories:
      - data:/var/lib/postgresql/data
```

A few details are easy to miss:

- **`proxy.host` and `ssl: true`** make kamal-proxy request a Let's Encrypt certificate. Kamal's docs say this requires a single server, a host that points to that server, and port 443 open for the challenge. For several web servers, put a load balancer in front and terminate SSL there.
- **`DB_HOST: my_app-db`** works because Kamal names an accessory container `<service>-<accessory>` and attaches it to the same Docker network as the app. The generated `database.yml` has the `host` line commented out, so add `host: <%= ENV["DB_HOST"] %>` to the `primary_production` block (the other three databases inherit it).
- **The password variable** must match your app name. Rails generates `database.yml` with `MY_APP_DATABASE_PASSWORD` for an app called `my_app`.
- **`volumes`** matter if you use Active Storage with local disk. Without a volume, uploaded files disappear on the next deploy. The path `/rails/storage` follows the Rails Dockerfile, which uses `/rails` as the app directory.
- **The PostgreSQL port is not published**, so the database is reachable only from other containers on the host. That is a sensible default. Do not add a public `port` mapping unless you also restrict it with a firewall.

### Secrets

Kamal reads secrets from `.kamal/secrets`, a file that is safe to commit because it holds references, not values. The template suggests reading from the environment, a command, or a password manager:

```bash
# .kamal/secrets
KAMAL_REGISTRY_PASSWORD=$KAMAL_REGISTRY_PASSWORD
RAILS_MASTER_KEY=$(cat config/master.key)
POSTGRES_PASSWORD=$POSTGRES_PASSWORD
MY_APP_DATABASE_PASSWORD=$POSTGRES_PASSWORD
```

Kamal also ships adapters for 1Password, Bitwarden, LastPass, Doppler, AWS Secrets Manager and Google Cloud Secret Manager through `kamal secrets fetch`. Rails 8.1 added a way to read secrets from Rails credentials too, so the master key can be your only external secret. Check the [Rails 8.1 announcement](https://rubyonrails.org/2025/10/22/rails-8-1) for details before relying on it.

Never put `RAILS_MASTER_KEY` or a database password in the `clear` section of `env`. Anything in `clear` lives in plain text in your repository.

## Your first deploy

With the server reachable over SSH as `root` (or another user set under `ssh.user`) and your DNS pointing at it:

```bash
bundle exec kamal setup
```

`kamal setup` installs Docker on the server if needed, boots the accessories (the PostgreSQL container), pushes the secrets, and deploys the app. After that, every release is:

```bash
bundle exec kamal deploy
```

Useful commands for daily work:

| Command | What it does |
| --- | --- |
| `kamal deploy` | Build, push and roll out a new version |
| `kamal rollback VERSION` | Start a previous version that is still on the server |
| `kamal app logs -f` | Follow application logs |
| `kamal app exec -i --reuse "bin/rails console"` | Open a Rails console in the running container |
| `kamal accessory reboot db` | Restart the database container |
| `kamal details` | Show running containers |

The default `kamal init` template includes a `console` alias for the third row. You can define your own under `aliases:` in `deploy.yml`.

### Registry-free deploys

Rails 8.1 and Kamal 2.8 added deploys without a remote registry. If `registry.server` starts with `localhost`, Kamal starts a temporary Docker registry on your machine and pushes through an SSH tunnel to the servers. The `deploy.yml` that `kamal init` writes uses `localhost:5555` for exactly this. It makes a first deploy easier, because you skip creating a Docker Hub or GitHub Container Registry token.

I still recommend a real registry once you deploy from CI. A CI runner is a fresh machine every time, so a remote registry gives you a place where previous images live, and it keeps rollbacks independent of any one machine's Docker cache.

## Running background jobs

Rails 8 apps use Solid Queue for background jobs, which stores jobs in your database. The `job` role in the config above starts a second container from the same image, with `cmd: bin/jobs`. It runs Solid Queue's supervisor and has no proxy, because it serves no web traffic.

For a small app on one server, you can skip the extra container. Add `SOLID_QUEUE_IN_PUMA: true` under `env.clear` and Puma runs the supervisor. That uses less memory but ties job capacity to your web process. My [Solid Queue guide](/blog/solid-queue-rails) covers sizing the connection pool and concurrency limits, which matter once jobs and requests share a database.

The shutdown behavior deserves attention. Per Kamal's configuration reference, the time a container gets to stop after SIGTERM (`stop_timeout`) defaults to the `drain_timeout` for roles without a proxy, and you can set it per role. A deploy replaces your job container, so a job that runs longer than that is killed mid-run. Write jobs to be safe to retry, or split long work into steps. Rails 8.1 added Active Job continuations for this, and its announcement specifically mentions Kamal's 30-second shutdown window.

## Zero downtime is not automatic for assets and migrations

Kamal avoids dropped requests by running old and new containers side by side during the switch. Two problems still catch teams:

1. **Missing assets.** A page rendered by the old container may reference `/assets/application-abc123.css`, but the request lands on the new container, which only has `application-def456.css`. Kamal's `asset_path` setting bridges this by merging files from the old and new versions. Set `asset_path: /rails/public/assets` if you serve assets from the app container.
2. **Migrations that break the old code.** `db:prepare` runs when the new container starts, while the old one is still serving users. Dropping or renaming a column in the same release that stops using it will cause errors in the old code. Split it into two deploys: first stop using the column, then remove it.

These are not Kamal bugs, and any rolling deploy has them. They are the reason a rollback with `kamal rollback` restores the code but not your database changes.

## Strengths and limitations

**Strengths**

- One tool and one YAML file for web servers, workers and the database container.
- Works on any Linux server with SSH, from a small VPS to AWS, Google Cloud or Azure virtual machines.
- Open source with no per-seat or per-app pricing.
- Rails 8 sets it up for you.

**Limitations**

- **You run the database.** The PostgreSQL accessory is a container on a server you manage. Backups, upgrades, disk space and monitoring are your job. Many teams use a managed PostgreSQL service instead and point `DB_HOST` at it.
- **Multiple servers need more work.** Automatic SSL does not work with several web servers, so you need a load balancer, and you must plan for shared storage for uploads.
- **No autoscaling or self-healing scheduler.** If a server dies, Kamal does not move your app. You redeploy to another host.
- **Single-host accessories.** An accessory is tied to the host you name.

## Kamal vs other ways to deploy Rails

| | Kamal | Heroku-style platform | Kubernetes |
| --- | --- | --- | --- |
| Setup effort | Low to medium | Lowest | High |
| Control over servers | Full | Limited | Full |
| Ops responsibility | You (OS, database, backups) | Mostly the platform | You, with more moving parts |
| Autoscaling | No | Yes | Yes |
| Best for | Small to mid-size apps on one or a few servers | Teams that want zero ops | Large systems with many services |

For a typical SaaS that runs on one to three servers, I recommend Kamal. If nobody on the team wants to own a server, a managed platform is cheaper in attention, even if it costs more in money.

## Production checklist

- [ ] The server has a firewall that allows only ports 22, 80 and 443.
- [ ] SSH access uses keys, and root login is disabled if you set `ssh.user`.
- [ ] Secrets come from `.kamal/secrets` references, not from `env.clear`.
- [ ] PostgreSQL has automated backups, and you have tested a restore.
- [ ] Active Storage files live on a volume or in object storage.
- [ ] `asset_path` is set so assets survive the switch between versions.
- [ ] Jobs are idempotent, because deploys can interrupt them.
- [ ] CI builds and deploys with a remote registry, and a rollback has been practiced once.
- [ ] Error tracking and log shipping are in place before you need them.

Upgrading first? My [Rails 8.1 upgrade guide](/blog/rails-8-1-upgrade) walks the path; for Ruby 4.0, see my [Ruby 4.0 upgrade guide](/blog/ruby-4-0-upgrade-rails). For searchable, structured logs at deploy time, see [Rails structured event reporting](/blog/rails-structured-event-reporting).

## Conclusion

Kamal makes the common case simple: one Rails app, one or a few servers, no platform bill. Rails 8.1 gives you a Dockerfile, a health endpoint, four database config entries and a job runner already wired up, so most of your work is the `deploy.yml`, DNS and secrets. The real decisions are about operations: who owns the database, how you back it up, and how you ship migrations safely.

I set up Rails deployment pipelines with Docker and Kamal, move apps off Heroku or older Capistrano setups, and review existing production setups for backup, security and rollback gaps. If you want a second pair of eyes on your deploy, or someone to build it, [send me a short brief and book a discovery call](/#contact). For apps that need modernizing first, see my [legacy Rails modernization service](/services/legacy-rails-modernization).

## Frequently asked questions

### Is Kamal free?

Yes. Kamal is open source under the MIT license. You pay only for your servers and, optionally, a container registry or managed database.

### Do I need Kamal 2 for Rails 8?

New Rails 8 apps include the `kamal` gem and expect Kamal 2. Kamal 1 configurations need to be migrated, and Kamal has a `kamal upgrade` command for that.

### Can Kamal deploy to AWS, Google Cloud or Azure?

Yes, if you can reach a Linux machine over SSH. Create a virtual machine on any cloud, point DNS at it, and use its IP in `deploy.yml`. Kamal does not manage cloud resources for you.

### Does Kamal need Docker Hub?

No. A remote registry such as Docker Hub, GitHub Container Registry or AWS ECR still works, and many teams prefer one for CI. Since Kamal 2.8, a registry whose server starts with `localhost` makes Kamal use a temporary local registry.

### How do I roll back with Kamal?

Run `kamal rollback VERSION` with a version that is still on the server. Kamal keeps recent containers and images (five by default, set with `retain_containers`). A rollback does not undo database migrations.

### Does Kamal work with Cloudflare?

Yes. The `kamal init` template notes that with Cloudflare you should set the SSL/TLS encryption mode to "Full", so traffic from Cloudflare to your app is encrypted too.

### Can I run PostgreSQL with Kamal in production?

You can, as an accessory container with a data directory on the host. It is fine for small apps if you handle backups yourself. For anything business-critical, I recommend a managed PostgreSQL service, since backups and failover are hard to get right.

## References

- [Kamal repository and releases](https://github.com/basecamp/kamal)
- [Kamal documentation](https://kamal-deploy.org)
- [kamal-proxy](https://github.com/basecamp/kamal-proxy)
- [Rails 8.1 announcement](https://rubyonrails.org/2025/10/22/rails-8-1)
- [Thruster](https://github.com/basecamp/thruster)
