In a containerised Rails 8.1 app, which of storage/, tmp/, log/ and public/assets must survive a redeploy, and which can be thrown away or rebuilt?
answer
- persistent, disposable, rebuilt
- storage/ holds SQLite and Disk files
- tmp/ is pids, caches, restart.txt
- production logs go to STDOUT
- public/assets from assets:precompile
basics
~10 sIn a Rails 8.1 container, storage/ must persist on a volume because it holds the SQLite databases and Disk-service uploads; tmp/ and log/ are disposable; public/assets is rebuilt by assets:precompile during the image build.
solid answer
~40 s`storage/` is the one that must survive. With the defaults, production's SQLite files (`storage/production.sqlite3` plus the cache, queue and cable databases) and Active Storage's `:local` Disk service both live there, so it needs a persistent volume; the generated Kamal config mounts one at `/rails/storage`, and both `.gitignore` and `.dockerignore` exclude its contents. `tmp/` holds pids, caches such as Bootsnap's, `restart.txt` and `local_secret.txt` in development: all rebuildable. `log/` matters little in production because the generated `production.rb` logs to STDOUT; in development it grows until `bin/rails log:clear`. `public/assets` is generated by `assets:precompile` into the image during the Docker build and is ignored by git, so it is rebuilt on every deploy rather than kept.
code
yaml · 8 lines# config/storage.yml
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>go deeper
Recall which folders the app writes to at runtime and that storage/, tmp/, log/ and public/assets are all kept out of git.
Explain what lives in each folder by default: SQLite and Disk files in storage/, pids and caches in tmp/, STDOUT logging in production, precompiled assets in public/assets.
Diagnose lost data or missing assets after a deploy by mapping each folder to persistent, disposable or rebuilt, and fix volumes, ignores and build steps accordingly.
Decide when the single-volume default of SQLite plus Disk storage is enough and when to move data to a managed database and object storage.
## Three kinds of folder A Rails 8.1 app's tree mixes source code with folders the running app writes to. In a container deploy, where each release starts from a fresh image, those runtime folders fall into three groups: | Folder | Kind | What is in it | Git / image | |---|---|---|---| | `storage/` | **persistent** | SQLite databases, Active Storage Disk files | ignored; mounted as a volume | | `tmp/` | disposable | pids, caches, `restart.txt`, dev secrets | ignored | | `log/` | disposable | per-environment log files | ignored | | `public/assets` | rebuilt | fingerprinted assets from Propshaft | ignored; built into the image | ## `storage/`: the one that must persist With the default SQLite setup, `config/database.yml` puts production data in `storage/production.sqlite3`, and the Solid gems' databases in `storage/production_cache.sqlite3`, `storage/production_queue.sqlite3` and `storage/production_cable.sqlite3`. Production's `config.active_storage.service = :local` points at the `local` service in `config/storage.yml`, whose root is `storage/`. So by default **the database and every uploaded file** live in one folder. Consequences: - **It needs a persistent volume.** The generated `config/deploy.yml` mounts `<app>_storage:/rails/storage` and its comment recommends a volume backed up off the server. Without it, each deploy starts with an empty database. - **It is never in git or the image.** Both `.gitignore` and `.dockerignore` exclude `/storage/*`, keeping only `.keep`. - **Test data stays apart**: the test environment's Disk service writes to `tmp/storage`, so test uploads never mix with real ones. Switching to PostgreSQL and a cloud storage service moves both kinds of data out of the folder, and `storage/` becomes nearly empty in production. ## `tmp/`: disposable by design `tmp/` holds files the app can always recreate: - `tmp/pids/server.pid`, the running server's pid; - `tmp/cache/`, including Bootsnap's load-path and compile caches; - `tmp/restart.txt`, which Puma's `tmp_restart` plugin (enabled in the generated `config/puma.rb`) watches to restart the server; - `tmp/caching-dev.txt`, toggled by `bin/rails dev:cache`, and `tmp/local_secret.txt`, the development `secret_key_base` when none is configured. `bin/rails tmp:clear` empties the caches safely. In a container it starts empty except for what the image build put there, such as precompiled Bootsnap caches. ## `log/`: mostly unused in production The generated `production.rb` sends logs to **STDOUT** through `ActiveSupport::TaggedLogging.logger(STDOUT)`, where the container runtime collects them. Development and test still write `log/development.log` and `log/test.log`, which grow without bound until `bin/rails log:clear` (also run by `bin/setup`). ## `public/` and `public/assets` `public/` is served as-is: the static error pages (`400.html`, `404.html`, `406-unsupported-browser.html`, `422.html`, `500.html`), the icons and `robots.txt` are committed source. **`public/assets`** is different: Propshaft's `assets:precompile` writes fingerprinted copies of `app/assets` there, and the generated Dockerfile runs it during the build. It is listed in `.gitignore` and `.dockerignore`, so it is always produced from source, never copied from a laptop. ## The checks that catch mistakes 1. After a deploy, are yesterday's orders and uploads still there? If not, `storage/` is not on a volume. 2. Is anything under `tmp/` or `log/` in git? Remove it; the generated ignore rules already exclude both. 3. Did someone commit `public/assets` to "fix" a missing stylesheet? Remove it and fix the precompile step instead. 4. Does the image's non-root user own the folders it writes to? The generated Dockerfile changes ownership of `db`, `log`, `storage` and `tmp` for that reason.
- Why does `storage/` need a backup plan even with a persistent volume?A volume survives redeploys but not the loss of the server or disk. With the defaults, the volume holds both the SQLite databases and the uploaded files, so losing it loses everything; the generated deploy config's comment recommends a volume backed up off the server for that reason.
- Is it safe to run `bin/rails tmp:clear` on a running production container?Generally yes: it clears caches, sockets, screenshots and test storage, all of which the app recreates. It does not touch `storage/`. Expect a slower next boot while caches such as Bootsnap's are rebuilt.
A container deploy is like moving into a freshly built office each release: the filing cabinet (storage/) must come along in the van, the scratch paper on the desks (tmp/, log/) can be thrown away, and the printed signs (public/assets) are reprinted from the originals.
- tmp/ must be on a persistent volume or the app loses data.
- Committing public/assets saves the build from precompiling.
- Production logs pile up in log/production.log inside the container.
- storage/ only holds uploads, so losing it never affects the database.
- Uploaded files in development land in public/uploads.