Retire backend/.env for durable Dokploy env vars; pre-build backend-billing image

Confirmed the hard way against a live Dokploy AutoDeploy: the code/
checkout is deleted and re-cloned on every deploy, so backend/.env (an
env_file: mount) never survived to the next deploy. Moved its four values
to plain environment: vars sourced from Dokploy's own Environment tab,
which persists across deploys independent of the git-clone wipe.

Also switches backend-billing to the same build-locally-push-to-registry-
pull pattern already used for erpnext-image, so the Dokploy VPS never
builds anything itself (backend/build.sh) — the user's explicit goal of
making this resilient to future frappe_docker/dependency changes, not
just today's deploy error.

Propagated through docker-compose.yml/.dokploy.yml, .env + .env.example,
CLAUDE.md, INSTALLATION_PLAYBOOK.md, CICD_PLAYBOOK.md, BACKUP_RESTORE.md,
backup.sh/restore.sh, and .gitignore's comments.

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-02 06:07:57 -04:00
parent 6e4b1302fa
commit 76f43abad0
12 changed files with 219 additions and 71 deletions

View File

@ -35,3 +35,32 @@
# CUSTOM_IMAGE=sightbox-erpnext
# CUSTOM_TAG=v16.34.1-branded
# PULL_POLICY=never
# backend-billing's own config — moved here from the now-retired
# backend/.env (plain environment: vars in docker-compose.yml's
# backend-billing service, not env_file:, specifically so Dokploy's
# AutoDeploy survives these across a redeploy — see that service's own
# comment for why env_file: didn't work).
# TENANTS_CONFIG_PATH=tenants.json
# SENTRY_DSN=
# ENVIRONMENT=production
# STATE_STORE_PATH=/data/state.db
# 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
# default below means "local build only, nothing to push" — build.sh
# detects this and skips pushing/redeploying automatically. Set to a real
# registry path to build-push-redeploy for real.
# BACKEND_BILLING_IMAGE=sightbox-backend-billing
# BACKEND_BILLING_TAG=latest
# BACKEND_BILLING_PULL_POLICY=never
# Only needed if backend/build.sh should trigger the VPS redeploy itself
# after pushing — otherwise redeploy by hand in Dokploy's dashboard.
# DOKPLOY_URL is this Dokploy instance's own base URL (not the deployed
# app's domain). DOKPLOY_COMPOSE_ID is this Compose Application's id, not
# an "application" id — see backend/build.sh's own comment for why those
# are two different Dokploy API resource types.
# DOKPLOY_URL=https://dokploy.yourdomain.com
# DOKPLOY_API_TOKEN=
# DOKPLOY_COMPOSE_ID=

4
.gitignore vendored
View File

@ -1,11 +1,11 @@
# Root-level ignores — backend/ has its own more specific .gitignore.
# Per-deployment settings (SIGHTBOX_DATA_DIR, FRAPPE_IMAGE_PLATFORM, ...) —
# never committed, same pattern as backend/.env.
# never committed, same pattern as backend/tenants.json.
/.env
# Real R2 credentials and per-deployment paths — never committed, same
# pattern as backend/.env and backend/tenants.json.
# pattern as backend/tenants.json.
backup/.env
# A local scratch copy of bind-mounted volume data, if one ever gets

View File

@ -85,8 +85,13 @@ sightbox/
├── backend/ ← the ONLY custom code in this project
│ ├── app.py
│ ├── Dockerfile
│ ├── build.sh ← builds + (if pointed at a registry path)
│ │ pushes + (if Dokploy API vars are set)
│ │ triggers the VPS redeploy — see its own
│ │ header comment. No backend/.env anymore;
│ │ its 4 values are plain environment: vars
│ │ in docker-compose.yml now (see .env.example)
│ ├── requirements.txt
│ ├── .env.example
│ └── tests/
└── frappe_docker/ ← official clone, native, unmodified
```
@ -108,9 +113,16 @@ sightbox/
its service definitions from the root file rather than duplicating them.
The root file's job is to *add* the `backend` service and network them
together, not to reimplement what `frappe_docker` already provides.
- **`backend/` gets its own `Dockerfile`**, built and run as its own
service in the root compose file — small, independent, and easy to
redeploy on its own if only the billing logic changes.
- **`backend/` gets its own `Dockerfile`, built and pushed via `backend/build.sh`
— the VPS only ever pulls, never builds.** Same build-locally-test-locally
pattern as `erpnext-image/`: `docker-compose.yml` references
`${BACKEND_BILLING_IMAGE}:${BACKEND_BILLING_TAG}`, a bare local tag by
default (local dev, `pull_policy: never`) or a real registry path once
deploying for real. Confirmed the hard way this matters on Dokploy
specifically: AutoDeploy deletes and re-clones the whole git checkout on
every deploy, so building fresh from source there is one more thing that
can behave differently than what was actually tested locally — pre-built
and pushed means the exact bytes tested are the exact bytes that run.
- **Deployment target: Dokploy** — but it points at `docker-compose.dokploy.yml`,
**not** the root `docker-compose.yml`. Confirmed the hard way, on a real deploy:
Dokploy's own compose validator rejects `docker-compose.yml`'s top-level
@ -227,8 +239,8 @@ backend/
├── frappe_client.py — the Frappe REST API wrapper (the one outbound dependency)
├── state_store.py — the small persistent "who was enabled before disabling" store
├── Dockerfile
├── build.sh — builds + pushes + optionally triggers redeploy, see its own header
├── requirements.txt
├── .env.example
└── tests/
├── test_stripe_webhook.py
└── test_billing.py

View File

@ -1,24 +0,0 @@
# Copy to .env — never commit the real file.
# Per-tenant Frappe credentials + Stripe webhook secrets now live in
# tenants.json (copy from tenants.example.json — also never committed with
# real values), not here. One JSON entry per client: each client registers
# their own /webhook/stripe/<tenant_slug> URL in their own Stripe Dashboard,
# so the URL itself carries the tenant — no credential set is ever shared
# across clients. See tenants.example.json for the exact shape.
TENANTS_CONFIG_PATH=tenants.json
# Empty disables Sentry (e.g. local development). Get a DSN from a Sentry
# project (sentry.io project settings -> Client Keys (DSN), or your
# self-hosted instance's equivalent) — Python/Flask platform.
SENTRY_DSN=
# Tags every Sentry event; lets errors from different deployments (or a
# local dev run with a test DSN) be told apart in one Sentry project.
ENVIRONMENT=production
# Path to the shared SQLite state store inside the container — one file for
# every tenant, every row scoped by tenant_slug (see billing/state_store.py).
# The compose service should mount a volume at this path's parent directory
# so it survives container restarts/redeploys.
STATE_STORE_PATH=/data/state.db

86
backend/build.sh Executable file
View File

@ -0,0 +1,86 @@
#!/usr/bin/env bash
# Builds the backend-billing image, optionally pushes it to the registry,
# and optionally triggers a Dokploy redeploy — the same build-locally,
# test-locally, ship-the-exact-tested-artifact pattern erpnext-image/build.sh
# already uses, so the VPS never builds anything itself, only pulls.
#
# Usage (from the sightbox/ project root or from here — either works):
# ./backend/build.sh
#
# Local-only build (no push, no redeploy trigger): just run it with
# BACKEND_BILLING_IMAGE left at its default (a bare local tag, not a
# registry path) — see .env.example. Nothing below pushes or redeploys
# unless BACKEND_BILLING_IMAGE actually points at a registry.
#
# Full build-push-redeploy: set BACKEND_BILLING_IMAGE to the registry path
# (e.g. git.bettertend.net/jasonfraser/sightbox-backend-billing) in .env,
# and additionally set DOKPLOY_URL/DOKPLOY_API_TOKEN/DOKPLOY_COMPOSE_ID (also
# .env.example) to have this script trigger the actual VPS redeploy itself
# once the push succeeds — "pull into the VPS automatically" meaning this
# one command does it, not that Dokploy polls the registry on its own
# (confirmed: Dokploy's native registry-webhook auto-deploy only exists for
# Docker Hub, not a self-hosted registry like this one — see
# playbook/CICD_PLAYBOOK.md). DOKPLOY_COMPOSE_ID, not an "application" id —
# this deployment is a Compose-type service, which is Dokploy's own API's
# own separate resource type (compose.deploy + composeId, not
# application.deploy + applicationId — confirmed against Dokploy's own API
# reference; the two are not interchangeable).
#
# Staged rollout (new container up, old one down) is Compose's own default
# `docker compose up -d` behavior for a changed image — nothing extra
# needed here for that part.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_ENV="${SCRIPT_DIR}/../.env"
if [ -f "$ROOT_ENV" ]; then
set -a
# shellcheck disable=SC1090
source "$ROOT_ENV"
set +a
fi
BACKEND_BILLING_IMAGE="${BACKEND_BILLING_IMAGE:-sightbox-backend-billing}"
BACKEND_BILLING_TAG="${BACKEND_BILLING_TAG:-latest}"
FULL_TAG="${BACKEND_BILLING_IMAGE}:${BACKEND_BILLING_TAG}"
echo "Building ${FULL_TAG} from ${SCRIPT_DIR}/Dockerfile"
docker build -t "${FULL_TAG}" "${SCRIPT_DIR}"
echo
echo "Built ${FULL_TAG}."
# A bare local tag (no "/" at all, e.g. "sightbox-backend-billing") means
# "local dev only" — nothing to push, nothing to redeploy. A registry path
# always has at least one "/" (host/owner/name or owner/name).
if [[ "${BACKEND_BILLING_IMAGE}" != */* ]]; then
echo "BACKEND_BILLING_IMAGE has no registry path (just \"${BACKEND_BILLING_IMAGE}\") — local build only."
echo "Set in .env (see .env.example):"
echo " BACKEND_BILLING_IMAGE=${BACKEND_BILLING_IMAGE}"
echo " BACKEND_BILLING_TAG=${BACKEND_BILLING_TAG}"
echo "Then: docker compose up -d --force-recreate backend-billing"
exit 0
fi
echo "Pushing ${FULL_TAG}..."
docker push "${FULL_TAG}"
echo "Pushed."
if [[ -z "${DOKPLOY_URL:-}" || -z "${DOKPLOY_API_TOKEN:-}" || -z "${DOKPLOY_COMPOSE_ID:-}" ]]; then
echo
echo "DOKPLOY_URL/DOKPLOY_API_TOKEN/DOKPLOY_COMPOSE_ID not all set (see .env.example) —"
echo "skipping the redeploy trigger. Redeploy by hand in Dokploy's dashboard, or set"
echo "all three and re-run this script to have it trigger the redeploy itself."
exit 0
fi
echo
echo "Triggering Dokploy redeploy (composeId=${DOKPLOY_COMPOSE_ID})..."
curl -sSf -X POST "${DOKPLOY_URL%/}/api/compose.deploy" \
-H "accept: application/json" \
-H "x-api-key: ${DOKPLOY_API_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"composeId\": \"${DOKPLOY_COMPOSE_ID}\"}" \
> /dev/null
echo "Triggered. Watch Dokploy's Deployments tab for the result."

View File

@ -8,8 +8,10 @@
# restore` per site: every persistent volume this deployment uses is
# bind-mounted under $SIGHTBOX_DATA_DIR (docker-compose.data-dir.yml) —
# briefly stopping the writer containers and tarring that whole tree,
# together with the app/ repo checkout itself (including backend/.env and
# backend/tenants.json, real secrets never committed to git), gives one
# together with the app/ repo checkout itself (including backend/tenants.json,
# real secrets never committed to git — backend/.env was retired; those
# four values are now plain Dokploy/Compose environment variables, not a
# file, see docker-compose.yml's backend-billing service), 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
# `bench restore` would — see restore.sh, which really is just "untar,

View File

@ -122,8 +122,10 @@ fi
# 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}
# backend/.env and backend/tenants.json travel inside app/ already (see
# backup.sh's docstring) — nothing further to restore for those.
# backend/tenants.json travels inside app/ already (see backup.sh's
# docstring) — nothing further to restore for it. backend/.env was retired
# in favor of plain Compose/Dokploy environment variables, so there's no
# file-based equivalent to restore for backend-billing's own config either.
echo "restore.sh: bringing the stack up..."
SIGHTBOX_DATA_DIR="${DATA_DIR}" docker compose \

View File

@ -30,18 +30,20 @@ services:
type: volume
volume: {}
backend-billing:
build:
context: ./backend
dockerfile: Dockerfile
depends_on:
backend:
condition: service_started
env_file:
- ./backend/.env
environment:
ENVIRONMENT: ${ENVIRONMENT:-production}
SENTRY_DSN: ${SENTRY_DSN:-}
STATE_STORE_PATH: ${STATE_STORE_PATH:-/data/state.db}
TENANTS_CONFIG_PATH: ${TENANTS_CONFIG_PATH:-tenants.json}
image: ${BACKEND_BILLING_IMAGE:-sightbox-backend-billing}:${BACKEND_BILLING_TAG:-latest}
networks:
default: null
ports:
- ${BILLING_PUBLISH_PORT:-8001}:8000
pull_policy: ${BACKEND_BILLING_PULL_POLICY:-never}
restart: unless-stopped
volumes:
- source: billing-state

View File

@ -66,11 +66,29 @@ services:
- ${MARKETING_PUBLISH_PORT:-8091}:80
backend-billing:
build:
context: ./backend
# Pre-built and pushed via backend/build.sh — the VPS only ever pulls,
# never builds, same build-locally-test-locally-ship-the-tested-artifact
# pattern as CUSTOM_IMAGE/erpnext-image/ below. BACKEND_BILLING_IMAGE
# defaults to a bare local tag (no registry path) for local dev; set it
# to a real registry path (see .env.example) once deploying for real.
image: ${BACKEND_BILLING_IMAGE:-sightbox-backend-billing}:${BACKEND_BILLING_TAG:-latest}
pull_policy: ${BACKEND_BILLING_PULL_POLICY:-never}
restart: unless-stopped
env_file:
- ./backend/.env
# Plain environment: vars, not env_file: ./backend/.env — confirmed the
# hard way against a real Dokploy AutoDeploy: Dokploy deletes and
# re-clones the whole git checkout on every deploy, so a file living
# inside it (as env_file: pointed at) never survives to the next
# deploy. Dokploy's own Environment tab variables, by contrast, are
# stored in Dokploy's own database and rewritten fresh into a .env next
# to this compose file on every deploy, independent of the git clone —
# the one mechanism here that's actually designed to persist. These
# four values are simple enough (no multi-line content, nothing
# structured) to move there outright; see .env.example.
environment:
TENANTS_CONFIG_PATH: ${TENANTS_CONFIG_PATH:-tenants.json}
SENTRY_DSN: ${SENTRY_DSN:-}
ENVIRONMENT: ${ENVIRONMENT:-production}
STATE_STORE_PATH: ${STATE_STORE_PATH:-/data/state.db}
volumes:
- billing-state:/data
# Per-tenant credentials, mounted read-only at runtime — never baked

View File

@ -31,7 +31,7 @@ a completely standard, safe pattern — the case-insensitivity problem doesn't e
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
`$SIGHTBOX_DATA_DIR`, tar the whole tree (plus the repo checkout itself, `$SIGHTBOX_APP_DIR`,
including `backend/.env` and `backend/tenants.json` — real secrets, never committed to git) into
including `backend/tenants.json` — a real secret, never committed to git) 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
knowledge required at all.
@ -93,7 +93,7 @@ Confirmed against a real R2 bucket while building this:
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
`backup/.env`, the same pattern as `backend/.env`/`backend/tenants.json`.
`backup/.env`, the same pattern as `backend/tenants.json`.
## Restoring — including onto a brand-new VPS, different provider

View File

@ -9,10 +9,13 @@ Sightbox-specific worked example.**
"confirmed the hard way" notes because it was built by breaking the real thing and fixing it live.
**Update, first real run:** the ungated route (A) — Gitea OAuth connection, manual webhook,
Enable Submodules, Compose Path — has now been walked end-to-end for real against
`git.bettertend.net` + a live Dokploy VPS for this project's own `sightbox` repo (see Part 3). Two
real gotchas came out of it, both folded in below: the `include:` validator rejection (Part 3
step 3, Known Pitfalls), and `backend/.env` needing to exist before Stage 1's first
`docker compose up`, not deferred to Stage 9 (`INSTALLATION_PLAYBOOK.md`). **Still unconfirmed:**
`git.bettertend.net` + a live Dokploy VPS for this project's own `sightbox` repo (see Part 3).
Real gotchas came out of it, all folded in below: three separate Dokploy compose-validator
rejections (`include:`, long-form `env_file:`, `depends_on`'s `required:` — Part 3 step 3, Known
Pitfalls) and `code/` being deleted and re-cloned on every AutoDeploy, which broke the original
`backend/.env` file-based approach outright — fixed by retiring that file for plain
`environment:` vars and by building/pushing `backend-billing`'s image ahead of time instead of
building it on the VPS (`CLAUDE.md`, `INSTALLATION_PLAYBOOK.md` Stage 1). **Still unconfirmed:**
the gated route (B) — Gitea Actions, the `act_runner`, and the test-then-deploy workflow in Part
1 Stages 2-3 and Part 2 Stages 2/5/6 — remains built from Dokploy's and Gitea's own docs, not yet
run live. Treat those specific sections as the starting plan still, not a proven runbook.
@ -206,14 +209,17 @@ Applying the pattern above here, concretely:
same deployment pre-flattened by `./generate-dokploy-compose.sh` — regenerate it any time
`docker-compose.yml` changes, never hand-edit it. Also enable this Application's **Enable
Submodules** toggle (confirmed working against our `frappe_docker` mirror submodule — see
`.gitmodules`). Set environment variables to match
`.env.example`/`backend/.env.example`/`backup/.env.example` — `SIGHTBOX_DATA_DIR`,
`SENTRY_DSN`, `ENVIRONMENT=production`, and so on. `tenants.json`, `backup/.env`, and
`backend/.env` are files, not env vars — those get created directly on the VPS's filesystem
per `INSTALLATION_PLAYBOOK.md` Stage 1/9/10 (`backend/.env` specifically is a **Stage 1**
requirement, not Stage 9 — it's loaded via `env_file:`, which Compose must resolve just to
parse the config, so its absence fails the whole stack, not only `backend-billing`); nothing
about adding CI/CD changes any of that.
`.gitmodules`). Set environment variables to match `.env.example`/`backup/.env.example` —
`SIGHTBOX_DATA_DIR`, `SENTRY_DSN`, `ENVIRONMENT=production`, `BACKEND_BILLING_IMAGE`/`_TAG`,
and so on. `tenants.json` and `backup/.env` are files, not env vars — those get created
directly on the VPS's filesystem per `INSTALLATION_PLAYBOOK.md` Stage 9/10; nothing about
adding CI/CD changes that. `backend/.env` no longer exists at all — confirmed the hard way that
Dokploy's AutoDeploy deletes and re-clones the git checkout on every deploy, so a file living
inside it (as `env_file:` pointed at) never survived to the next one; those four values are now
plain `environment:` vars in `docker-compose.yml`, same mechanism as everything else here.
Similarly, `backend-billing` no longer builds on the VPS at all — `./backend/build.sh` builds
and pushes it to the registry beforehand (Installation Playbook Stage 1), same
build-locally-ship-the-tested-artifact pattern as the branding image.
4. `.gitea/workflows/ci.yml`'s test step becomes:
```yaml
- name: Run backend test suite

View File

@ -70,16 +70,14 @@ per-client backup.
bench serves every client on it (see "Architecture" above) — and go straight to Stage 2 with the
new site name.
⚠ **Confirmed the hard way: `backend/.env` must exist before the first `docker compose up`, not
later.** `cp backend/.env.example backend/.env`, filling in `SENTRY_DSN` and leaving
`ENVIRONMENT=production` — do this now, before anything below. Unlike `tenants.json` (a volume
mount, tolerant of arriving later, in Stage 9) and `backup/.env` (read only by the standalone
backup script, in Stage 10), `backend/.env` is loaded via `env_file:` in `docker-compose.yml` —
Compose needs to resolve that file just to parse the project config, before starting *any*
container. Missing it doesn't just fail `backend-billing`; it fails `docker compose config`
outright, which means the whole stack — Frappe included — never comes up at all. This is a
one-time, per-bench file, the same as the root `.env` — not something each new client (Stage 2
onward) needs their own copy of.
⚠ **Confirmed the hard way, now fixed rather than just worked around:** `backend-billing`'s
config (`TENANTS_CONFIG_PATH`/`SENTRY_DSN`/`ENVIRONMENT`/`STATE_STORE_PATH`) used to live in a
separate `backend/.env` file, loaded via `env_file:` — which broke outright under Dokploy's
AutoDeploy, since Dokploy deletes and re-clones the whole git checkout on every deploy, wiping
any file that lived inside it. These four are now plain `environment:` vars in
`docker-compose.yml`, same as every other setting — just set them in `.env` (see `.env.example`)
like everything else below. No separate file, no separate step, nothing that can go stale between
deploys.
**Optional — this product's own branding (recommended for a real client), before the first
`docker compose up`.** Stock ERPNext shows its own name and logo in a few spots Website Settings
@ -98,6 +96,22 @@ no edits to `frappe_docker/` or this repo's own `docker-compose.yml`. Re-run `bu
`ERPNEXT_VERSION` in `.env` changes; its own build-time checks fail loudly if a version bump means
the patches no longer apply cleanly, rather than silently shipping unpatched branding.
**Required for a real client deployed via Dokploy (not optional like branding above): build and
push `backend-billing`'s image before the first deploy.**
```
./backend/build.sh
```
`docker-compose.yml` references `${BACKEND_BILLING_IMAGE}:${BACKEND_BILLING_TAG}` with
`pull_policy: never` by default — correct for local dev (uses whatever was just built locally),
but on a fresh Dokploy VPS there's no image to pull at all until one has actually been pushed
somewhere it can reach. Set `BACKEND_BILLING_IMAGE` to a real registry path and
`BACKEND_BILLING_PULL_POLICY=missing` in `.env` first (see `.env.example`) — `build.sh` detects
the registry path automatically and pushes; set `DOKPLOY_URL`/`DOKPLOY_API_TOKEN`/
`DOKPLOY_COMPOSE_ID` too if it should also trigger the redeploy itself. The VPS never builds this
image from source — same build-locally-test-locally-ship-the-tested-artifact reasoning as the
branding image above, and the one confirmed way to avoid Dokploy's AutoDeploy rebuilding
something slightly differently than what was actually tested.
**Real client, on the production Linux VPS:** every persistent volume this stack uses (site
data, the database, the billing state) gets bind-mounted to one visible host directory instead of
Docker's own opaque storage — this is what makes Stage 10's backups possible at all, and it has
@ -281,9 +295,10 @@ by Stage 5's provisioning script.
bench, isolated by its own entry in `backend/tenants.json`. QR generation needs no per-client
setup at all; it's already tenant-agnostic.
(`backend/.env` itself — `SENTRY_DSN`/`ENVIRONMENT`/the two path vars — should already exist from
Stage 1; it's one-time-per-bench, not something this or any later client needs its own copy of.
If `backend-billing` never came up at all back in Stage 1, that file is the first thing to check.)
(`backend-billing`'s own `SENTRY_DSN`/`ENVIRONMENT`/path vars and its image/registry setup are
both one-time-per-bench, from Stage 1 — not something this or any later client needs to redo. If
`backend-billing` never came up at all back in Stage 1, its image is the first thing to check —
`docker compose ps` should show it running before anything below will work.)
1. Create one dedicated System Manager User for this client (e.g.
`billing-service@<client>.local`), no welcome email.