skip to content

What does Thruster add in front of Puma in a Rails 8.1 container, and how do the Dockerfile's `EXPOSE 80` and Puma's port 3000 fit together?

level: middleimportance: must knowfreq 45%

answer

  1. Go binary wrapping the server command
  2. TARGET_PORT 3000 exported as PORT
  3. listens on HTTP_PORT 80
  4. gzip, public-cache, X-Sendfile, HTTP/2
  5. TLS_DOMAIN turns on Let's Encrypt

basics

~20 s

Thruster is a small Go proxy the Dockerfile runs as ./bin/thrust ./bin/rails server: it starts Puma on PORT 3000, listens on port 80 itself, and adds gzip, caching of public responses, X-Sendfile, HTTP/2 and optional Let's Encrypt TLS.

solid answer

~40 s

The generated `CMD` is `["./bin/thrust", "./bin/rails", "server"]`. `bin/thrust` loads the `thruster` gem's executable, a Go binary, which starts the rest of the command as a child process with `PORT` set to `TARGET_PORT` (3000 by default), so `config/puma.rb`'s `port ENV.fetch("PORT", 3000)` binds 3000. Thruster itself listens on `HTTP_PORT` 80, which is why the Dockerfile says `EXPOSE 80`, and proxies to Puma. On the way it gzips responses, caches responses marked `Cache-Control: public` with a max-age in a 64 MB in-memory cache, serves files Rails hands off with `X-Sendfile`, and with `TLS_DOMAIN` set provisions Let's Encrypt certificates and serves HTTP/2. It also relays SIGTERM and SIGINT to Puma. Behind Kamal, kamal-proxy terminates TLS and forwards to port 80, so you leave `TLS_DOMAIN` unset.

code

bash · 10 lines
bash
# What the generated image runs by default
./bin/thrust ./bin/rails server
# Thruster: listens on 80, starts Puma with PORT=3000

# Same image, single server with no proxy in front: let Thruster get certificates
docker run -d -p 80:80 -p 443:443 \
  -e RAILS_MASTER_KEY="$RAILS_MASTER_KEY" \
  -e TLS_DOMAIN=books.example.com \
  -v bookstore_storage:/rails/storage \
  bookstore

go deeper

for a junior

Recall that the image runs bin/thrust in front of bin/rails server, and that Thruster listens on 80 while Puma uses 3000.

for a middle

Explain TARGET_PORT becoming PORT, which responses Thruster caches, what compression and X-Sendfile buy, and when TLS_DOMAIN applies.

for a senior

Place Thruster correctly behind kamal-proxy, size or disable its cache and body limit deliberately, and know it relays SIGTERM for graceful shutdown.

for a principal

Decide when Thruster's zero-config proxy is enough and when the team needs a dedicated proxy or CDN layer with its own operating cost.

## What Thruster is **Thruster** is an HTTP/2 proxy written in Go and distributed as the `thruster` gem with prebuilt platform binaries. Rails 8.0 put it in the generated Gemfile (`gem "thruster", require: false`) and in the Dockerfile, and Rails 8.1 keeps it. It has no configuration file: it reads environment variables, each of which may also be written with a `THRUSTER_` prefix that takes precedence (`THRUSTER_TLS_DOMAIN` beats `TLS_DOMAIN`). The binstub `bin/thrust` is four lines: it sets up Bundler and `load`s the gem's `thrust` executable. The Dockerfile ends with: - `EXPOSE 80` - `CMD ["./bin/thrust", "./bin/rails", "server"]` With `rails new --skip-thruster` the same lines become `EXPOSE 3000` and `CMD ["./bin/rails", "server"]`. ## How the ports line up 1. Thruster parses everything after `thrust` as the **upstream command**, here `./bin/rails server`. 2. Before starting it, Thruster sets `PORT` to `TARGET_PORT`, default **3000**. 3. The generated `config/puma.rb` says `port ENV.fetch("PORT", 3000)`, so Puma binds 3000 inside the container. 4. Thruster listens on `HTTP_PORT`, default **80**, and on `HTTPS_PORT` 443 when TLS is on, then proxies each request to Puma. So port 3000 is private to the container, and port 80 is what the outside world, or Kamal's proxy, talks to. Kamal 2's default `app_port` is 80 to match. ## What it adds to Puma | Feature | Variable and default | Notes | |---|---|---| | gzip compression | `GZIP_COMPRESSION_ENABLED` on; `GZIP_COMPRESSION_JITTER` 32 bytes | The jitter pads compressed sizes to blunt BREACH-style attacks; `GZIP_COMPRESSION_DISABLE_ON_AUTH` can switch it off for requests with cookies or auth headers | | In-memory HTTP cache | `CACHE_SIZE` 64 MB, `MAX_CACHE_ITEM_SIZE` 1 MB | Only GET/HEAD responses whose `Cache-Control` contains `public` and a max-age | | X-Sendfile | `X_SENDFILE_ENABLED` on | Thruster serves the file named in an `X-Sendfile` response header itself | | TLS and HTTP/2 | `TLS_DOMAIN` unset means HTTP only | Certificates stored under `STORAGE_PATH`, default `./storage/thruster` | | Request limits and timeouts | `MAX_REQUEST_BODY` 0 (no limit), `HTTP_READ_TIMEOUT` 30 s | Durations are whole seconds | | Error page | `BAD_GATEWAY_PAGE` `./public/502.html` | Rails generates no 502.html, so Puma being down yields an empty 502 | The cache rule matters for Rails: the generated `production.rb` sends `cache-control: public, max-age=` followed by `1.year.to_i` seconds for files under `public/`, so fingerprinted assets are exactly what Thruster caches. Pages rendered for a signed-in user normally carry `private` or no `public` directive, so Thruster does not store them. ## Tuning it for the bookstore A few variables are worth setting deliberately rather than leaving at their defaults: - `MAX_REQUEST_BODY`: the default `0` means no limit, so a cover-image upload of any size reaches Puma; a byte limit makes Thruster cut off oversized bodies and answer 413 Request Entity Too Large. - `CACHE_SIZE` and `MAX_CACHE_ITEM_SIZE`: raise them if large fingerprinted bundles exceed 1 MB and are therefore never cached. - `GZIP_COMPRESSION_DISABLE_ON_AUTH=true`: turns off compression for requests carrying `Cookie`, `Authorization` or `X-Csrf-Token`, trading bandwidth for a stronger defence against compression side channels. - `LOG_REQUESTS=false`: drops Thruster's own per-request log lines when Rails already logs every request. ## Process supervision in a container Containers usually have no process manager. Thruster fills that gap for one process: - it starts Puma as a child and waits on it; - it relays `SIGINT` and `SIGTERM` to Puma, so `docker stop` triggers Puma's graceful shutdown; - it exits with Puma's exit code, or 128 plus the signal number when Puma was killed by a signal. ## Where it sits in a Kamal deploy With Kamal 2, **kamal-proxy** runs on the host, terminates TLS when `proxy: ssl: true` is set, and forwards to the container's port 80, where Thruster is listening. In that setup: - leave `TLS_DOMAIN` unset on Thruster, or you would terminate TLS twice; - Thruster still compresses, caches public assets and handles X-Sendfile; - `FORWARD_HEADERS` defaults to on when Thruster has no TLS, so Thruster passes along the `X-Forwarded-*` headers the proxy in front of it sends. For a single container on the open Internet with no proxy in front, setting `TLS_DOMAIN` and publishing ports 80 and 443 lets Thruster obtain certificates itself.

  • Puma is still booting when the first request reaches the bookstore container. What does the client see, and how can you change it?
    Thruster starts in milliseconds and proxies to port 3000 before Puma listens, so the request fails with 502 Bad Gateway. Thruster serves `BAD_GATEWAY_PAGE`, default `./public/502.html`; Rails does not generate that file, so the 502 is empty until you add one, for example a short "starting up" page.
  • Why does Thruster not cache the bookstore's signed-in cart page?
    Thruster stores only GET or HEAD responses whose `Cache-Control` includes `public` and a positive `max-age`, and skips any marked `no-cache` or sent with `Vary: *`. A Rails page rendered for a session normally lacks `public`, so it bypasses the cache; only responses like `public/` files with the generated far-future header are stored.

  • Thruster is Ruby middleware inserted into the Rails middleware stack
  • Puma listens on port 80 directly inside the generated container
  • Set TLS_DOMAIN on Thruster even when kamal-proxy already terminates TLS
  • Thruster caches every rendered page, so private pages can leak between users
  • Thruster is configured through directives in config/puma.rb

Assumes the Rails 8.1 Dockerfile and Thruster 0.1.26's source defaults; Thruster became the default CMD in Rails 8.0, and Rails' own lockfile resolves 0.1.21.