Why does the Rails 8.1 generated Dockerfile run `SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile`, and what would go wrong without that variable?
answer
- the build has no master key
- production never invents a secret_key_base
- ENV then credentials then local-only
- random value in tmp/local_secret.txt
- never set the dummy at run time
basics
~10 sPrecompiling boots the production app, and production looks up secret_key_base in SECRET_KEY_BASE or encrypted credentials. The build has no master key, so SECRET_KEY_BASE_DUMMY makes Rails generate a throwaway secret in tmp/local_secret.txt instead.
solid answer
~30 s`assets:precompile` loads the app with `RAILS_ENV=production`, the value the Dockerfile's base stage sets. In production `config.secret_key_base` comes from `ENV["SECRET_KEY_BASE"]`, then `credentials.secret_key_base`; only development and test (`Rails.env.local?`) generate one. The build context has no key to decrypt credentials, because `.dockerignore` excludes `config/master.key` and `config/credentials/*.key` and `RAILS_MASTER_KEY` is passed only to `docker run`. If anything during boot reads the secret, Rails raises `ArgumentError` "Missing `secret_key_base` for 'production' environment". With `SECRET_KEY_BASE_DUMMY` set, the getter calls `generate_local_secret`, which writes a random hex value to `tmp/local_secret.txt`. The running container does not set the variable, so it reads the real secret, and the dummy never signs anything.
go deeper
Remember that the build has no master key and that SECRET_KEY_BASE_DUMMY lets assets compile without it.
Walk through the lookup order: the dummy flag, SECRET_KEY_BASE, credentials, then local-only generation, and the ArgumentError production raises otherwise.
Point out that tmp/local_secret.txt ships in the image, so the dummy must never reach the runtime environment, and keep boot-time credential reads out of initializers.
Set a team rule for where build-time and run-time secrets live, and how a missing secret fails a deploy loudly instead of being papered over.
## The problem: a production boot without production secrets The generated Dockerfile's `base` stage sets `RAILS_ENV="production"`, and the `build` stage inherits it. When it runs `./bin/rails assets:precompile`, that rake task loads the application in the **production environment**: `config/application.rb`, `config/environments/production.rb` and every initializer. Any of that code may ask for `Rails.application.secret_key_base`, the root secret from which Rails derives the keys that sign and encrypt cookies, message verifiers and encryptors. In a default app something always does: Active Storage's `after_initialize` hook builds `ActiveStorage.verifier` with `app.message_verifier("ActiveStorage")`, which derives a key from the secret on every boot. The build is deliberately missing the secrets: - `.dockerignore` excludes `/config/master.key` and `/config/credentials/*.key`, so the key that decrypts `config/credentials.yml.enc` never enters the build context; - `RAILS_MASTER_KEY` is meant for run time; the Dockerfile's header comment shows it passed to `docker run -e`, and the generated `deploy.yml` lists it under `env: secret:`. ## How Rails 8.1 resolves `secret_key_base` The getter in `Rails::Application::Configuration` checks, in order: 1. if `ENV["SECRET_KEY_BASE_DUMMY"]` is set, call `generate_local_secret`; 2. otherwise `ENV["SECRET_KEY_BASE"]`; 3. then `Rails.application.credentials.secret_key_base`; 4. then, **only when `Rails.env.local?`** (development or test), `generate_local_secret`. If all of that yields nothing, the setter raises `ArgumentError` with "Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit`". Production never invents a secret silently, because a random secret per process would invalidate every session on each restart and differ between servers. `generate_local_secret` writes `SecureRandom.hex(64)` to `tmp/local_secret.txt` if that file does not exist, then reads it back. ## What the dummy buys and what it costs | Approach | Build needs the master key? | Risk | |---|---|---| | `SECRET_KEY_BASE_DUMMY=1` on the precompile line | No | None at run time, if the variable stays on that one line | | Pass `RAILS_MASTER_KEY` as a build `ARG` | Yes | The key can end up in build metadata or image history | | Commit `config/master.key` | Yes | Everyone with the repository or image can decrypt credentials | The variable is set **inline on one `RUN` line**, not with `ENV`, so it does not persist into the image's environment. Fingerprinted asset files do not depend on `secret_key_base`, so a throwaway secret is enough for this task. ## The trap: the dummy file ships in the image The final stage copies `/rails` from `build` wholesale, and `.dockerignore`'s `/tmp/*` rule only filters the build *context*, so the `tmp/local_secret.txt` written during the build is in the final image. That is harmless while production reads the real secret. It becomes a vulnerability if someone adds `SECRET_KEY_BASE_DUMMY=1` to the container's runtime environment to "fix" a missing secret: - the app would sign and encrypt cookies with a key stored in the image itself; - anyone who can pull the image can read that key and forge signed or encrypted cookies; - the error that should have stopped the deploy disappears, so the misconfiguration goes unnoticed. ## Common misreadings - **"The dummy is a fake production secret."** It is a real random secret, just one nobody else knows and nothing at run time uses. It exists so the boot does not fail, not to sign anything that matters. - **"Precompiling needs the credentials."** Building fingerprinted assets reads files and writes digests; it does not decrypt credentials. If a build fails on credentials, an initializer is reading one at boot. - **"The variable replaces `RAILS_MASTER_KEY` everywhere."** It affects only `secret_key_base`. A boot-time `credentials.fetch(:something)` still raises `KeyError` during the build, because without the key the credentials are empty. - **"Rails reads `tmp/local_secret.txt` in production."** It does so only through `generate_local_secret`, which production reaches only when the dummy flag is set. ## Checklist for the bookstore image 1. Keep `SECRET_KEY_BASE_DUMMY=1` inline on the `assets:precompile` line only. 2. Provide `RAILS_MASTER_KEY` (or `SECRET_KEY_BASE`) at `docker run` time or through Kamal's secrets. 3. If an initializer needs a credential at boot, guard it or read it lazily so that precompiling does not depend on it. 4. Treat "Missing `secret_key_base`" at run time as a missing-secret deploy error, never as a reason to set the dummy.
- An initializer in the bookstore app calls `Rails.application.credentials.fetch(:payments_key)` at boot. What happens during the Docker build, and how do you fix it?Without a master key the credentials are empty, so `fetch` raises `KeyError` while `assets:precompile` boots the app, and the build fails even though the dummy secret is set. The dummy covers only `secret_key_base`. Read the credential lazily where it is used, or guard the initializer, so building assets does not need production secrets.
- Why does Rails generate a secret in development and test but refuse to in production?In `Rails.env.local?` environments a random secret stored in `tmp/local_secret.txt` is convenient and losing it only logs you out. In production a generated secret would differ per server and change when the file is lost, breaking sessions and signed data, so Rails raises `ArgumentError` and makes you configure `SECRET_KEY_BASE` or credentials.
- Pass RAILS_MASTER_KEY as a build ARG so assets:precompile can decrypt credentials
- SECRET_KEY_BASE_DUMMY is the secret production uses to sign cookies
- Rails generates a random secret_key_base in production when none is configured
- Setting SECRET_KEY_BASE_DUMMY=1 in the container's runtime environment is harmless
- The dummy also stands in for every other credential an initializer reads