In Rails 8.1's generated deploy files for a SQLite bookstore on one rented server, why does `.dockerignore` exclude `storage/` while `config/deploy.yml` mounts a volume at `/rails/storage`?
answer
- image is rebuilt and replaced every deploy
- /storage/* and key files ignored
- production.sqlite3 lives in storage/
- volume app_storage:/rails/storage
- writable by UID 1000, backed up
basics
~20 sstorage/ holds state: SQLite files, local Active Storage uploads and Thruster's certificates. .dockerignore keeps the developer's copies and key files out of the image, and deploy.yml's volume keeps production's copies alive when each deploy replaces the container.
solid answer
~40 sAn image should hold code, not data. The generated `.dockerignore` excludes `/storage/*` (keeping `.keep`), plus `/config/master.key`, `/config/credentials/*.key`, `/.env*`, `/log/*`, `/tmp/*`, built assets and Kamal's files, so a laptop's `development.sqlite3`, test uploads and secrets never reach a layer. In production, `database.yml` points `primary`, `cache`, `queue` and `cable` at `storage/production*.sqlite3`, the `:local` Active Storage service uses `storage/`, and Thruster keeps certificates in `./storage/thruster`. Kamal starts a new container per deploy, so `deploy.yml` mounts `<app>_storage:/rails/storage` as a named volume that outlives containers. On the first boot, the entrypoint's `db:prepare` creates the databases inside it. The process runs as UID 1000, so the volume must be writable by that user, and the data needs backups off the server because it now lives on one host.
code
yaml · 17 lines# config/database.yml (generated, SQLite, production part)
production:
primary:
<<: *default
database: storage/production.sqlite3
cache:
<<: *default
database: storage/production_cache.sqlite3
migrations_paths: db/cache_migrate
queue:
<<: *default
database: storage/production_queue.sqlite3
migrations_paths: db/queue_migrate
cable:
<<: *default
database: storage/production_cable.sqlite3
migrations_paths: db/cable_migratego deeper
Remember that the image holds code and the volume holds data, and that storage/ is where the generated app keeps its SQLite files and uploads.
List what .dockerignore excludes and why, and trace how the deploy.yml volume, database.yml paths and db:prepare fit together on first boot.
Cover volume ownership for UID 1000, off-server backups and restore tests, and the point where one server with SQLite stops being enough.
Judge when the single-server SQLite and local-storage setup is the right cost trade-off and what the migration path to shared services will cost.
## Image versus state A container image is rebuilt on every deploy and each container started from it is disposable: Kamal boots a new container, switches traffic and stops the old one. Anything written to a container's own filesystem stays with that container, which the next deploy replaces. So Rails' deploy defaults separate two things: - **code and build output**, which belong in the image; - **state**, which belongs on a volume mounted into each new container. In a default Rails 8.1 app the state is concentrated in one directory, `storage/`. ## What lives in `storage/` in production | File or directory | Written by | Why it must persist | |---|---|---| | `storage/production.sqlite3` | Active Record, the `primary` database | The bookstore's books, orders and users | | `storage/production_cache.sqlite3`, `_queue`, `_cable` | Solid Cache, Solid Queue, Solid Cable | Cache entries, enqueued and scheduled jobs, broadcast messages | | `storage/` files of the `:local` service | Active Storage (`config.active_storage.service = :local`) | Uploaded cover images | | `storage/thruster/` | Thruster's default `STORAGE_PATH` | TLS certificates, so they are not requested on every boot | The comment above `production:` in the generated SQLite `database.yml` says it outright: the production database lives in `storage/`, "which by default is mounted as a persistent Docker volume in config/deploy.yml". ## What `.dockerignore` keeps out, and why The generated file excludes, among others: - `/storage/*` (except `/storage/.keep`) and `/tmp/storage/*`: a developer's SQLite files and uploads must never be baked into a production image; - `/config/master.key` and `/config/credentials/*.key`, plus `/.env*`: secrets arrive at run time, never in a layer; - `/log/*` and `/tmp/*` (keeping `.keep` files and `tmp/pids`), and `/.git/`; - `/node_modules/`, `/app/assets/builds/*` and `/public/assets`, because the build stage regenerates them; - `/config/deploy*.yml`, `/.kamal`, `/.github`, `/.devcontainer`, `/Dockerfile*` and `/.dockerignore`, which describe how to build and ship rather than what runs. Keeping `storage/.keep` means the directory exists in the image, owned by `rails` through the final stage's `COPY --chown=rails:rails`, so there is a mount point the app user owns. ## What `deploy.yml` adds Unless the app skips Active Storage and also uses a database other than SQLite, the generated `config/deploy.yml` contains: ```yaml volumes: - "bookstore_storage:/rails/storage" ``` That named Docker volume lives on the host and is mounted into every new container at the same path, so `storage/production.sqlite3` survives deploys. Its comment recommends switching to a mounted path that is backed up off the server. ## How the pieces meet on first deploy 1. Kamal starts the first container with an empty volume at `/rails/storage`. 2. `bin/docker-entrypoint` sees `./bin/rails server` at the end of the command and runs `db:prepare`. 3. `db:prepare` finds no database files, creates them in the volume, loads the schema and runs the seeds for the primary database. 4. Later deploys mount the same volume; `db:prepare` finds the files and runs only pending migrations. ## Checking it on the server After the first deploy, three quick checks confirm the setup: - list the host's Docker volumes and confirm `bookstore_storage` exists; - open a shell in the running container and confirm `/rails/storage` holds the `production*.sqlite3` files; - redeploy and confirm an order created before the deploy is still there. ## Production judgement - **Ownership.** The process runs as UID 1000. If you replace the named volume with a host directory owned by root, SQLite and Active Storage fail with permission errors; give UID 1000 write access to it. - **Backups.** The data now exists on one disk on one server. Copy the SQLite files and uploads off the host on a schedule, and test a restore. - **One host only.** SQLite files on a local volume cannot be shared by a second web server; scaling out means a networked database server and object storage for uploads. - **Secrets stay out.** Anything sensitive added to the project later, such as a `.env.production`, is excluded only because `/.env*` already matches it; other new secret files need their own lines.
- A teammate deletes the `volumes:` entry from the bookstore's deploy.yml to simplify it. What happens on the next two deploys?The next container writes `storage/production.sqlite3` into its own filesystem; `db:prepare` creates an empty database, so the store appears wiped. On the following deploy that container is replaced, and every order and upload written in between is gone from the app too, because nothing outside the old container kept the files.
- Why is `/public/assets` in the generated .dockerignore when the image must serve those files?The build stage runs `assets:precompile` and writes fresh fingerprinted files into `public/assets`, which the final stage copies. Ignoring a locally precompiled `public/assets` stops stale or development builds from entering the context and mixing with the ones the image compiles.
The image is a hire car you swap on every trip and the storage volume is your suitcase: each deploy hands you a new car, but whatever is packed in the suitcase moves into it, and nothing left in the old car's glovebox comes along.
- Bake production.sqlite3 into the image so every server starts with the data
- Files written inside the container survive the next Kamal deploy
- The .dockerignore keeps storage/ out only to shrink the image
- Any host directory works as the volume, whoever owns it
- A storage volume lets several web servers share one SQLite database