Deploy runbook
Staging and production on Dokploy at The Keep, rollback, backups and restore.
What runs
Each environment is one Dokploy compose service built from docker-compose.deploy.yml: its own postgres (own volume), api, worker (pg-boss), web, dashboard, docs and backup. Traefik terminates tls with Let's Encrypt. Dokploy projects: ritorno-staging, ritorno-production and ritorno-previews.
| staging | production | |
|---|---|---|
| trigger | every merge to main | merged release PR (tag) |
| hosts | staging-ritorno, app-staging-ritorno, api-staging-ritorno, docs-staging-ritorno | ritorno, app-ritorno, api-ritorno, docs-ritorno |
SEED | demo | none |
MESSAGING_LIVE | false | true (a channel still needs its provider keys) |
ALLOW_PREVIEW_ORIGINS | true | false |
S3_PREFIX | staging/ | empty |
All hosts are under findmalek.engineer. Health: the api answers /health, and web, dashboard and docs answer /api/version, each with the commit its image was built from (sha, the GIT_SHA build arg). Every image also ships a docker HEALTHCHECK. The deploy job resolves the deployed ref to a commit and waits until all four report that sha. After 20 minutes it fails, prints the status and sha of each app that did not, and on staging nothing is released. Rolling back to a tag older than these routes redeploys it, but the job ends red: check the apps by hand. The worker starts once the api is healthy, so migrations have run.
Secrets
App secrets come from Infisical, see secrets: the deploy job exports the staging or prod environment and writes it, with the computed urls and flags, to the Dokploy compose env before deploying. Without the INFISICAL_* settings the env already in Dokploy is used.
Set up from scratch
- optional: the Infisical settings (secrets
INFISICAL_CLIENT_ID,INFISICAL_CLIENT_SECRET, variablesINFISICAL_URL,INFISICAL_PROJECT_ID), see secrets - an api key for a Dokploy user (Dokploy UI, profile, api keys) as repo secret
DOKPLOY_TOKEN, plus repo variablesDOKPLOY_URL,PREVIEW_DOMAIN,DOKPLOY_ENVIRONMENT_ID(previews environment),STAGING_API_URL,PRODUCTION_API_URL DOKPLOY_URL=... DOKPLOY_TOKEN=... TARGET=staging ENVIRONMENT_ID=<env id> node .github/scripts/dokploy-bootstrap.mjs, then again withTARGET=production. It creates the compose service, a random env (secrets, admin password) and the domains, and prints the compose id. Set it as variableDOKPLOY_STAGING_COMPOSE_IDorDOKPLOY_PRODUCTION_COMPOSE_ID. Re-running keeps the env of an existing service- add the provider keys (Resend, Kapso, S3, GlitchTip, OpenObserve) in the compose Environment tab, the keys are listed in
docker-compose.deploy.yml - DNS: a CNAME per host to the server, plus
*.preview-ritorno
Database names after the rename
New stacks use the role and database ritorno (POSTGRES_USER and POSTGRES_DB in docker-compose.deploy.yml, default ritorno). The staging and production volumes were created before the rename and still hold le_hoetel, so both keep POSTGRES_USER=le_hoetel and POSTGRES_DB=le_hoetel in their env (Infisical or the Dokploy Environment tab). The deploy job writes those two values itself when the env sets neither, so a deploy never points the api at a database that does not exist.
To move an environment to ritorno: dump the database with pg_dump --no-owner, set both keys to ritorno and docker volume rm the old volume (or point the service at a new one), deploy so postgres starts empty, restore the dump, then check /health.
Hosts after the rename
The Ritorno hosts are added next to the old ones, nothing is removed. Once DNS for the new hosts points to the server, run the dokploy-domains workflow (or TARGET=staging node .github/scripts/dokploy-domains.mjs with the Dokploy env) for staging and production. Remove the old domains in Dokploy only after the new ones answer.
Reseed staging
Staging runs SEED=demo, but the api only seeds an empty database, so a deploy never refreshes data that is already there. To get fresh demo data (new seed rows, dates close to today), empty the staging database and restart. This deletes everything in it, including what was typed in by hand. Staging only: check the container belongs to ritorno-staging before running anything.
- Back it up:
docker exec <staging backup container> db-backup now - Stop the
apiandworkerservices in Dokploy - Drop the app, migration and job schemas (
<POSTGRES_USER>and<POSTGRES_DB>are in the env, see below for their names):
docker exec <staging postgres container> psql -U <POSTGRES_USER> -d <POSTGRES_DB> -v ON_ERROR_STOP=1 -c 'drop schema if exists public, drizzle, pgboss cascade; create schema public;'- Start
api, thenworker. The api migrates, creates the admin fromADMIN_EMAILand seeds, and logsseeded demo data - Sign in as
owner.sunset-bay@demo.ritorno.testwith the env'sDEMO_PASSWORD, and as the sales viewersales@demo.ritorno.testwith itsDEMO_VIEWER_PASSWORD. Set that variable in the Dokploy env before step 4: without it, or on a database seeded before the viewer existed, there is no such account
Previews get a new database per pull request, so they need nothing.
Releasing
Merging to main deploys staging only. Production deploys when the release PR is merged, see releases.
Rollback
Run the release workflow by hand with tag set to an older tag (or to the current one to only redeploy). It points the production compose service at that tag and rebuilds it. Migrations only go forward: if a migration must be undone, restore the database from a backup first.
Backups
Each environment runs a backup service next to its database (docker/backup, same postgres major as the postgres service, so pg_dump always matches the server). Every day at 03:00 UTC it runs pg_dump -Fc and uploads backups/<DEPLOY_ENV>/<db>-<timestamp>.dump to S3_BUCKET, with the same S3_* keys as the api. The dump must be non-empty and readable by pg_restore --list before it is uploaded, and the uploaded size must match. Only after that it deletes dumps older than 14 days, always keeping the newest 3. A failed step logs backup: failed: <step>, uploads and deletes nothing, and the next run is the next day. The logs show object names and sizes only. Without S3_BUCKET and the S3 keys it logs that backups are off and idles.
A backup on demand, for example before a risky migration:
docker exec <backup container> db-backup nowWhen you change the postgres image, change docker/backup/Dockerfile in the same PR.
Restore a backup
db-restore (scripts/db-restore.sh, shipped in the backup image) downloads a dump and runs pg_restore --clean --if-exists --no-owner into the database you name, on the environment's postgres. It takes a database name, not a url: the connection and password come from the container's PG* env, so nothing secret shows in ps. With a terminal (docker exec -it) it asks you to type the database name. In production it refuses unless you pass --prod. Always restore into a scratch database first and check it.
- On the server, find the backup container of the environment:
docker ps --filter name=backup. - List the dumps with their size and date:
docker exec <backup container> db-restore list. - Create a scratch database:
docker exec <postgres container> psql -U <POSTGRES_USER> -c 'create database restore_check'. - Restore into it:
docker exec -it <backup container> db-restore latest restore_check(production:db-restore --prod latest restore_check). Pass a dump name such asritorno-2026-11-26T030000Z.dumpinstead oflatestfor an older day. - Check it:
docker exec <postgres container> psql -U <POSTGRES_USER> -d restore_check -c 'select count(*) from guest'and compare with what you expect. - To replace the live database: stop the
apiandworkerservices in Dokploy, run step 4 with the live database name instead ofrestore_check, start them again and check/health. Then drop the scratch database.
Tested locally with docker compose: postgres 17, the backup image and rclone serve s3 as the bucket. A dump was uploaded and old planted dumps were pruned down to the newest 3. A failing pg_dump (wrong password) uploaded and pruned nothing. db-restore latest brought back every row into a fresh database and again over a changed one, and refused with DEPLOY_ENV=production until --prod was passed.