# ============================================================================= # docker-compose.yml — production topology for the whole site. # # WHAT IT DOES # Defines every container, the single bridge network they share, and the # named volumes that survive restarts. This file alone IS production: # docker compose up --build # Dev layers docker-compose.dev.yml on top of it (see that file). # # WHEN IT RUNS # On the Raspberry Pi, via the Gitea Actions deploy workflow # (.gitea/workflows/deploy.yaml) on every push to main, and by hand. # # ENV VARS # Everything interpolated as ${VAR} below is read by the docker compose CLI # from ./.env at the repo root (which is gitignored — see sync-secrets.sh for # how it is distributed to the Pi). Note the two distinct mechanisms: # - ${VAR} in this file -> substituted by compose when parsing # - `env_file: ./.env` -> the whole file injected into the container # Many services need both: the container name comes from substitution, the # app config from env_file. # # Required by this file: POSTGRES_{USER,PASSWORD,DB,PORT,HOST}, # POSTGRES_GITEA_DB, BACKEND_HOST, # ICECAST_HOST, GITEA_HOST, OBSIDIAN_DIR, and the GITEA_* # secrets. PYTHON_HOST/PYTHON_PORT/TFL_APP_KEY/LIQUIDSOAP_HARBOR_PORT have # inline defaults. # # NAMING CONVENTION / WHY container_name IS SET FROM env # Service discovery is by container name on app-network. nginx builds its # upstreams from the same ${*_HOST} variables (nginx/entrypoint.sh), so # naming the containers from .env keeps the proxy and the containers in sync # from one source of truth. Renaming a *_HOST value renames the container AND # repoints nginx at it — do not change one without the other. # # WHAT IS PUBLISHED TO THE HOST # Only nginx (80, 443), gitea (3000 HTTP, 2222 SSH) and the liquidsoap harbor # port. Everything else is reachable only from inside app-network, which is # why the backend, db, silverbullet and python have no `ports:` — they are # deliberately not exposed and are reached through nginx. # ============================================================================= networks: # One flat bridge network; every service resolves every other by container # name via docker's embedded DNS at 172.28.0.11 (nginx configs point their # `resolver` there). The subnet is pinned explicitly so it cannot collide # with the Pi's LAN range or wander between compose versions. app-network: driver: bridge ipam: config: - subnet: 172.28.0.0/16 # PERSISTENT vs EPHEMERAL state # Named volumes below survive `docker compose down` and container rebuilds; # they are only destroyed by `docker compose down -v` or an explicit # `docker volume rm`. Bind mounts (./gitea/data, ./backend/token, ./logs, # ./certbot/conf) live in the repo checkout on the Pi and survive everything # short of deleting the directory. Anything inside a container's own # filesystem is ephemeral and gone on rebuild. volumes: # Postgres database — PERSISTENT. Holds the app DB *and* the Gitea DB. # Deleting this wipes posts, users, messages and every git repo's metadata. dbdata: # File upload — PERSISTENT. Written by the backend at /backend/uploads, # served read-only by nginx at /uploads/. Shared so nginx can serve user # uploads without proxying through the Go process. uploads: # Vue build — EPHEMERAL in spirit but a named volume for handoff: the # one-shot `vue` container writes the built SPA here and exits; nginx mounts # it at /etc/nginx/html and serves it. Repopulated on every rebuild. vue_dist: services: # One-shot build container, NOT a long-running service. `vite build` writes # the SPA into the shared vue_dist volume and the container exits 0. That # exit is expected — `docker compose ps` showing vue as Exited is healthy. # In dev this same service is overridden to run the Vite dev server instead # (docker-compose.dev.yml) and does stay up. vue: build: context: ./vue dockerfile: Dockerfile container_name: vue volumes: - vue_dist:/output networks: - app-network # The only public entrypoint. Terminates TLS, serves the SPA, reverse-proxies # everything else. Its config is chosen at container start — see # nginx/entrypoint.sh for the DEV_MODE / cert-present / bootstrap decision. # # START ORDERING: these `depends_on` entries are the plain list form, which # only waits for each container to be *created and started*, NOT for it to be # ready or healthy. That is deliberate — nginx's `resolver 127.0.0.11` means # upstreams are resolved per-request, so a late-starting backend produces a # temporary 502 rather than a failed nginx boot. The one ordering nginx # genuinely cares about (the Vue build finishing) is handled by the polling # loop in its entrypoint, because depends_on cannot express "wait for a # one-shot container to exit successfully" here. nginx: build: context: ./nginx dockerfile: Dockerfile container_name: nginx env_file: ./.env restart: always depends_on: - vue - backend - icecast2 - gitea - silverbullet - silverbullet-public - python networks: - app-network ports: - 80:80 - 443:443 volumes: # Shared with certbot: the cert store nginx reads and certbot writes. # PERSISTENT, and private key material — gitignored, never commit. - ./certbot/conf:/etc/letsencrypt # Shared with certbot: the ACME HTTP-01 challenge webroot. certbot drops # tokens in, nginx serves them on port 80. - ./certbot/www:/var/www/certbot - uploads:/uploads # The SPA written by the one-shot `vue` container. - vue_dist:/etc/nginx/html # Obtains the cert once, then renews every 12h forever. Disabled in dev via # the `disabled` profile in docker-compose.dev.yml. Note this is the stock # certbot image with our script bind-mounted over the entrypoint, so editing # certbot/entrypoint.sh takes effect on restart with no rebuild. Version is # pinned so a certbot release cannot change behaviour under us on redeploy. certbot: image: certbot/certbot:v3.1.0 container_name: certbot volumes: - ./certbot/entrypoint.sh:/entrypoint.sh - ./certbot/conf:/etc/letsencrypt - ./certbot/www:/var/www/certbot entrypoint: ["/entrypoint.sh"] env_file: - .env networks: - app-network backend: build: context: ./backend dockerfile: Dockerfile container_name: "${BACKEND_HOST}" restart: always # MAP form, not the list form used elsewhere: this waits for db's # healthcheck to pass, not merely for its container to start. Without it # the Go process raced Postgres on every boot, failed its first connection, # died, and relied on `restart: always` to try again — noisy log churn and # a slower cold start in practice. # `restart: always` is deliberately KEPT as the backstop: if db's health # probe were ever to stall past compose's patience, or the DB dropped out # later, the restart policy still recovers the backend. depends_on: db: condition: service_healthy networks: - app-network env_file: - ./.env volumes: # PERSISTENT, and the subtle one: the Spotify OAuth refresh token is # written here as spotify_token.json. Without this bind mount the token # would live in the container filesystem and be destroyed on every # rebuild, forcing a manual re-authorisation of Spotify after each # deploy. Gitignored (it is a credential). - ./backend/token/:/backend/token # Hand-off directory for the admin panel's Environment tab: the backend # queues a change here and env-apply/apply.py on the host applies it. # The backend gets this instead of .env itself or the Docker socket. - ./env-apply/run:/backend/env-apply # The Obsidian vault on the host, read by the backend for the notes API. # Same directory is mounted read-write into silverbullet (the editor) and # read-only into silverbullet-public (the /notes/ site). - ${OBSIDIAN_DIR}:/backend/notes - ./logs:/backend/logs # Same named volume nginx serves at /uploads/ — backend writes, nginx reads. - uploads:/backend/uploads # Shared with icecast2, which plays these as the radio fallback playlist; # the backend reads them to report what is queued. - ./icecast2/fallback_music:/backend/fallback_music # Reported to docker only: nothing acts on `unhealthy` since the autoheal # watchdog was retired (.deprecated/autoheal), so this just shows up in # `docker ps`. `restart: always` covers a crashed process, not a hung one. # start_period gives the Go process 10s to bind before failures start # counting. healthcheck: test: ["CMD", "wget", "-qO-", "http://localhost:8080/"] interval: 30s timeout: 5s retries: 3 start_period: 10s # Single Postgres instance serving TWO databases: ${POSTGRES_DB} for the Go # backend and ${POSTGRES_GITEA_DB} for Gitea. The postgres image only creates # POSTGRES_DB on first init, so the Gitea database was created by hand once; # a from-scratch restore must `createdb` it before Gitea will start. # Schema for the app DB comes from GORM auto-migration at backend start — # there are no migration files. db: image: postgres:16 container_name: "${POSTGRES_HOST}" restart: always env_file: - ./.env networks: - app-network volumes: - dbdata:/var/lib/postgresql/data # Postgres is the one dependency the backend genuinely cannot start # without, so `backend` gates on this healthcheck (condition: # service_healthy) instead of on mere container start. # TUNED FOR SLOW ARM HARDWARE: on a Raspberry Pi with a cold page cache a # first-boot Postgres can take well over a minute to finish initdb/recovery # and accept connections. start_period is the grace window during which a # failing probe does NOT count against `retries` and does NOT mark the # container unhealthy, so 90s costs nothing on a warm boot and prevents a # false "unhealthy" on a cold one. After that, 10 retries at 10s gives # another ~100s before anything gives up. `pg_isready -U $POSTGRES_USER` # is the cheapest real readiness probe (it does a protocol-level connect, # not just a TCP open). Note $$POSTGRES_USER: the doubled $ escapes # compose's own interpolation so the variable is expanded by the shell # INSIDE the container, from env_file, at probe time. healthcheck: test: ["CMD-SHELL", "pg_isready -U \"$$POSTGRES_USER\" -d \"$$POSTGRES_DB\" -q"] interval: 10s timeout: 5s retries: 10 start_period: 90s # Internet radio: one container running BOTH icecast2 (the streaming server # listeners connect to, proxied at /radio/) and liquidsoap (the source that # feeds it). See icecast2/entrypoint.sh. # # This is the only non-nginx service that publishes a host port, and it has # to: LIQUIDSOAP_HARBOR_PORT is the live-DJ input. A broadcasting client # (e.g. butt) connects straight to the Pi on that port with the icecast # source password and pre-empts the fallback playlist. It is NOT proxied # through nginx because it is a raw source protocol, not HTTP the SPA uses. # Note the host:container mapping uses the same variable on both sides, so # the published port always matches what liquidsoap actually binds. icecast2: build: context: ./icecast2 dockerfile: Dockerfile container_name: "${ICECAST_HOST}" restart: always networks: - app-network env_file: - ./.env volumes: - ./icecast2/fallback_music:/music:ro ports: - "${LIQUIDSOAP_HARBOR_PORT:-8001}:${LIQUIDSOAP_HARBOR_PORT:-8001}" # Browser-based editor for the vault, so notes are editable from a phone. # Proxied at /sb/ behind the nginx admin gate. The public site at /notes/ is # a SECOND, read-only SilverBullet over the same folder (silverbullet-public, # below). # # SINGLE-SPACE MODE is auto-detected because the mounted folder is not empty # (the alternative, multi-space, expects /data to be a SilverBullet-owned # folder of spaces). In this mode it treats the mount as the space itself and # writes NO database or metadata files into the vault — the folder stays a # plain directory of markdown that Obsidian still understands. silverbullet: image: ghcr.io/silverbulletmd/silverbullet:2.11.0 container_name: "${SILVERBULLET_HOST:-silverbullet}" restart: always networks: - app-network environment: SB_PORT: "${SILVERBULLET_PORT:-3000}" # The app builds its service-worker scope and asset URLs from this, which # is why nginx must NOT strip the /sb/ prefix. See nginx.conf.template. SB_URL_PREFIX: /sb # Only .obsidian: it is editor config and plugin JS, not notes. # # Deliberately NOT silverbullet-public's list, which also hides private/ # and templates/. The two answer different questions: the public # instance hides private/ because it must never be PUBLISHED, whereas # this is the editor, reachable only through the admin gate, and hiding # notes from it just stops you editing them. # # MUST be newline separated: a comma separated list is accepted silently # and does nothing. SB_SPACE_IGNORE: | .obsidian # Pinned rather than inferred from the mount's owner, so the files it # writes match the vault's ownership on the host. PUID: "1000" PGID: "1000" # Uncomment for a look-but-don't-touch first boot against a real vault: # SB_READ_ONLY: "true" volumes: - ${OBSIDIAN_DIR}:/data # The PUBLIC notes site at /notes/ (replaced Quartz): the same SilverBullet # image in read-only mode over the same vault, with no auth. Visitors get the # full client — navigation, backlinks, live queries — but cannot write. # Edits made in the /sb/ editor show up here live, since both watch one folder. # # KEEPING private/ PRIVATE takes three layers, because none is enough alone: # 1. SB_SPACE_IGNORE hides the paths from the file LIST, so they are # never indexed, searched or shown. It does NOT stop # a direct fetch of /.fs/private/x.md, nor the page # URL /notes/private/x (server-rendered with the # note's content), nor /.fs/.git/ — SilverBullet # only applies it to listing (checked in 2.11.0). # 2. nginx deny blocks 404 any private/ or templates/ segment and any # nested dot-folder under /notes/; this is what # actually closes those holes. Keep its folder list # in step with the one below. # 3. SB_REVISIONS the default ("unmanaged") serves history from the # vault's own .git, i.e. every version of every file # including private/ and deleted ones. Disabled here, # and nginx blocks /notes/.revisions as well. # # SB_READ_ONLY rejects writes server-side and also disables the shell and the # /.proxy endpoint (otherwise an open fetch proxy). The :ro mount is a second # lock on writes. It is flagged "legacy" upstream in favour of multi-space # mode's per-space publish setting, which cannot exclude a folder and would # mean migrating the editor too — revisit if single-space mode is removed. # # Keep the image tag in lockstep with the editor above. silverbullet-public: image: ghcr.io/silverbulletmd/silverbullet:2.11.0 container_name: "${SILVERBULLET_PUBLIC_HOST:-silverbullet-public}" restart: always networks: - app-network environment: SB_PORT: "${SILVERBULLET_PUBLIC_PORT:-3000}" # Same reason as the editor's SB_URL_PREFIX: nginx must not strip /notes/. SB_URL_PREFIX: /notes SB_READ_ONLY: "true" SB_REVISIONS: disabled # Newline separated (commas are silently ignored). Gitignore semantics, # so these match at any depth. Must match the folder list in the # /notes/ deny regex in nginx/nginx_routes.conf.template, or a new entry here is # hidden from the list but still publicly fetchable. SB_SPACE_IGNORE: | private/ templates/ .obsidian PUID: "1000" PGID: "1000" volumes: - ${OBSIDIAN_DIR}:/data:ro # SilverBullet has no concept of git, so this commits and pushes what it # writes, and pulls what Obsidian pushes from elsewhere. # # WHY A SEPARATE CONTAINER and not SilverBullet's own shell backend: enabling # that would give anyone who gets past the nginx admin gate command execution # inside the container. Since SilverBullet itself runs with no SB_USER, that # gate is the only thing protecting it, so the blast radius stays here. silverbullet-git: image: alpine/git:v2.49.1 container_name: "${SILVERBULLET_GIT_HOST:-silverbullet-git}" restart: always networks: - app-network # Same uid SilverBullet writes as, so commits don't leave root-owned objects # in .git that the host user can no longer clean up. user: "1000:1000" environment: SYNC_INTERVAL: "${SILVERBULLET_GIT_INTERVAL:-300}" GIT_AUTHOR_NAME: silverbullet GIT_AUTHOR_EMAIL: "silverbullet@${DOMAIN}" GIT_COMMITTER_NAME: silverbullet GIT_COMMITTER_EMAIL: "silverbullet@${DOMAIN}" # uid 1000 has no home in this image; give git somewhere writable. HOME: /tmp GIT_SSH_COMMAND: "ssh -i /ssh/id_ed25519 -o UserKnownHostsFile=/ssh/known_hosts -o IdentitiesOnly=yes" entrypoint: ["/bin/sh", "/git-sync.sh"] volumes: - ${OBSIDIAN_DIR}:/space - ./silverbullet/git-sync.sh:/git-sync.sh:ro - ./silverbullet/ssh:/ssh:ro # ssh will not run for a uid with no passwd entry; see the file's comment. - ./silverbullet/passwd:/etc/passwd:ro # FastAPI side-service, proxied at /py/ with the prefix stripped by nginx. # ROOT_PATH=/py tells FastAPI it is mounted under a prefix so the generated # OpenAPI/Swagger URLs (/py/docs) are correct even though the app's own # routes are written relative to /. # Note this service uses `environment:` with an explicit allow-list rather # than `env_file: ./.env` — it only gets the variables it needs and never # sees the database password, Spotify secrets, or the other API keys. # CLAUDE_API_KEY is the one secret on the list: the rowing photo reader and # the job posting reader (POST /internal/rowing/read and /internal/jobs/extract, # called by the backend, blocked at nginx) make their Claude calls here. python: build: context: ./python dockerfile: Dockerfile container_name: "${PYTHON_HOST:-python}" restart: always networks: - app-network environment: - PYTHON_PORT=${PYTHON_PORT:-8000} - ROOT_PATH=/py - TFL_APP_KEY=${TFL_APP_KEY:-} - CLAUDE_API_KEY=${CLAUDE_API_KEY:-} - CLAUDE_MODEL=${CLAUDE_MODEL:-} healthcheck: test: ["CMD", "python", "-c", "import urllib.request,os; urllib.request.urlopen(f\"http://localhost:{os.environ.get('PYTHON_PORT','8000')}/health\")"] interval: 30s timeout: 5s retries: 3 start_period: 10s # Self-hosted git, served at /gitea/ and on port 3000 directly. Also the CI # server: .gitea/workflows/deploy.yaml redeploys this whole stack on push to # main, executed by the act_runner that lives outside docker (gitea-runner/). # # ROOTLESS image: runs as an unprivileged user, so config lives in /etc/gitea # and data in /var/lib/gitea (not /data as in the root image) — that is why # the bind mounts look unlike most Gitea compose examples. # # The GITEA__section__KEY environment convention is Gitea's own: each maps to # an app.ini key and overrides the file at runtime, which is how the secrets # stay in .env instead of in the committed app.ini.template. # # entrypoint is overridden to run our wrapper (which materialises app.ini # from the template on first boot) before handing off to Gitea's real # docker-entrypoint.sh. dumb-init is kept as PID 1 for correct signal/zombie # handling, exactly as the stock image does it. gitea: image: docker.gitea.com/gitea:1.25.4-rootless container_name: "${GITEA_HOST}" entrypoint: ["/usr/bin/dumb-init", "--", "/etc/gitea/entrypoint.sh"] networks: - app-network environment: - GITEA__database__DB_TYPE=postgres - GITEA__database__HOST=${POSTGRES_HOST} - GITEA__database__NAME=${POSTGRES_GITEA_DB} - GITEA__database__USER=${POSTGRES_USER} - GITEA__database__PASSWD=${POSTGRES_PASSWORD} - GITEA__server__LFS_JWT_SECRET=${GITEA_LFS_JWT_SECRET} - GITEA__security__INTERNAL_TOKEN=${GITEA_INTERNAL_TOKEN} - GITEA__oauth2__JWT_SECRET=${GITEA_OAUTH2_JWT_SECRET} - USER_UID=1000 - USER_GID=1000 restart: always volumes: # PERSISTENT: repositories, LFS objects, avatars, sessions. The git data # itself. (Gitea's *relational* data is in Postgres, not here.) - ./gitea/data:/var/lib/gitea # PERSISTENT: app.ini is generated into this host directory on first boot # and then edited by Gitea itself, so it is gitignored — only the # template is committed. - ./gitea/config:/etc/gitea # Mounted over the config dir so the wrapper script is available at the # path the overridden entrypoint points at. - ./gitea/entrypoint.sh:/etc/gitea/entrypoint.sh:ro # Match the Pi's clock/zone so commit and CI timestamps are local time. - /etc/timezone:/etc/timezone:ro - /etc/localtime:/etc/localtime:ro ports: # 2222 = Gitea's *built-in* SSH server (the rootless image cannot use 22). # Clones use ssh://git@host:2222/... — including the deploy workflow, # which fetches from ssh://git@localhost:2222. - "2222:2222" # 3000 = HTTP, published directly as well as proxied at /gitea/, because # the act_runner registers against http://localhost:3000 from the host. - "3000:3000" depends_on: - db