Make tenants.json durable: move its bind-mount source into SIGHTBOX_DATA_DIR

Confirmed the hard way on a fresh second deployment: the default bind
source (./backend/tenants.json, relative to the repo checkout) gets wiped
on every Dokploy AutoDeploy same as backend/.env used to — except worse,
Docker silently creates an empty DIRECTORY at a missing bind-mount source
instead of failing, which crashes backend-billing with a confusing
IsADirectoryError instead of an obvious missing-file error.

TENANTS_JSON_PATH (new, defaults to the old relative path for local dev)
lets production point this at an absolute path inside $SIGHTBOX_DATA_DIR
instead — survives every redeploy, and is already covered by backup.sh's
nightly snapshot for free, resolving the tenants.json backup-fidelity gap
flagged earlier but never addressed.

Also fixes a second bug this surfaced: generate-dokploy-compose.sh's
--no-interpolate flag leaves a variable-based bind source as a literal
unresolved string, which `docker compose config` can't recognize as a
path and silently mis-normalizes to type: volume instead of type: bind —
Dokploy then rejects it as "refers to undefined volume". Declaring
type: bind explicitly in docker-compose.yml's long-form volume entry
removes the guesswork regardless of interpolation.

Stage 1 now seeds the file empty (valid, zero-tenant config) before the
first docker compose up. Stage 9, BACKUP_RESTORE.md, and both backup
scripts updated to match where the file actually lives now.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012pc2x7vyuUofdT3kLuqF3Y
This commit is contained in:
JasonFraser 2026-10-03 20:51:29 -04:00
parent 3ab0e1e2a0
commit cde7dbd4c6
7 changed files with 78 additions and 18 deletions

View File

@ -46,6 +46,19 @@
# ENVIRONMENT=production # ENVIRONMENT=production
# STATE_STORE_PATH=/data/state.db # STATE_STORE_PATH=/data/state.db
# Host path for tenants.json itself (the bind-mount source, not its path
# inside the container — that's TENANTS_CONFIG_PATH above). Leave unset for
# local dev (defaults to ./backend/tenants.json, next to this repo
# checkout). Production MUST override this to an absolute path inside
# $SIGHTBOX_DATA_DIR — confirmed the hard way: Dokploy deletes and
# re-clones the whole git checkout on every AutoDeploy, so a file left at
# the default relative path never survives to the next deploy, and Docker
# silently creates an empty directory there instead of failing, which then
# crashes backend-billing with a confusing IsADirectoryError. Putting it in
# $SIGHTBOX_DATA_DIR instead means it survives every redeploy AND is
# already covered by backup.sh's nightly snapshot for free.
# TENANTS_JSON_PATH=/opt/sightbox/data/tenants.json
# backend-billing's image — same build-locally-push-to-registry pattern as # backend-billing's image — same build-locally-push-to-registry pattern as
# CUSTOM_IMAGE above, see backend/build.sh. A bare name (no "/") like the # CUSTOM_IMAGE above, see backend/build.sh. A bare name (no "/") like the
# default below means "local build only, nothing to push" — build.sh # default below means "local build only, nothing to push" — build.sh

View File

@ -7,12 +7,14 @@
# What this backs up, and why raw-copy instead of `bench backup`/`bench # What this backs up, and why raw-copy instead of `bench backup`/`bench
# restore` per site: every persistent volume this deployment uses is # restore` per site: every persistent volume this deployment uses is
# bind-mounted under $SIGHTBOX_DATA_DIR (docker-compose.data-dir.yml) — # bind-mounted under $SIGHTBOX_DATA_DIR (docker-compose.data-dir.yml) —
# briefly stopping the writer containers and tarring that whole tree, # including tenants.json itself (real secrets, never committed to git;
# together with the app/ repo checkout itself (including backend/tenants.json, # TENANTS_JSON_PATH points here specifically so it survives a Dokploy
# real secrets never committed to git — backend/.env was retired; those # AutoDeploy's git-checkout wipe, see docker-compose.yml's backend-billing
# four values are now plain Dokploy/Compose environment variables, not a # service) — briefly stopping the writer containers and tarring that whole
# file, see docker-compose.yml's backend-billing service), gives one # tree, together with the app/ repo checkout itself (backend/.env was
# self-contained, site-count-agnostic archive. Restoring never needs to # retired the same way; its four values are now plain Dokploy/Compose
# environment variables, not a file), gives one self-contained,
# site-count-agnostic archive. Restoring never needs to
# know how many client sites exist or loop over them individually the way # know how many client sites exist or loop over them individually the way
# `bench restore` would — see restore.sh, which really is just "untar, # `bench restore` would — see restore.sh, which really is just "untar,
# docker compose up -d". # docker compose up -d".

View File

@ -122,7 +122,7 @@ fi
# but on the off chance one's missing, create it rather than fail obscurely. # but on the off chance one's missing, create it rather than fail obscurely.
mkdir -p "${DATA_DIR}"/{sites,db-data,redis-queue-data,billing-state} mkdir -p "${DATA_DIR}"/{sites,db-data,redis-queue-data,billing-state}
# backend/tenants.json travels inside app/ already (see backup.sh's # tenants.json travels inside $SIGHTBOX_DATA_DIR already (see backup.sh's
# docstring) — nothing further to restore for it. backend/.env was retired # docstring) — nothing further to restore for it. backend/.env was retired
# in favor of plain Compose/Dokploy environment variables, so there's no # in favor of plain Compose/Dokploy environment variables, so there's no
# file-based equivalent to restore for backend-billing's own config either. # file-based equivalent to restore for backend-billing's own config either.

View File

@ -50,10 +50,8 @@ services:
target: /data target: /data
type: volume type: volume
volume: {} volume: {}
- bind: - read_only: true
create_host_path: true source: ${TENANTS_JSON_PATH:-./backend/tenants.json}
read_only: true
source: ./backend/tenants.json
target: /app/tenants.json target: /app/tenants.json
type: bind type: bind
configurator: configurator:

View File

@ -95,7 +95,36 @@ services:
# into the image (see backend/.dockerignore). Multi-tenant billing: # into the image (see backend/.dockerignore). Multi-tenant billing:
# every client's Frappe API key/secret and Stripe webhook secret live # every client's Frappe API key/secret and Stripe webhook secret live
# here, one JSON object per tenant slug (see backend/tenants.example.json). # here, one JSON object per tenant slug (see backend/tenants.example.json).
- ./backend/tenants.json:/app/tenants.json:ro #
# TENANTS_JSON_PATH, not a hardcoded ./backend/tenants.json — confirmed
# the hard way: Dokploy deletes and re-clones the whole git checkout
# (including backend/) on every AutoDeploy, so a file living inside it
# never survives to the next deploy. Worse, Docker silently creates an
# empty DIRECTORY at a bind-mount source that doesn't exist yet rather
# than failing, which then crashes the app with a confusing
# IsADirectoryError instead of a clear missing-file error. The default
# below (./backend/tenants.json) is correct for local dev, where the
# checkout is stable; production must override this in .env to an
# absolute path inside $SIGHTBOX_DATA_DIR (e.g.
# /opt/sightbox/data/tenants.json) — outside the wiped checkout, and
# already covered by the nightly backup since it's already inside the
# directory backup.sh tars. See .env.example and
# INSTALLATION_PLAYBOOK.md Stage 1.
#
# Explicit long-form (type: bind), not the short-form
# "source:target:ro" string — confirmed the hard way:
# generate-dokploy-compose.sh's --no-interpolate flag (needed so
# Dokploy's own Environment tab keeps control of these values, see
# that script's header) leaves this source as a literal, unresolved
# "${TENANTS_JSON_PATH:-...}" string. `docker compose config` can't
# tell that's a path when it can't interpolate it, and silently
# normalizes the short form to type: volume instead of type: bind —
# Dokploy then rejects it as "refers to undefined volume". Declaring
# type: bind explicitly here removes the guesswork entirely.
- type: bind
source: ${TENANTS_JSON_PATH:-./backend/tenants.json}
target: /app/tenants.json
read_only: true
depends_on: depends_on:
- backend - backend
ports: ports:

View File

@ -30,9 +30,10 @@ a completely standard, safe pattern — the case-insensitivity problem doesn't e
`bench backup` is the right tool when you need a live, no-downtime, per-site backup — but a `bench backup` is the right tool when you need a live, no-downtime, per-site backup — but a
restore then has to loop over every client site individually, which is exactly the kind of restore then has to loop over every client site individually, which is exactly the kind of
per-site orchestration this design avoids. Instead: briefly stop every container that writes to per-site orchestration this design avoids. Instead: briefly stop every container that writes to
`$SIGHTBOX_DATA_DIR`, tar the whole tree (plus the repo checkout itself, `$SIGHTBOX_APP_DIR`, `$SIGHTBOX_DATA_DIR` — which includes `tenants.json` itself (a real secret, never committed to
including `backend/tenants.json` — a real secret, never committed to git) into git; `TENANTS_JSON_PATH` points here specifically so it survives a Dokploy AutoDeploy's
one archive, restart. New client sites are automatically included in the very next backup, with git-checkout wipe) — tar that whole tree, plus the repo checkout itself (`$SIGHTBOX_APP_DIR`),
into one archive, restart. New client sites are automatically included in the very next backup, with
zero script changes. Restore genuinely is "unpack, `docker compose up -d`" — no per-site zero script changes. Restore genuinely is "unpack, `docker compose up -d`" — no per-site
knowledge required at all. knowledge required at all.
@ -93,7 +94,7 @@ Confirmed against a real R2 bucket while building this:
Both scripts set these via `RCLONE_CONFIG_R2_*` environment variables inline — no Both scripts set these via `RCLONE_CONFIG_R2_*` environment variables inline — no
`~/.config/rclone/rclone.conf` needed, keeping every credential inside this repo's own gitignored `~/.config/rclone/rclone.conf` needed, keeping every credential inside this repo's own gitignored
`backup/.env`, the same pattern as `backend/tenants.json`. `backup/.env`, the same never-committed-secret pattern as `tenants.json`.
## Restoring — including onto a brand-new VPS, different provider ## Restoring — including onto a brand-new VPS, different provider

View File

@ -122,10 +122,24 @@ work if this is done up front). Bind mounts don't auto-create their target direc
named volume does, so create them first: named volume does, so create them first:
``` ```
mkdir -p /opt/sightbox/data/{sites,db-data,redis-queue-data,billing-state} mkdir -p /opt/sightbox/data/{sites,db-data,redis-queue-data,billing-state}
[ -f /opt/sightbox/data/tenants.json ] || echo '{}' > /opt/sightbox/data/tenants.json
echo "SIGHTBOX_DATA_DIR=/opt/sightbox/data" >> .env echo "SIGHTBOX_DATA_DIR=/opt/sightbox/data" >> .env
echo "TENANTS_JSON_PATH=/opt/sightbox/data/tenants.json" >> .env
docker compose -f docker-compose.yml -f docker-compose.data-dir.yml up -d docker compose -f docker-compose.yml -f docker-compose.data-dir.yml up -d
``` ```
⚠ **Confirmed the hard way:** `TENANTS_JSON_PATH` isn't optional here the way it might look —
`backend-billing`'s own `docker-compose.yml` mount defaults to a path *inside* the repo checkout
(`./backend/tenants.json`), which is correct for local dev but gets wiped on every Dokploy
AutoDeploy same as `backend/.env` used to. Worse than a clean failure: Docker silently creates an
empty **directory** at a bind-mount source that doesn't exist yet, which then crashes
`backend-billing` with a confusing `IsADirectoryError` instead of an obvious missing-file error.
The `echo '{}' >` above seeds a real, empty (zero-tenant) file at the durable path before the
first `docker compose up` — safe even before Stage 9 adds a real client, since an empty object is
a valid, zero-tenant config. Putting it inside `$SIGHTBOX_DATA_DIR` specifically (not some other
absolute path) means it also survives a VPS migration and is already covered by Stage 10's nightly
backup, with nothing extra to configure there.
**Local dev / testing (`<client>.localhost`):** skip the `data-dir` file entirely — just **Local dev / testing (`<client>.localhost`):** skip the `data-dir` file entirely — just
``` ```
docker compose up -d docker compose up -d
@ -409,8 +423,11 @@ both one-time-per-bench, from Stage 1 — not something this or any later client
configure here either. configure here either.
2. Generate its API key/secret: Settings → My Settings → API Access, or 2. Generate its API key/secret: Settings → My Settings → API Access, or
`bench --site <site> execute frappe.core.doctype.user.user.generate_keys --args "['<email>']"`. `bench --site <site> execute frappe.core.doctype.user.user.generate_keys --args "['<email>']"`.
3. Add one entry to `backend/tenants.json` (copy the shape from `tenants.example.json`), keyed by 3. Add one entry to `tenants.json` (copy the shape from `backend/tenants.example.json`), keyed by
a slug for this client: a slug for this client — for a real Dokploy deployment this file now lives at
`$SIGHTBOX_DATA_DIR/tenants.json` (`/opt/sightbox/data/tenants.json`, seeded empty back in
Stage 1), **not** `backend/tenants.json` inside the repo checkout; edit it directly at that
path on the VPS. Local dev still uses `backend/tenants.json` next to the repo, unchanged:
```json ```json
{ {
"acme": { "acme": {