Runtime images: install production dependencies only #250

Closed
opened 2026-10-11 13:04:22 +00:00 by ullrich · 1 comment
Owner

Problem

The journal and planner runtime images install all dependencies, including dev dependencies. Each image is about 1.7 GB. The runtime images contain build tools that the apps do not use at runtime (Vite, React Router dev, Tailwind, Vitest, and others).

Two side effects:

  • Image scan noise. The images contain two esbuild binaries. They give 146 Go standard-library advisories per image in the image scan (#249). The dashboard ignores these advisories now (IGNORED_IN_IMAGES in .forgejo/scripts/vuln-dashboard.py).
  • Undeclared imports. App code imports packages that only the root package.json declares, as dev dependencies. These imports work because Node searches up to /app/node_modules. An image with production dependencies only crashes at startup.

Found on 2026-10-11:

Package Import Declared in
apps/journal postgres (server.ts), fit-file-parser, react-i18next, leaflet, react-leaflet root devDependencies only
apps/planner postgres (server.ts), i18next, react-i18next, leaflet, react-leaflet root devDependencies only
apps/planner leaflet.markercluster planner devDependencies
packages/i18n i18next-browser-languagedetector root devDependencies only

Constraint: drizzle-kit in the journal image

cd-apps.yml and cd-staging.yml run npx drizzle-kit push inside the journal image. So the journal image must keep drizzle-kit, and with it both esbuild binaries. packages/db/drizzle.config.ts imports drizzle-kit as a value, and that import resolves from packages/db/.

Plan

  1. Declare every runtime import in dependencies of the package that imports it.
  2. Add import-x/no-extraneous-dependencies to eslint.config.js for app and package source, so that new undeclared imports fail lint.
  3. Make drizzle-kit a dependency of apps/journal. Change drizzle.config.ts to a type-only import, so that the config does not resolve drizzle-kit from packages/db/.
  4. Add a prod-deps stage (pnpm install --frozen-lockfile --prod) to both Dockerfiles, and copy the runtime node_modules from that stage.
  5. Smoke test: run drizzle-kit push and migrate-data.ts in the journal image, start both apps against a test database, and request their health and main pages.

Later, optionally: move drizzle-kit to a separate migration image. Then the journal image also loses esbuild, and the IGNORED_IN_IMAGES filter can go.

Status

Work is in progress on branch prod-only-images.

## Problem The journal and planner runtime images install **all** dependencies, including dev dependencies. Each image is about 1.7 GB. The runtime images contain build tools that the apps do not use at runtime (Vite, React Router dev, Tailwind, Vitest, and others). Two side effects: - **Image scan noise.** The images contain two `esbuild` binaries. They give 146 Go standard-library advisories per image in the image scan (#249). The dashboard ignores these advisories now (`IGNORED_IN_IMAGES` in `.forgejo/scripts/vuln-dashboard.py`). - **Undeclared imports.** App code imports packages that only the root `package.json` declares, as dev dependencies. These imports work because Node searches up to `/app/node_modules`. An image with production dependencies only crashes at startup. Found on 2026-10-11: | Package | Import | Declared in | |---|---|---| | `apps/journal` | `postgres` (`server.ts`), `fit-file-parser`, `react-i18next`, `leaflet`, `react-leaflet` | root devDependencies only | | `apps/planner` | `postgres` (`server.ts`), `i18next`, `react-i18next`, `leaflet`, `react-leaflet` | root devDependencies only | | `apps/planner` | `leaflet.markercluster` | planner devDependencies | | `packages/i18n` | `i18next-browser-languagedetector` | root devDependencies only | ## Constraint: `drizzle-kit` in the journal image `cd-apps.yml` and `cd-staging.yml` run `npx drizzle-kit push` inside the journal image. So the journal image must keep `drizzle-kit`, and with it both `esbuild` binaries. `packages/db/drizzle.config.ts` imports `drizzle-kit` as a value, and that import resolves from `packages/db/`. ## Plan 1. Declare every runtime import in `dependencies` of the package that imports it. 2. Add `import-x/no-extraneous-dependencies` to `eslint.config.js` for app and package source, so that new undeclared imports fail lint. 3. Make `drizzle-kit` a dependency of `apps/journal`. Change `drizzle.config.ts` to a type-only import, so that the config does not resolve `drizzle-kit` from `packages/db/`. 4. Add a `prod-deps` stage (`pnpm install --frozen-lockfile --prod`) to both Dockerfiles, and copy the runtime `node_modules` from that stage. 5. Smoke test: run `drizzle-kit push` and `migrate-data.ts` in the journal image, start both apps against a test database, and request their health and main pages. Later, optionally: move `drizzle-kit` to a separate migration image. Then the journal image also loses `esbuild`, and the `IGNORED_IN_IMAGES` filter can go. ## Status Work is in progress on branch `prod-only-images`.
Author
Owner

Implemented in #251. During the tests I found two more things:

  1. Dependencies of bundled workspace packages. Vite bundles @trails-cool/i18n and @trails-cool/gpx into each app's server build, but leaves their dependencies as external imports. The app must declare them too. Lint cannot see this, so scripts/check-server-externals.mjs now checks it in the Docker build.
  2. Follow-up: React Native in the production images. Even with production dependencies only, the images are about 1.4 GB. About 70 React Native and Expo packages (expo-sqlite, react-native, hermes-compiler, …) get installed. pnpm resolves drizzle-orm (optional peer expo-sqlite) and react-i18next (optional peer react-native) with the mobile app's peers across the whole workspace. Possible fixes: separate peer resolution for apps/mobile, or dedupe-peer-dependents=false. This needs its own investigation.
Implemented in #251. During the tests I found two more things: 1. **Dependencies of bundled workspace packages.** Vite bundles `@trails-cool/i18n` and `@trails-cool/gpx` into each app's server build, but leaves *their* dependencies as external imports. The app must declare them too. Lint cannot see this, so `scripts/check-server-externals.mjs` now checks it in the Docker build. 2. **Follow-up: React Native in the production images.** Even with production dependencies only, the images are about 1.4 GB. About 70 React Native and Expo packages (`expo-sqlite`, `react-native`, `hermes-compiler`, …) get installed. pnpm resolves `drizzle-orm` (optional peer `expo-sqlite`) and `react-i18next` (optional peer `react-native`) with the mobile app's peers across the whole workspace. Possible fixes: separate peer resolution for `apps/mobile`, or `dedupe-peer-dependents=false`. This needs its own investigation.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
trails-cool/trails#250
No description provided.