Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
build(docker): slim the app images and cache dependency installs (#1649)
Sid66c8f161b21d
diff --git a/Dockerfile b/Dockerfileindex ede900acff..a6470aa96e 100644--- a/Dockerfile+++ b/Dockerfile@@ -1,39 +1,102 @@# syntax=docker/dockerfile:1# Shared build for React Router SSR apps. Build: docker build --build-arg APP=erp -t carbon/erp .ARG APP+# SOURCEMAPS=1 keeps node_modules sourcemaps for a debuggable image. Nothing+# reads them at runtime (no --enable-source-maps), so they go by default.+ARG SOURCEMAPS=0-FROM node:22 AS deps+# slim, not node:22 — every native dep ships prebuilt, nothing needs the toolchain.+FROM node:22-slim AS depsWORKDIR /repoRUN corepack enable+# Store on a cache mount, so a source-only commit relinks instead of refetching.+ENV npm_config_store_dir=/pnpm/storeCOPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc turbo.json lingui.config.js ./-COPY apps ./apps+# Only the apps this image can build — the rest would bust this layer for nothing.+COPY apps/erp ./apps/erp+COPY apps/mes ./apps/mesCOPY packages ./packagesCOPY patches ./patches-# scripts/ is needed by the postinstall (pnpm run generate:mcp → tsx-# scripts/generate-mcp.ts) and by the //#generate:mcp turbo task that build:${APP}-# depends on; without it `pnpm install` fails with ERR_MODULE_NOT_FOUND.+# Needed by the postinstall and the //#generate:mcp turbo task.COPY scripts ./scripts-RUN pnpm install --frozen-lockfile+RUN --mount=type=cache,id=pnpm-store,target=/pnpm/store,sharing=locked \+ pnpm install --frozen-lockfileFROM deps AS buildARG APPARG NODE_OPTIONS="--max-old-space-size=8024"ENV NODE_OPTIONS=${NODE_OPTIONS}-RUN pnpm run build:${APP}+RUN --mount=type=cache,id=turbo,target=/repo/.turbo,sharing=locked \+ pnpm run build:${APP}+# Build scratch `runner` must not inherit: .vite is the dep-optimizer cache,+# .ignored_<name> is pnpm's per-importer copy of a side-effects-cached package.+RUN rm -rf apps/${APP}/node_modules/.vite apps/${APP}/node_modules/.ignored_*# --- Ops image (DB migrations + first-boot seed) ---------------------------# The migrate Job (supabase migration up) and the seed Helm hook (tsx src/seed.ts)-# repurpose the app build to run one-off ops tasks. They need the supabase CLI-# and tsx/esbuild — exactly the build tooling the `runner` stage strips for its-# CVE posture. Rather than un-harden the served image, publish a separate-# un-stripped ops image from the `deps` stage for those short-lived Jobs. It is-# never exposed and is scanned report-only (it intentionally carries build-tool-# CVEs). migrate.yml and charts/apps seed-job point at carbon/ops:<same-tag>.-# Kept BEFORE `runner` so `runner` remains the default (no --target) build stage.-FROM deps AS ops+# The migrate/seed Jobs need the supabase CLI and tsx/esbuild — exactly what+# `runner` strips for its CVE posture — so they get their own never-exposed+# image, scanned report-only. Kept BEFORE `runner` so `runner` stays the+# default build stage. Pruned in a separate stage because a delete only+# reclaims space across a stage boundary.+FROM deps AS ops-pruned+ARG SOURCEMAPS+RUN find /repo -maxdepth 4 \( -name '.ignored_*' -o -name '.vite' \) \+ -prune -exec rm -rf {} + 2>/dev/null || true ; \+ find /repo/node_modules -type f \( -name '*.d.ts' -o -name '*.d.mts' \+ -o -name '*.d.cts' -o -name '*.md' \) -delete 2>/dev/null || true ; \+ if [ "${SOURCEMAPS}" != "1" ]; then \+ find /repo/node_modules -type f -name '*.map' -delete 2>/dev/null || true ; \+ fi++FROM node:22-slim AS ops+# slim ships no CA certs, and the supabase CLI is a Go binary that verifies TLS+# against the system store — migrations default to sslmode=require.+RUN apt-get update \+ && apt-get install -y --no-install-recommends ca-certificates \+ && rm -rf /var/lib/apt/lists/*+RUN corepack enable+# Pre-seeded corepack cache, so `pnpm exec` never dials npmjs from the migrate Job.+COPY --from=ops-pruned /root/.cache/node/corepack /root/.cache/node/corepack+COPY --from=ops-pruned /repo /repoWORKDIR /repo/packages/databaseCMD ["bash"]+# --- Runtime dependency tree ----------------------------------------------+# Runs in its own stage: done in `runner` after the COPY, a delete reclaims+# nothing. Strips build CLIs/binaries (the remaining Trivy CRITICAL/HIGHs; the+# `sst` JS package is kept, only its CLI binary goes; `tar`'s sole consumer is+# the supabase CLI and its fix is unpublished), packages the lockfile hydrates+# but nothing links (a frozen install materializes every importer, docs/+# included — hence next/@mui; react-icons is inlined via ssr.noExternal), and+# sourcemaps/.d.ts/readmes. Verify additions the same way — monaco-editor looks+# strippable but the server bundle imports @monaco-editor/react.+FROM deps AS pruned+ARG SOURCEMAPS+RUN find node_modules/.pnpm -maxdepth 1 -type d \( \+ -name 'sst-linux-*' -o -name 'sst-darwin-*' -o -name 'sst-win32-*' -o \+ -name 'esbuild@*' -o -name '@esbuild+*' -o \+ -name 'supabase@*' -o \+ -name 'tar@*' -o \+ -name 'npm@*' -o \+ -name '@typescript+native-preview-*' -o \+ -name '@biomejs+*' -o \+ -name 'turbo@*' -o -name 'turbo-linux-*' -o -name 'turbo-darwin-*' -o \+ -name '@turbo+*' -o \+ -name '@rolldown+binding-*' -o \+ -name 'vitest@*' -o -name '@vitest+*' -o \+ -name '@react-email+preview-server@*' -o \+ -name 'next@*' -o -name '@next+*' -o \+ -name '@mui+*' -o \+ -name 'react-icons@*' \+ \) -prune -exec rm -rf {} + ; \+ find node_modules -type d -name '@esbuild' -prune -exec rm -rf {} + 2>/dev/null || true ; \+ find packages -type d \( -name '.ignored_*' -o -name '.vite' \) -prune -exec rm -rf {} + 2>/dev/null || true ; \+ find node_modules -type f \( -name '*.d.ts' -o -name '*.d.mts' \+ -o -name '*.d.cts' -o -name '*.md' \) -delete 2>/dev/null || true ; \+ if [ "${SOURCEMAPS}" != "1" ]; then \+ find node_modules -type f -name '*.map' -delete 2>/dev/null || true ; \+ fi+FROM node:22-slim AS runnerARG APPWORKDIR /repo@@ -44,35 +107,11 @@ ENV PORT=3000# Date derivation assumes UTC until company/location timezones are threaded everywhereENV TZ=UTCCOPY --from=deps /repo/package.json /repo/pnpm-lock.yaml /repo/pnpm-workspace.yaml /repo/.npmrc ./-COPY --from=deps /repo/node_modules ./node_modules-COPY --from=deps /repo/packages ./packages+COPY --from=pruned /repo/node_modules ./node_modules+COPY --from=pruned /repo/packages ./packagesCOPY --from=build /repo/apps/${APP} ./apps/${APP}-# Strip build-time CLI tooling that the multi-stage copy drags into the runtime-# node_modules but the running server (react-router-serve) never executes: the-# prebuilt Go binaries inside sst's CLI, esbuild, the supabase CLI, and the-# typescript-go preview. They trail the current Go security release and are the-# only Trivy CRITICAL/HIGH left once the JS deps are patched, so removing them-# clears the findings at the source and shrinks the image. The `sst` JS package-# (import { Resource }) is kept — only its platform CLI binary is dropped.-# `tar` is stripped for the same reason: its sole consumer is the supabase CLI-# (removed just above), no app runtime imports it, and its GHSA-r292-9mhp-454m-# DoS fix ([email protected]) is not published to npm yet — so it cannot be pinned away-# and is instead removed from the image where the scanner sees it.-# npm itself is stripped for the same reason: the runtime uses corepack/pnpm and-# never invokes npm, but npm (pulled as a devDep via linguito, and shipped-# globally by the node base image) vendors its own copies of tar/pacote/sigstore/-# ip-address/etc. — including the only remaining Trivy CRITICALs (npm's bundled-# tar). Dropping npm clears those at the source and shrinks the image.-RUN find node_modules/.pnpm -maxdepth 1 -type d \( \- -name 'sst-linux-*' -o -name 'sst-darwin-*' -o -name 'sst-win32-*' -o \- -name 'esbuild@*' -o -name '@esbuild+*' -o \- -name 'supabase@*' -o \- -name 'tar@*' -o \- -name 'npm@*' -o \- -name '@typescript+native-preview-*' \- \) -prune -exec rm -rf {} + ; \- find node_modules -type d -name '@esbuild' -prune -exec rm -rf {} + 2>/dev/null || true ; \- rm -rf /usr/local/lib/node_modules/npm /usr/local/bin/npm /usr/local/bin/npx+# The base image's npm is unused (corepack/pnpm only) and vendors the last Trivy CRITICALs.+RUN rm -rf /usr/local/lib/node_modules/npm /usr/local/bin/npm /usr/local/bin/npxEXPOSE 3000WORKDIR /repo/apps/${APP}CMD ["pnpm","run","start"]