Two runtime defects in the crm-app prod image (never exercised before this
deploy; CI only builds + pushes):
1. Replacing the standalone node_modules wholesale to add socket.io's deps
swapped out Next's standalone-tuned `next` and broke its runtime
("Invariant: AsyncLocalStorage accessed in runtime where it is not
available"). Instead, stage the complete hoisted prod tree in a separate
dir on NODE_PATH: the standalone node_modules (and its `next`) stay
intact, and only the socket server's otherwise-missing deps
(engine.io→accepts/ws/cors, @socket.io/redis-adapter) fall through to it.
2. Defensively set globalThis.AsyncLocalStorage before Next's app-render
modules load, via a preamble that is the first import in server.ts.
Next's node-environment-baseline normally sets it during the standalone
bootstrap, but the custom server can load app-render storage first.
Verified in the esbuild bundle that the assignment runs before
require("next").
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
64 lines
3.0 KiB
Docker
64 lines
3.0 KiB
Docker
# Stage 1: Install dependencies
|
|
FROM node:20-alpine AS deps
|
|
RUN corepack enable && corepack prepare pnpm@10.33.2 --activate
|
|
WORKDIR /app
|
|
COPY package.json pnpm-lock.yaml ./
|
|
RUN pnpm install --frozen-lockfile --prod=false
|
|
|
|
# Stage 1b: Production dependency tree in a flat (hoisted) node_modules.
|
|
# Hoisted = symlink-free, so a Docker COPY into the runner is faithful
|
|
# (copying pnpm's default symlinked layout dereferences and breaks
|
|
# transitive resolution); complete = the custom socket.io server's deps
|
|
# (engine.io, accepts, ws, ...) all resolve at runtime.
|
|
FROM node:20-alpine AS prod-deps
|
|
RUN corepack enable && corepack prepare pnpm@10.33.2 --activate
|
|
WORKDIR /app
|
|
COPY package.json pnpm-lock.yaml ./
|
|
RUN echo "node-linker=hoisted" > .npmrc && pnpm install --frozen-lockfile --prod
|
|
|
|
# Stage 2: Build the application
|
|
FROM node:20-alpine AS builder
|
|
RUN corepack enable && corepack prepare pnpm@10.33.2 --activate
|
|
WORKDIR /app
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
COPY . .
|
|
# NODE_ENV=production in the builder makes `next build` and any code
|
|
# branching on isProd deterministic (build-auditor M9). Without this,
|
|
# CSP and other prod-only paths would compile under whatever NODE_ENV
|
|
# the host carried in.
|
|
ENV NODE_ENV=production
|
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
ENV SKIP_ENV_VALIDATION=1
|
|
RUN pnpm build
|
|
|
|
# Stage 3: Production runner
|
|
FROM node:20-alpine AS runner
|
|
RUN addgroup --system --gid 1001 nodejs && adduser --system --uid 1001 nextjs
|
|
WORKDIR /app
|
|
ENV NODE_ENV=production
|
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
|
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
|
|
COPY --from=builder --chown=nextjs:nodejs /app/dist/server.js ./server-custom.js
|
|
# The custom socket.io server (server-custom.js, built with esbuild
|
|
# --packages=external) resolves socket.io and its FULL transitive closure
|
|
# (engine.io → accepts/ws/cors, @socket.io/redis-adapter, ...) from
|
|
# node_modules at runtime. The Next tracer omits these from
|
|
# .next/standalone because no Next route imports the socket server
|
|
# (→ MODULE_NOT_FOUND 'accepts'). Stage the complete hoisted prod tree in
|
|
# a SEPARATE dir on NODE_PATH rather than touching the standalone
|
|
# node_modules: overlaying real dirs onto its pnpm symlinks (e.g.
|
|
# @react-pdf/renderer) fails the COPY, and replacing it wholesale swaps
|
|
# out the standalone-tuned `next` and breaks Next's runtime
|
|
# (AsyncLocalStorage invariant). NODE_PATH is searched AFTER the local
|
|
# walk, so Next still resolves its own deps from ./node_modules; only the
|
|
# socket server's otherwise-missing deps fall through to here.
|
|
COPY --from=prod-deps --chown=nextjs:nodejs /app/node_modules ./_server_deps
|
|
ENV NODE_PATH=/app/_server_deps
|
|
USER nextjs
|
|
EXPOSE 3000
|
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
|
|
CMD wget --no-verbose --tries=1 --spider http://localhost:${PORT:-3000}/api/health || exit 1
|
|
CMD ["node", "server-custom.js"]
|