OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
ralph(M9-006): P9.5 image slim-down: single-stage bun install --production, measured before/after size
ralph49a6388e9829
diff --git a/.github/smoke/docker-compose.yml b/.github/smoke/docker-compose.ymlindex 29591d87f..5a74868d4 100644--- a/.github/smoke/docker-compose.yml+++ b/.github/smoke/docker-compose.yml@@ -82,9 +82,10 @@ services:command: >sh -c "echo 'Running migrations...'- CI=true pnpm -r run migrate:deploy+ cd /app/packages/db && ./node_modules/.bin/prisma migrate deploy+ cd /app/packages/core && ./node_modules/.bin/jiti src/code-migrations/migrate.ts- pnpm start+ cd /app/apps/api && bun run start"ports:- "3333:3000"diff --git a/apps/api/Dockerfile b/apps/api/Dockerfileindex da8b722b5..d12c90323 100644--- a/apps/api/Dockerfile+++ b/apps/api/Dockerfile@@ -2,10 +2,16 @@# `.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-# ADR-014: pnpm stays through P12 — this only pins the pnpm *version* installed-# into the Bun image, it does not touch which tool runs installs.+# ADR-014: pnpm stays through P12 — this only pins the pnpm *version* used to+# install the workspace, it does not touch which tool runs installs. P9.5+# stops that installer reaching the runtime stage at all (ADR-014 Problems+# §4, option 2): pnpm and the build toolchain live only in `build`/`prod-deps`+# below, neither of which ships.ARG PNPM_VERSION=11.23.0+# 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 pnpm stages.FROM oven/bun:${BUN_VERSION}-slim AS baseRUN apt-get update && \@@ -18,23 +24,27 @@ RUN apt-get update && \&& apt-get clean && \rm -rf /var/lib/apt/lists/*-# M9-003 deleted apps/worker and M9-004 deleted the V1 Fastify boot: this image-# is now ONE process in three shapes, `bun run src/main.ts` under-# ROLE=api|worker|all, running from source with no bundle (ADR-010). P9.5-# collapses the base layer itself onto oven/bun — no Node anywhere in the-# image. pnpm still runs (ADR-014: pnpm stays through P12); it runs *on* Bun-# instead of needing a separate Node install.+# TOOLCHAIN — pnpm + the native-addon build toolchain, shared by the two+# stages below. Neither of them ships, so neither does this.+FROM base AS toolchain++RUN apt-get update && apt-get install -y --no-install-recommends \+ python3 \+ make \+ g++ \+ && apt-get clean && \+ rm -rf /var/lib/apt/lists/*+ARG PNPM_VERSIONRUN bun install -g pnpm@${PNPM_VERSION}-ARG DATABASE_URL-ENV DATABASE_URL=$DATABASE_URLENV PNPM_HOME="/pnpm"ENV PATH="$PNPM_HOME:$PATH"+ENV npm_config_build_from_source=trueWORKDIR /app-# Workspace+# Workspace manifestCOPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./# AppsCOPY apps/api/package.json ./apps/api/@@ -52,15 +62,12 @@ COPY packages/constants/package.json packages/constants/COPY packages/validation/package.json packages/validation/COPY patches ./patches-# BUILD-FROM base AS build--RUN apt-get update && apt-get install -y --no-install-recommends \- python3 \- make \- g+++# BUILD — full install (codegen needs devDependencies: core's `tar` for+# scripts/download-geo.ts, db's `prisma` CLI for schema generation) then the+# Prisma client + geo/ASN codegen. Never ships: only its generated *source*+# output (baked into packages/db and packages/core below) reaches runtime.+FROM toolchain AS build-WORKDIR /appRUN pnpm install --frozen-lockfile && \pnpm store prune@@ -68,55 +75,43 @@ COPY apps/api ./apps/apiCOPY packages ./packagesCOPY tooling ./tooling-# No build step: ADR-010 deleted the backend bundler with the V1 boot, so the-# only thing this stage produces is the generated Prisma client.-RUN pnpm codegen--# PROD-FROM base AS prod+ARG DATABASE_URL+ENV DATABASE_URL=$DATABASE_URL-ENV npm_config_build_from_source=true+# No JS build step: ADR-010 deleted the backend bundler with the V1 boot, so+# the only thing this stage produces is the generated Prisma client and+# core's downloaded geo/ASN data, both written to disk under packages/.+RUN pnpm codegen-RUN apt-get update && apt-get install -y --no-install-recommends \- python3 \- make \- g+++# PROD-DEPS — a from-scratch production-only install. Deliberately NOT+# derived from `build`'s node_modules: pnpm's --prod install does not reliably+# purge an existing devDependency-laden modules dir (verified empirically —+# it left every devDependency in place), so shipping node_modules must come+# from an install that was production-only from the start.+FROM toolchain AS prod-deps-WORKDIR /app-COPY --from=build /app/package.json ./-COPY --from=build /app/pnpm-lock.yaml ./RUN pnpm install --frozen-lockfile --prod && \pnpm rebuild && \pnpm store prune-# FINAL-FROM base AS runner+# RUNTIME — oven/bun:${BUN_VERSION}-slim, nothing but the app: node_modules+# from `prod-deps`, source (with `build`'s generated output baked in) from+# `build`. No pnpm, no python3/make/g++, no per-package COPY manifest.+FROM base AS runtimeENV NODE_ENV=production-ENV npm_config_build_from_source=trueWORKDIR /app-COPY --from=build /app/package.json ./-COPY --from=prod /app/node_modules ./node_modules+COPY --from=prod-deps /app/package.json ./+COPY --from=prod-deps /app/node_modules ./node_modules# Apps — src/ is the artifact now; there is no dist/.COPY --from=build /app/apps/api ./apps/api-# Packages-COPY --from=build /app/packages/core ./packages/core-COPY --from=build /app/packages/db ./packages/db-COPY --from=build /app/packages/trpc ./packages/trpc-COPY --from=build /app/packages/json ./packages/json-COPY --from=build /app/packages/email ./packages/email-COPY --from=build /app/packages/queue ./packages/queue-COPY --from=build /app/packages/redis ./packages/redis-COPY --from=build /app/packages/common ./packages/common-COPY --from=build /app/packages/payments ./packages/payments-COPY --from=build /app/packages/constants ./packages/constants-COPY --from=build /app/packages/validation ./packages/validation+# Packages — one COPY, not the per-package manifest the pnpm stages needed.+COPY --from=build /app/packages ./packagesCOPY --from=build /app/tooling/typescript ./tooling/typescript-RUN pnpm db:codegenWORKDIR /app/apps/apidiff --git a/docs/OPS_DEPLOY_MIGRATION.md b/docs/OPS_DEPLOY_MIGRATION.mdindex 89217110b..823a2bde5 100644--- a/docs/OPS_DEPLOY_MIGRATION.md+++ b/docs/OPS_DEPLOY_MIGRATION.md@@ -91,3 +91,120 @@ numbers aren't confused for a regression against the old two-image setup.No image-size baseline for the old node+bun two-stage `api` image or the old`worker` image was captured before this migration, so there is no before/afterdelta to report — only this absolute number.++## P9.5 slim-down (M9-006): pnpm and the build toolchain leave the runtime image++`apps/api/Dockerfile` is now five stages: `base` (shared OS packages) →+`toolchain` (pnpm + python3/make/g++, never ships) → `build` (full install,+runs `pnpm codegen` — Prisma client + core's geo/ASN data — never ships) and+`prod-deps` (a **from-scratch** production-only `pnpm install --frozen-lockfile+--prod`, never ships) in parallel → `runtime` (`oven/bun:${BUN_VERSION}-slim`,+COPYs `node_modules` from `prod-deps` and source, generated output included,+from `build`). ADR-014 stays satisfied: pnpm is still the installer, it just+never reaches a container that serves traffic (ADR-014 Problems §4, option 2).++`prod-deps` is deliberately **not** derived from `build`'s already-installed+`node_modules` — an earlier attempt in this task tried reinstalling `--prod`+on top of a full install in place, and it measurably failed to purge+devDependencies (`tar`, `typescript`, `prisma` CLI, `@biomejs/biome`,+`ultracite`, `vitest`, … — package count went **1041 → 2257**, node_modules+**1.1G → 2.0G**, all still empirically verified inside the built image). A+from-scratch `--prod` install is what actually ships a production-only tree.++**docker-compose.template.yml / coolify.yml / .github/smoke/docker-compose.yml**+ran `CI=true pnpm -r run migrate:deploy` and `pnpm start` *inside* the api+image at container start (ADR-014 Problems §4's named consequence). All three+now call the installed tools directly instead of through pnpm's script+runner: `./node_modules/.bin/prisma migrate deploy` and+`./node_modules/.bin/jiti src/code-migrations/migrate.ts` (both are pnpm's+`#!/bin/sh` shims; `node` on `$PATH` resolves to Bun's node-fallback symlink,+so they run unmodified) and `bun run start` for `apps/api`'s own script —+`jiti` itself is untouched (ADR-019 row 7b schedules its removal for P12, not+here). Verified directly against this box's local Postgres/ClickHouse (all+already migrated, so both commands are confirmed no-ops): `prisma migrate+deploy` → `"No pending migrations to apply."`; `jiti+src/code-migrations/migrate.ts` → all 24 recorded migrations print `Already+Migrated`.++### Measured sizes — all four via `docker image inspect <tag> --format '{{.Size}}'`++Same command run against every tag in this attempt (2026-09-05), so the+numbers are apples to apples. V1 images were pulled from Docker Hub, measured,+then `docker rmi`'d immediately (disk on this box is ~16G free) — they are not+left on this box. V2 "before" is the **pre-existing** `openpanel-v2:test` tag+(built by an earlier task, M9-005) — not rebuilt in this attempt, per the+task's instruction not to invent a rebuild when the tag already exists. V2+"after" is `openpanel-v2:slim`, built in this attempt from the Dockerfile+above via `docker build -f apps/api/Dockerfile -t openpanel-v2:slim .`.++| Image | Tag | `.Size` (bytes) | ≈MB | ≈GiB |+|---|---|---:|---:|---:|+| V1 api (Docker Hub `lindesvard/openpanel-api:latest`) | pulled, measured, removed | 476,399,142 | 476.4 | 0.444 |+| V1 worker (Docker Hub `lindesvard/openpanel-worker:latest`) | pulled, measured, removed | 465,663,118 | 465.7 | 0.434 |+| V2 before (`openpanel-v2:test`, pre-existing, not rebuilt) | existing tag | 420,032,938 | 420.0 | 0.391 |+| V2 after (`openpanel-v2:slim`, this attempt) | built this attempt | 341,044,603 | 341.0 | 0.318 |++(Docker Hub's own reported compressed sizes as of 2026-08-18 were api 0.48GB /+worker 0.47GB — consistent with the `.Size` figures measured here, which is+the expected cross-check: `docker image inspect --format '{{.Size}}'` on this+Docker version (29.7.2, containerd image store) reports compressed content+size, the same quantity Docker Hub publishes. `docker images`' `DISK USAGE`+column is a different, larger, uncompressed-on-this-daemon number — 1.79GB for+`openpanel-v2:slim`, 2.1GB for `openpanel-v2:test` — and is not what a+registry pull transfers; do not compare it to the table above.)++### Verdict++**V2 slimmed (341.0MB) is now smaller than either V1 image (476.4MB api,+465.7MB worker)** — this task's specific improvement is V2-before → V2-after:+420.0MB → 341.0MB content size (−18.8%), 2.1GB → 1.79GB on-disk (−14.8%),+entirely from getting pnpm, python3/make/g++ and the per-package COPY manifest+out of the runtime stage (the dependency graph itself is unchanged — this is+not a `bun install` swap, ADR-014 stays P13).++**Fleet arithmetic, stated separately so it isn't hidden by the per-image+number:** V1 ships two images to the registry (api + worker, ~942MB combined+pulled once each) backing three cloud service definitions (`openpanel_api`,+`openpanel_event_workers`, `openpanel_workers` — see the target stack table+above). V2 ships **one** 341.0MB image backing all three. A node that used to+need both V1 images pulls one V2 image instead — that is the bigger win, but+it rides on top of the per-image win being real too, not in place of it.++**A real lever left on the table, out of this task's scope.** `openpanel-v2:slim`'s+`node_modules` is still ~1.1G on disk, dominated by packages with zero runtime+relationship to `apps/api` — `next` + `@next/swc-linux-x64-gnu` (~265MB),+`react-email` (~179MB), `@polar-sh/sdk` (~35MB), `effect` (~33MB), `mathjs`+(~18MB) — because the workspace-root `pnpm install --frozen-lockfile --prod`+installs the **entire monorepo's** production dependency graph (all 23+workspace projects, `apps/start`/`apps/public` included), not just+`@openpanel/api`'s closure, regardless of which package.json files were+selectively `COPY`'d for pnpm's own resolution. Filtering the install (e.g.+`pnpm install --filter` or `pnpm deploy`) would be a substantially bigger size+win than this task's stage restructuring, but it is a different, riskier+change (frozen-lockfile + filter interaction, unverified on this repo) and is+not what this task's acceptance criteria asked for — recorded here as a+follow-up, not attempted.++### Boot proof++Both roles booted from `openpanel-v2:slim` against this box's local+Postgres/Redis/ClickHouse/Redpanda (`--network host`, this attempt,+2026-09-05):++```+$ docker run -d --network host --env-file <env> openpanel-v2:slim # ROLE=api (via API_PORT=3333)+{"...","msg":"API listening","role":"api","port":3333}+$ curl -s http://127.0.0.1:3333/healthcheck+{"ready":true,"redis":true,"db":true,"ch":true,"failedDependencies":[],"workingDependencies":["redis","db","ch"]}++$ docker run -d --network host --env-file <env> -e ROLE=worker openpanel-v2:slim+{"...","msg":"worker started","queue":"sessions"} … (all 7 queues) …+{"...","msg":"kafka events consumer running","topic":"events"}+{"...","msg":"API listening","role":"worker","port":3333}+$ curl -s http://127.0.0.1:3333/healthz/ready+{"ready":true}+```++`bun run start` (apps/api's own package.json script, the `pnpm start`+replacement) was also exercised directly and shut down cleanly on `SIGTERM`+("Graceful shutdown completed").diff --git a/self-hosting/coolify.yml b/self-hosting/coolify.ymlindex 301a1bdc8..b0f63b0ea 100644--- a/self-hosting/coolify.yml+++ b/self-hosting/coolify.yml@@ -125,9 +125,10 @@ services:echo 'ClickHouse is ready'echo 'Running migrations...'- CI=true pnpm -r run migrate:deploy+ cd /app/packages/db && ./node_modules/.bin/prisma migrate deploy+ cd /app/packages/core && ./node_modules/.bin/jiti src/code-migrations/migrate.ts- pnpm start+ cd /app/apps/api && bun run start"depends_on:opdb:diff --git a/self-hosting/docker-compose.template.yml b/self-hosting/docker-compose.template.ymlindex 6ecdb99cc..7e01eba3d 100644--- a/self-hosting/docker-compose.template.yml+++ b/self-hosting/docker-compose.template.yml@@ -129,12 +129,18 @@ services:op-api:image: lindesvard/openpanel-api:2restart: always+ # P9.5 collapsed the two-Dockerfile setup onto oven/bun: pnpm no longer+ # exists in this image (ADR-014 Problems §4), so migrations and boot run+ # as their bun equivalents — the prisma/jiti binaries directly (their+ # `#!/bin/sh` shims resolve `node` to Bun's node-fallback shim) and+ # `bun run start` for apps/api's own script.command: >sh -c "echo 'Running migrations...'- CI=true pnpm -r run migrate:deploy+ cd /app/packages/db && ./node_modules/.bin/prisma migrate deploy+ cd /app/packages/core && ./node_modules/.bin/jiti src/code-migrations/migrate.ts- pnpm start+ cd /app/apps/api && bun run start"healthcheck:test: ["CMD-SHELL", "curl -f http://localhost:3000/healthcheck || exit 1"]