Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

ralph(M15-203): Both Dockerfiles rewritten from scratch on the M14-203 measurements: production deps only, non-root, staged, and tested to build AND boot

ralph6a80320e8038
diff --git a/.github/smoke/docker-compose.yml b/.github/smoke/docker-compose.yml
index 5a74868d4..9ea41a332 100644
--- a/.github/smoke/docker-compose.yml
+++ b/.github/smoke/docker-compose.yml
@@ -82,8 +82,8 @@ services:
command: >
sh -c "
echo 'Running migrations...'
- cd /app/packages/db && ./node_modules/.bin/prisma migrate deploy
- cd /app/packages/core && ./node_modules/.bin/jiti src/code-migrations/migrate.ts
+ cd /app/packages/db && bunx [email protected] migrate deploy
+ cd /app/packages/core && bun scripts/migrate-code.ts
cd /app/apps/api && bun run start
"
diff --git a/.github/smoke/smoke.sh b/.github/smoke/smoke.sh
index 23b38d580..67add89b7 100755
--- a/.github/smoke/smoke.sh
+++ b/.github/smoke/smoke.sh
@@ -81,6 +81,28 @@ assert_ssr_route() {
echo " $path OK (HTTP 200, ${#body} bytes)"
}
+# The api container's start command runs `prisma migrate deploy` before it
+# boots, and `sh -c` does not stop on a failed line — so a broken migration
+# step is invisible: the api starts anyway and every probe stays green. That is
+# exactly how the rewrite shipped an image whose start command could not find
+# the Prisma CLI at all. Assert the step's own success line instead.
+assert_migrations_ran() {
+ local logs
+ logs=$(docker compose logs --no-color op-api 2>/dev/null || true)
+
+ if echo "$logs" | grep -qE 'sh: .*: not found|command not found'; then
+ echo "--- matching lines ---"
+ echo "$logs" | grep -nE 'sh: .*: not found|command not found' | head -20
+ fail "op-api's start command could not find a binary it invokes"
+ fi
+ if ! echo "$logs" | grep -q 'migrations have been successfully applied\|No pending migrations to apply'; then
+ echo "--- op-api logs (first 80 lines) ---"
+ echo "$logs" | head -80
+ fail "op-api never reported a successful 'prisma migrate deploy'"
+ fi
+ echo " op-api migrations applied"
+}
+
# An SSR failure that gets swallowed into a 200 would still show up here.
assert_no_server_errors() {
local svc="$1" logs
@@ -103,6 +125,9 @@ wait_for "api" "$API/healthcheck"
wait_for "worker" "$WORKER/healthz/ready"
wait_for "dashboard" "$DASHBOARD/api/healthcheck"
+echo "==> database migrations"
+assert_migrations_ran
+
echo "==> SSR rendering (the check that would have caught main-8e60)"
assert_ssr_route "/login"
diff --git a/apps/api/Dockerfile b/apps/api/Dockerfile
index fca89b5c4..0791156ac 100644
--- a/apps/api/Dockerfile
+++ b/apps/api/Dockerfile
@@ -1,24 +1,30 @@
+# API image — one image, two roles (ROLE=api | worker | all).
+#
+# Rewritten for M15-203 against the per-layer measurements in
+# docs/DOCKER_SLIMMING.md (M14-203). Every cut below is one that document
+# measured AND boot-tested; nothing it did not measure is attempted, and its
+# "not worth it" list (§9) is treated as binding — the geo databases stay
+# baked in, `netcat-openbsd` stays, the api's openssl/libssl3 security upgrade
+# stays, and the base image is not up for discussion (see below).
+#
# ADR-016 pins Bun exactly at 1.4.0 (floor >=1.2.23). Its declared home is
# `.bun-version` + this ARG; the file does not exist in the tree yet, so this
# ARG is the single home until it does. Keep the two in sync when it lands.
ARG BUN_VERSION=1.4.0
-# P13 (ADR-014) made `bun install` the installer for the whole repo, so this
-# image installs with bun and nothing else: no second package manager, no
-# Node-side installer shim, no lockfile or workspace file bun does not read.
-# ADR-014 Problems §4 already ruled that a bun-base image cannot bootstrap
-# another installer, and its answer is the shape below — ONE full-workspace
-# install per image, from the root manifest bun.lock was resolved against.
+# BASE — oven/bun:${BUN_VERSION}-slim. DECIDED, not re-evaluated: Carl closed
+# alpine and distroless on 2026-09-08 ("fuck it, lets keep slim"). Distroless
+# has no shell and all three shipped compose files start this image with
+# `sh -c "…"`; alpine is musl against a `debian-openssl-3.0.x` Prisma engine.
#
-# `bunfig.toml` is copied with the tree on purpose: it carries
-# `[install] ignoreScripts = true` (ADR-014 amendment 2 — no dependency build
-# script has ever run in an image this repo ships) and `linker = "isolated"`.
-# The two install lines below name `--linker=isolated` explicitly anyway, so
-# the layout is a property of the command and not only of a file.
-
-# BASE — OS packages every stage needs. curl and netcat-openbsd are here for
-# the docker-compose HEALTHCHECKs, which exec *inside* the running container,
-# so they must reach the runtime stage too, not just the install stages.
+# The apt set is the one the running container needs, not the one the build
+# needs: `curl` backs the CMD-SHELL HEALTHCHECKs in all three compose files and
+# `netcat-openbsd` backs self-hosting/coolify.yml's `nc -z` wait loops, and
+# both exec *inside* the container. `ca-certificates`, `openssl` and `libssl3`
+# are already present in the base — naming them buys the Debian-Security
+# upgrade of libssl3t64/openssl (measured: 3.5.6 -> 3.5.7, 3,416,908 B
+# compressed). DOCKER_SLIMMING.md §8 row 4 files dropping them as a security
+# decision for Carl, not a size one, and recommends keeping the upgrade.
FROM oven/bun:${BUN_VERSION}-slim AS base
RUN apt-get update && \
@@ -31,7 +37,7 @@ RUN apt-get update && \
&& apt-get clean && \
rm -rf /var/lib/apt/lists/*
-# TOOLCHAIN — the full workspace, installed once with devDependencies, because
+# TOOLCHAIN — the full workspace, installed once WITH devDependencies, because
# codegen needs them (core's `tar` for scripts/download-geo.ts, db's `prisma`
# CLI for schema generation). Neither this stage nor `build` ships.
#
@@ -39,13 +45,17 @@ RUN apt-get update && \
# a workspace install has to see every member's `package.json` anyway, Docker's
# COPY cannot glob a directory level, and the old per-package list failed
# SILENTLY when it drifted (a missing manifest omitted a package from
-# node_modules and surfaced as a runtime "Cannot find module"). The cost is
-# that a source edit invalidates the install layer; the gate builds from
-# scratch regardless.
+# node_modules and surfaced as a runtime "Cannot find module").
+#
+# `bunfig.toml` is copied with the tree on purpose: it carries
+# `[install] ignoreScripts = true` (ADR-014 amendment 2 — no dependency build
+# script has ever run in an image this repo ships) and `linker = "isolated"`.
+# Both install lines name `--linker=isolated` explicitly anyway, so the layout
+# is a property of the command and not only of a file.
#
# No python3/make/g++: with `ignoreScripts` nothing compiles at install time,
# and the two native addons this image needs (@node-rs/argon2, sharp) resolve
-# to prebuilt linux-x64 packages.
+# to prebuilt packages.
FROM base AS toolchain
WORKDIR /app
@@ -64,28 +74,46 @@ ENV DATABASE_URL=$DATABASE_URL
RUN bun run codegen
-# PROD-DEPS — the node_modules that ships. Derived from `build` so the
-# generated output above survives, but with every node_modules directory
-# deleted first: a `--production` install must not be a filtered view of a
-# devDependency-laden tree, it must be an install that was production-only
-# from the start.
+# PROD-DEPS — the node_modules that ships, and the only place anything is
+# deleted. Derived from `build` so the generated output above survives, but
+# with every node_modules directory removed first: a `--production` install
+# must not be a filtered view of a devDependency-laden tree, it must be an
+# install that was production-only from the start.
#
-# `--filter '@openpanel/api'` is the closure boundary, and it is the
-# "equivalent" a bare `bun install --production --frozen-lockfile` is traded
-# for. The bare form installs every workspace member's production closure —
-# apps/start's React/Nitro tree included, ~1.1GB the API never loads
-# (docs/TECH_DEBT.md §5d). Verified on Bun 1.4.0: `--filter` follows
-# `workspace:` edges TRANSITIVELY (api -> core -> db -> redis/email/payments)
-# and installs nothing outside them, which is exactly the boundary the
-# previous image expressed with its own installer's `...` filter suffix. It
-# fails visibly rather than silently: a filter that matches nothing leaves the
-# app unable to resolve its imports, and tooling/gates/p13-images.sh boots the
-# image and asserts /healthz/ready before it passes.
+# `--filter '@openpanel/api'` is the closure boundary. Verified on Bun 1.4.0:
+# it follows `workspace:` edges TRANSITIVELY (api -> core -> db ->
+# redis/email/payments) and installs nothing outside them. It fails visibly
+# rather than silently — a filter that matches nothing leaves the app unable to
+# resolve its imports, and tooling/gates/p13-images.sh boots the image and
+# asserts /healthz/ready before it passes.
FROM build AS prod-deps
RUN find . -maxdepth 4 -type d -name node_modules -prune -exec rm -rf {} + && \
bun install --production --frozen-lockfile --linker=isolated --filter '@openpanel/api'
+# TRIM — the four measured, boot-tested cuts from DOCKER_SLIMMING.md's
+# "What chore C6 should implement" list. They run HERE, in a build stage, and
+# never in the runtime stage: deleting a file in the final stage adds a
+# whiteout and makes the image LARGER (§9.3), so the only way a removal shrinks
+# anything is for the bytes never to be COPYed forward.
+#
+# 1. The musl-only native store entries. The base is glibc, so a package
+# whose name says `musl` is dead by construction on every arch this image
+# is built for; the glob is what §8 row 3 asks for instead of a list of
+# content-hashed store keys, and it is arch-independent for the same
+# reason (an arm64 build resolves `-linux-arm64`, never `-linuxmusl-*`).
+# §2.4 proved sharp and @node-rs/argon2 still load without them.
+# 2/3. apps/api/scripts (97.4% of that layer: two mock fixtures, 15.5 MB,
+# whose only caller is the non-production `test:manage` script) and
+# apps/api/e2e. Removed here rather than by splitting the COPY below,
+# which is what keeps that COPY drift-proof.
+# 4. `*.test.ts` under packages/, and packages/sdks — nothing in the api's
+# closure imports an SDK (grepped 2026-09-11).
+RUN find /app/node_modules/.bun -maxdepth 1 -name '*musl*' -prune -exec rm -rf {} + && \
+ rm -rf /app/apps/api/scripts /app/apps/api/e2e && \
+ find /app/packages -type f -name '*.test.ts' -delete && \
+ rm -rf /app/packages/sdks
+
# RUNTIME — oven/bun:${BUN_VERSION}-slim, nothing but the app: the isolated
# node_modules and the source (with `build`'s generated output baked in), both
# from `prod-deps` so the store and the per-package symlinks into it come from
@@ -94,16 +122,34 @@ FROM base AS runtime
ENV NODE_ENV=production
+# The image runs as `bun` (uid 1000, created by the base image), so HOME must
+# point somewhere that user owns. Docker does not move HOME with USER, and the
+# start command in all three compose files runs `bunx [email protected] migrate
+# deploy`, which writes to bun's install cache under HOME.
+ENV HOME=/home/bun
+
WORKDIR /app
-COPY --from=prod-deps /app/package.json ./
-COPY --from=prod-deps /app/node_modules ./node_modules
+COPY --from=prod-deps --chown=bun:bun /app/package.json ./
+COPY --from=prod-deps --chown=bun:bun /app/node_modules ./node_modules
# Apps — src/ is the artifact now; there is no dist/.
-COPY --from=prod-deps /app/apps/api ./apps/api
-
-COPY --from=prod-deps /app/packages ./packages
-COPY --from=prod-deps /app/tooling/typescript ./tooling/typescript
+COPY --from=prod-deps --chown=bun:bun /app/apps/api ./apps/api
+
+COPY --from=prod-deps --chown=bun:bun /app/packages ./packages
+COPY --from=prod-deps --chown=bun:bun /app/tooling/typescript ./tooling/typescript
+
+# The shipped start command is
+# `cd /app/packages/db && bunx [email protected] migrate deploy`, so the schema and
+# the migrations directory are load-bearing image content, not source-tree
+# detail. Assert them at build time — the alternative is a self-hoster finding
+# out at container start. (`packages/core/scripts/migrate-code.ts` is the code
+# migrations' entrypoint and is asserted for the same reason.)
+RUN test -f /app/packages/db/prisma/schema.prisma && \
+ test -d /app/packages/db/prisma/migrations && \
+ test -f /app/packages/core/scripts/migrate-code.ts
+
+USER bun
WORKDIR /app/apps/api
diff --git a/apps/start/Dockerfile b/apps/start/Dockerfile
index 23c07f837..359deb7c9 100644
--- a/apps/start/Dockerfile
+++ b/apps/start/Dockerfile
@@ -1,28 +1,30 @@
# Dashboard image.
#
-# THE TWO ROOT CAUSES THIS FILE FIXES. Measured at 4925d2f7 (2026-09-07) this
-# image did not build, and neither break came from P13:
+# Rewritten for M15-203 against docs/DOCKER_SLIMMING.md (M14-203) §1.2 and
+# §4.2. That document measured this image layer by layer and found almost
+# nothing of it is ours: 84.5% is `node:${NODE_VERSION}-slim`, of which one
+# layer (the Node runtime itself) is 53%, and the application `.output` is
+# 9,024,064 B — 9.5%. So the only cuts available here are the two apt ones
+# below, and the base is DECIDED and stays put (§9.1, §9.2 — Carl, 2026-09-08).
#
-# 1. STALE COPIES OF THE DELETED `@openpanel/auth` PACKAGE. Two lines copied
-# its manifest and its whole directory out of the build stage. ADR-007
-# dissolved that package into `@openpanel/core` and P11 deleted its
-# directory, so both COPYs referenced a path that no longer exists and
-# the build would have died on them. The grep gate for this file asserts
-# that neither the old installer nor that path is named anywhere in it.
+# THE TWO ROOT CAUSES THIS FILE FIXES, carried forward from M13-003 because
+# both are still the reason it is shaped this way:
+#
+# 1. STALE COPIES OF THE DELETED `@openpanel/auth` PACKAGE. ADR-007 dissolved
+# that package into `@openpanel/core` and P11 deleted its directory, so
+# the COPYs that named it referenced a path that no longer exists. The
+# grep gate for this file asserts that neither the old installer nor that
+# path is named anywhere in it.
#
# 2. THE WORKSPACE-SUBSET INSTALL. The old base stage copied ~10 of the
-# workspace's ~20 member manifests and none of `packages/core`, which was
-# fine while the dashboard imported nothing from it. P11 made
+# workspace's ~20 member manifests and none of `packages/core`. P11 made
# `apps/start` import `@openpanel/core/modules/*.constants`, and
# `vite-tsconfig-paths` then reads `packages/core/tsconfig.json`, whose
# `"extends": "@openpanel/tsconfig/base.json"` cannot resolve when
-# `tooling/typescript` was never installed as a workspace member:
-# `failed to resolve "extends":"@openpanel/tsconfig/base.json" in
-# /app/packages/core/tsconfig.json`. The fix is not another manifest in
-# the list — it is ONE FULL WORKSPACE INSTALL, which is the shape ADR-014
-# Problems §4 rules for a bun-base image anyway. A subset install is what
-# the deleted `verifyDepsBeforeRun: false` setting existed to paper over;
-# that setting has no successor, so the subset it protected has to go.
+# `tooling/typescript` was never installed as a workspace member. The fix
+# is not another manifest in the list — it is ONE FULL WORKSPACE INSTALL,
+# which is the shape ADR-014 Problems §4 rules for a bun-base image
+# anyway.
#
# ADR-014 scopes P13 to the INSTALLER only. The build runs on bun; the runtime
# deliberately stays Node — nothing here decides otherwise.
@@ -34,17 +36,16 @@ ARG NODE_VERSION=24.19.0
# `[install] ignoreScripts = true` (ADR-014 amendment 2) and
# `linker = "isolated"`; `--linker=isolated` is named here too so the layout is
# a property of the command, not only of a file.
+#
+# NO apt BLOCK. There used to be one here mirroring the runner's. It shipped
+# nothing — the runner starts `FROM node:${NODE_VERSION}-slim`, a fresh base,
+# and copies only `.output` — so it bought build time and nothing else.
+# Measured (§4.2): deleting it changes the image by -74 B and the build still
+# succeeds. `bun install` needs TLS and `oven/bun:${BUN_VERSION}-slim` already
+# ships `ca-certificates`; nothing in `bun install` or `vite build` calls
+# `curl`, `openssl` or `libssl3`.
FROM oven/bun:${BUN_VERSION}-slim AS install
-RUN apt-get update && \
- apt-get install -y --no-install-recommends \
- ca-certificates \
- openssl \
- libssl3 \
- curl \
- && apt-get clean && \
- rm -rf /var/lib/apt/lists/*
-
WORKDIR /app
COPY . .
@@ -71,12 +72,18 @@ RUN cd apps/start && NITRO=1 SELF_HOSTED=1 bun run build
# RUNNER — node:${NODE_VERSION}-slim, and only `.output`.
#
# The Nitro output is standalone: it ships its own `.output/server/node_modules`
-# (222 packages) and `.output/public` holds every asset `/login` references.
-# Verified by running `.output/server/index.mjs` from an otherwise empty
-# directory on 2026-09-07: `/api/healthcheck` 200, `/login` 200 / 136710 bytes,
-# no resolution errors. So the workspace `node_modules` and the per-package
-# source copies the previous image carried are dead weight, and dropping them
-# is what removes the last reference to the deleted `@openpanel/auth` tree.
+# and `.output/public` holds every asset `/login` references. So the workspace
+# `node_modules` and the per-package source copies the previous image carried
+# are dead weight, and dropping them is what removes the last reference to the
+# deleted `@openpanel/auth` tree.
+#
+# apt: `curl` for the CMD-SHELL HEALTHCHECKs, `ca-certificates` for it. The
+# `openssl` and `libssl3` names that used to be here were inert (§4.2, 47 B):
+# `curl` pulls libssl3 through libcurl4 and `ca-certificates` pulls openssl,
+# and Node needs neither — it bundles its own OpenSSL (`ldd node` shows no
+# libssl) and its own CA store. `ca-certificates` itself is 1,113,308 B and is
+# KEPT: §8's dashboard row 3 files removing it as Carl's call, since it would
+# break any HTTPS `curl` inside the container.
FROM node:${NODE_VERSION}-slim AS runner
ENV NODE_ENV=production
@@ -84,15 +91,13 @@ ENV NODE_ENV=production
RUN apt-get update && \
apt-get install -y --no-install-recommends \
ca-certificates \
- openssl \
- libssl3 \
curl \
&& apt-get clean && \
rm -rf /var/lib/apt/lists/*
WORKDIR /app/apps/start
-COPY --from=build /app/apps/start/.output ./.output
+COPY --from=build --chown=node:node /app/apps/start/.output ./.output
# The standalone Nitro output preserves the target package name for the
# esm-env -> esm-env-runtime override. @number-flow/react imports "esm-env"
@@ -112,9 +117,14 @@ RUN set -eux; \
'}' \
> "${node_modules}/esm-env/package.json"; \
fi; \
+ chown -R node:node "${node_modules}/esm-env"; \
cd /app/apps/start/.output/server; \
node --input-type=module -e 'const assert = await import("node:assert/strict"); const env = await import("esm-env"); assert.equal(env.BROWSER, false, "esm-env BROWSER should be false in the Node SSR runtime"); assert.equal(env.NODE, true, "esm-env NODE should be true in the Node SSR runtime"); assert.equal(env.DEV, false, "esm-env DEV should be false in the production runtime");'
+# The SSR server reads `.output` and writes nothing. `node` (uid 1000) is
+# created by the base image.
+USER node
+
EXPOSE 3000
# Start the Tanstack Start server
diff --git a/docs/DOCKER_IMAGES.md b/docs/DOCKER_IMAGES.md
new file mode 100644
index 000000000..506dde478
--- /dev/null
+++ b/docs/DOCKER_IMAGES.md
@@ -0,0 +1,147 @@
+# The two shipped images — what they contain, and what they measure
+
+> Task: M15-203. Rewritten from `docs/DOCKER_SLIMMING.md` (M14-203), which is
+> the specification. Every cut here was measured there; nothing it did not
+> measure was attempted, and its §9 "not worth it" list is treated as binding.
+
+## How to read every number below
+
+Two quantities, never mixed — M14-203's §0 convention, kept verbatim:
+
+| Term | Obtained by | Means |
+|---|---|---|
+| **compressed** | `docker image inspect --format '{{.Size}}'` | the gzip blob bytes — what a registry stores and a `docker pull` transfers. Under this box's containerd snapshotter this is what `.Size` reports. |
+| **uncompressed** | sum of `docker history --no-trunc --format '{{.Size}}'` | the layer tar/diff bytes — what the snapshotter unpacks onto disk |
+| **DISK USAGE** | `docker images` | **not used for any claim**. It double-counts: unpacked snapshot **plus** the compressed blobs it was unpacked from. |
+
+## Before / after
+
+Both images were built from scratch on this box on **2026-09-11**.
+
+- **before** = `apps/api/Dockerfile` and `apps/start/Dockerfile` as of
+ `de2090d7`, built with the same commands `tooling/gates/p13-images.sh` uses.
+- **after** = this task's rewrite, as built by the gate run
+ (`bash tooling/gates/p13-images.sh`) that passed.
+
+### api
+
+| | compressed (`image inspect .Size`) | uncompressed (`docker history` sum) | `docker images` DISK USAGE |
+|---|---:|---:|---:|
+| M13-003 baseline (recorded then) | 412,899,646 B | — | 2.11 GB |
+| before, `de2090d7` | 407,700,106 B | 1,669,828,490 B (1,669.8 MB) | 2.08 GB |
+| **after** | **341,483,316 B** | **1,472,071,590 B (1,472.1 MB)** | 1.81 GB |
+| **change vs before** | **−66,216,790 B (−16.2 %)** | **−197,756,900 B (−11.8 %)** | — |
+
+### dashboard
+
+| | compressed | uncompressed | DISK USAGE |
+|---|---:|---:|---:|
+| M13-003 baseline (recorded then) | 95,217,524 B | — | 419 MB |
+| before, `de2090d7` | 94,745,677 B | 317,767,500 B (317.8 MB) | 413 MB |
+| **after** | **94,746,761 B** | **317,767,500 B (317.8 MB)** | 413 MB |
+| **change vs before** | **+1,084 B (+0.001 %)** | 0 B | — |
+
+**The dashboard image does not get smaller, and M14-203 said it would not.**
+§1.2 measured it as 84.5 % `node:24.19.0-slim` with 9,024,064 B of application
+content, and the only two cuts available in this repo are worth 74 B (the
+install-stage apt block, which ships nothing) and 47 B (the inert `openssl` /
+`libssl3` names in the runner). Per-layer blob measurement of before vs after,
+2026-09-11:
+
+| Runner layer | before | after |
+|---|---:|---:|
+| apt | 5,744,455 B | 5,744,628 B |
+| `COPY .output` | 8,536,296 B | 8,537,010 B |
+
+The apt layer moves by 173 B because dropping two names changes which packages
+dpkg marks manually-installed, not which packages are installed — `curl` pulls
+`libssl3` through `libcurl4` and `ca-certificates` pulls `openssl`. The
+`.output` layer moves by 714 B because `COPY --chown=node:node` writes
+different uid/gid into the tar headers. Both are below build-to-build noise.
+The dashboard changes here are **for clarity and for the non-root user**, not
+for size.
+
+## What the api image gave up, and what it did not
+
+Cuts taken — all four from M14-203's "What chore C6 should implement" list,
+applied in the `prod-deps` **build** stage so the bytes are never COPYed into
+the final image. (Deleting in a final stage adds a whiteout and makes the image
+*larger* — §9.3.)
+
+| Cut | Spec | Result |
+|---|---|---|
+| musl-only store entries, by glob | §8 row 3 / Open questions | 4 entries removed, store 955 → 951 |
+| `apps/api/scripts` + `apps/api/e2e` | §6 | 15.7 MB of test fixture out of a production image |
+| `*.test.ts` under `packages/` | §6 | 0 remain in the image |
+| `packages/sdks` | §5 | nothing in the api's closure imports an SDK |
+
+The musl glob is what §8 row 3 asks for in place of a list of content-hashed
+store keys, and it is arch-independent for the reason the name gives: the base
+is glibc, so an arm64 build resolves `-linux-arm64` and never `-linuxmusl-*`.
+It also catches `@next/swc-linux-x64-musl` (54 MB compressed), which is most of
+the saving — that package belongs to the `react-email` → `next` chain §2.2
+found has no importer at all, so removing its dead-libc half costs nothing.
+**The 144 MB `react-email`/`next` cut itself is NOT taken here**: it is a
+`packages/email/package.json` + `bun.lock` change and needs its own task.
+
+Cuts deliberately **not** taken, each because M14-203 says so:
+
+- **The two `GeoLite2-*.mmdb` files stay baked in** (§3.3). They are read on the
+ ingest hot path, their absence is silent and permanent, every health probe
+ the project ships stays green without them, and no boot-fetch code exists.
+- **`netcat-openbsd` stays** (§9.5) — `self-hosting/coolify.yml` waits on
+ Postgres and ClickHouse with `nc -z`.
+- **The api's `openssl` / `libssl3` apt names stay** (§8 row 4). They buy a
+ Debian-Security upgrade of the base's already-installed OpenSSL; dropping
+ them is 3,416,908 B and a security decision for Carl, not a size one.
+- **`ca-certificates` stays in the dashboard runner** (§8 dashboard row 3) —
+ same reason, Carl's call.
+- **The base images stay** (§9.1, §9.2). Closed by Carl on 2026-09-08.
+- **`COPY /app/packages` stays one line** (§9.4). Splitting it re-introduces the
+ silent-drift failure the single COPY exists to prevent.
+
+## Non-root
+
+Both final stages run as uid 1000 — `bun` in the api image, `node` in the
+dashboard image, both created by their base images. The api stage also sets
+`HOME=/home/bun`, because Docker does not move `HOME` with `USER` and the
+shipped start command's `bunx [email protected]` writes to bun's install cache
+under `HOME`. Verified 2026-09-11: `id` inside both running containers reports
+uid 1000, the api answers `/healthz/ready` 200, and the dashboard
+server-renders `/login`.
+
+## Migrations
+
+Migrations run in the **api container's start command** (Carl, 2026-09-08 —
+*"I don't want to move the migration out of it"*). All three shipped compose
+files now read:
+
+```sh
+cd /app/packages/db && bunx [email protected] migrate deploy
+cd /app/packages/core && bun scripts/migrate-code.ts
+```
+
+- The Prisma CLI is a devDependency, so the image's production-only install
+ correctly omits it; `bunx` fetches it at start rather than the image carrying
+ the CLI plus `@prisma/engines` (~86 MB) for a once-per-deploy command.
+- **The version is pinned.** A bare `bunx prisma` fetches latest, which can
+ drift from the generated client and the schema the image ships. The repo is
+ on `prisma 6.14.0`.
+- `packages/db/prisma/schema.prisma`, `packages/db/prisma/migrations` and
+ `packages/core/scripts/migrate-code.ts` are asserted at build time by a `RUN
+ test` in the runtime stage. The whole command depends on them, and the
+ alternative is a self-hoster finding out at container start.
+- No new apt packages: the runtime stage is `FROM base`, which already carries
+ `ca-certificates`, `openssl` and `libssl3` — what Prisma's engines need.
+- Accepted consequence, recorded not re-argued: the api container needs
+ npm-registry reach at start and downloads the CLI on a cold start.
+
+Verified 2026-09-11 against a scratch local Postgres, running as uid 1000
+inside the built image: `bunx [email protected] migrate deploy` → *"All migrations
+have been successfully applied."*; re-run → *"No pending migrations to apply."*;
+the full three-line start command then reached `/healthz/ready` 200 in 8 s.
+
+`.github/smoke/smoke.sh` gained `assert_migrations_ran`, because `sh -c` does
+not stop on a failed line: a broken migration step leaves the api booting and
+every probe green, which is exactly how an image whose start command could not
+find the Prisma CLI shipped unnoticed.
diff --git a/docs/OPS_DEPLOY_MIGRATION.md b/docs/OPS_DEPLOY_MIGRATION.md
index 2d9964f11..65d43ecdf 100644
--- a/docs/OPS_DEPLOY_MIGRATION.md
+++ b/docs/OPS_DEPLOY_MIGRATION.md
@@ -5,6 +5,14 @@
> agent has applied it. Source facts are `docs/ANSWERS.md §1.1` and `§1.3`
> (Carl-authored, authoritative).
+> **SUPERSEDED, 2026-09-11 (M15-203) — the migration commands only.** Every
+> `./node_modules/.bin/prisma` / `./node_modules/.bin/jiti` invocation below is
+> a record of what a past attempt verified, and neither binary is in the image
+> any more. The shipped commands are now
+> `cd /app/packages/db && bunx [email protected] migrate deploy` and
+> `cd /app/packages/core && bun scripts/migrate-code.ts`; see
+> `docs/DOCKER_IMAGES.md`. Everything else in this file stands.
+
## What changed in the repo
`apps/worker` is deleted (M9-003) and `apps/api/Dockerfile` now builds a
diff --git a/docs/TECH_DEBT.md b/docs/TECH_DEBT.md
index 70184d053..aa388ade4 100644
--- a/docs/TECH_DEBT.md
+++ b/docs/TECH_DEBT.md
@@ -1737,6 +1737,54 @@ between them is a decision about what the image contains, not an installer
change. M13-003's scope is the swap. The note is repeated in both compose
templates so a self-hoster reading the file sees it.
+**RESOLVED by M15-203 (2026-09-11).** Carl decided the question on 2026-09-08:
+`bunx`, and migrations stay in the api container's start command. All three
+compose files now run `cd /app/packages/db && bunx [email protected] migrate deploy`
+— pinned, because a bare `bunx prisma` fetches latest and can drift from the
+generated client and the schema. The api image does not carry the CLI plus
+`@prisma/engines` (~86 MB) for a once-per-deploy command; the accepted cost is
+that the container needs npm-registry reach at start. `apps/api/Dockerfile`'s
+runtime stage now asserts `packages/db/prisma/schema.prisma`,
+`packages/db/prisma/migrations` and `packages/core/scripts/migrate-code.ts` at
+build time, and `.github/smoke/smoke.sh` gained `assert_migrations_ran`, because
+`sh -c` does not stop on a failed line — which is how this shipped unnoticed.
+Verified 2026-09-11 inside the built image as uid 1000 against a scratch local
+Postgres: "All migrations have been successfully applied.", then "No pending
+migrations to apply." on a re-run.
+
+Two corrections to the paragraph above, both measured 2026-09-11:
+
+- The claim that "both the old and the new image install production
+ dependencies only" is wrong about the old one. On `main`,
+ `apps/api/Dockerfile` copied `packages/db` from a build stage installed WITH
+ devDependencies, so `packages/db/node_modules/.bin/prisma` shipped by
+ construction. This was a **rewrite-introduced regression**, not a
+ longstanding gap.
+- `./node_modules/.bin/jiti` is no longer present either — `jiti` is not a
+ dependency of any workspace package any more, and
+ `src/code-migrations/migrate.ts` now only *exports* the runner (ADR-022 R7).
+ The compose files run `cd /app/packages/core && bun scripts/migrate-code.ts`
+ instead, which is the shell that reads the environment for it.
+
+### `3-init-ch.ts` cannot run against an empty ClickHouse database
+
+Found while proving M15-203's start command; **outside that task's scope, left
+unfixed**. `packages/core/src/code-migrations/3-init-ch.ts` (and its paired
+`3-init-ch.sql:1`) opens with
+
+```
+RENAME TABLE cohort_events_mv TO cohort_events_mv_tmp
+```
+
+so on a database where that materialized view does not exist yet the migration
+dies with `Table <db>.cohort_events_mv doesn't exist (UNKNOWN_TABLE, code 60)`.
+Measured 2026-09-11 twice, against two freshly created ClickHouse databases —
+once inside the built api image, once on the host with
+`bun packages/core/scripts/migrate-code.ts` — with identical output, so it is
+neither an image nor a container property. Migrations 1 and 2 apply first, so a
+fresh self-host install is left partially migrated rather than untouched. The
+api still boots: the shipped start command is one `sh -c` without `set -e`.
+
### Also recorded
- **`.bun-version` still does not exist.** ADR-016 names it as Bun's declared
diff --git a/self-hosting/coolify.yml b/self-hosting/coolify.yml
index a5cb809a8..74a20bea9 100644
--- a/self-hosting/coolify.yml
+++ b/self-hosting/coolify.yml
@@ -111,9 +111,9 @@ services:
image: lindesvard/openpanel-api:latest
restart: always
# Same contract as docker-compose.template.yml's op-api: no second package
- # manager exists in this image (P13 / ADR-014), so nothing below may invoke
- # one. The same KNOWN, pre-existing `prisma` break applies — see the note
- # there and docs/TECH_DEBT.md.
+ # manager exists in this image (P13 / ADR-014), migrations run in this
+ # container's start command, and the Prisma CLI is fetched by `bunx` at a
+ # PINNED version rather than shipped. See the long note there.
command: >
sh -c "
echo 'Waiting for PostgreSQL to be ready...'
@@ -129,8 +129,8 @@ services:
echo 'ClickHouse is ready'
echo 'Running migrations...'
- cd /app/packages/db && ./node_modules/.bin/prisma migrate deploy
- cd /app/packages/core && ./node_modules/.bin/jiti src/code-migrations/migrate.ts
+ cd /app/packages/db && bunx [email protected] migrate deploy
+ cd /app/packages/core && bun scripts/migrate-code.ts
cd /app/apps/api && bun run start
"
diff --git a/self-hosting/docker-compose.template.yml b/self-hosting/docker-compose.template.yml
index a36aaae55..f8cf6ec20 100644
--- a/self-hosting/docker-compose.template.yml
+++ b/self-hosting/docker-compose.template.yml
@@ -131,23 +131,26 @@ services:
restart: always
# P9.5 collapsed the two-Dockerfile setup onto oven/bun, and P13
# (ADR-014, M13-003) made bun the installer as well, so no second package
- # manager exists in this image or in the repo it is built from. Nothing
- # here may invoke one: migrations and boot run the installed binaries
- # directly (their `#!/bin/sh` shims resolve `node` to Bun's node-fallback
- # shim) and `bun run start` for apps/api's own script.
+ # manager exists in this image or in the repo it is built from.
#
- # KNOWN BREAK, pre-existing and NOT introduced by P13: `prisma` is a
- # devDependency of packages/db, so the image's production-only install
- # correctly omits it and the first line below cannot run. Measured against
- # a freshly built image on 2026-09-07:
- # `sh: ./node_modules/.bin/prisma: not found`. Recorded in
- # docs/TECH_DEBT.md; fixing it is a decision about what the image ships,
- # not an installer change.
+ # MIGRATIONS RUN HERE, in the api container's start command (Carl,
+ # 2026-09-08). The Prisma CLI is a devDependency of packages/db, so the
+ # image's production-only install correctly omits it; `bunx` fetches it at
+ # start instead of the image carrying the CLI plus @prisma/engines (~86 MB)
+ # for a command that runs once per deploy. The version is PINNED because a
+ # bare `bunx prisma` fetches latest, which can drift from the generated
+ # client and the schema this image ships. Accepted consequence: the api
+ # container needs npm-registry reach at start.
+ #
+ # The code migrations run through `scripts/migrate-code.ts`, the shell that
+ # reads the environment for them — `src/code-migrations/migrate.ts` only
+ # exports the runner (ADR-022 R7), and the `jiti` this line used to name is
+ # no longer a dependency of anything.
command: >
sh -c "
echo 'Running migrations...'
- cd /app/packages/db && ./node_modules/.bin/prisma migrate deploy
- cd /app/packages/core && ./node_modules/.bin/jiti src/code-migrations/migrate.ts
+ cd /app/packages/db && bunx [email protected] migrate deploy
+ cd /app/packages/core && bun scripts/migrate-code.ts
cd /app/apps/api && bun run start
"