# One build argument selects both the bundle payload and the runtime mode. P6 # enables the Web UI by changing this default to "required"; the matching stage # builds the Vite artifact and the runner then fails startup if it is invalid. ARG GATEWAY_WEB_UI_MODE=disabled FROM node:22-alpine AS base ENV PNPM_HOME="/pnpm" ENV PATH="$PNPM_HOME:$PATH" RUN corepack enable FROM base AS builder WORKDIR /app # Copy workspace manifests first for layer-cached install COPY pnpm-workspace.yaml pnpm-lock.yaml package.json ./ COPY apps/gateway/package.json ./apps/gateway/ COPY packages/ ./packages/ COPY plugins/ ./plugins/ # the root prepare script runs scripts/install-hooks.mjs on install COPY scripts/ ./scripts/ RUN pnpm install --frozen-lockfile COPY . . # Build gateway and all of its workspace dependencies via turbo dependency graph RUN pnpm turbo run build --filter @mosaicstack/gateway... # Produce a self-contained deploy artifact: flat node_modules, no pnpm symlinks # --legacy is required for pnpm v10 when inject-workspace-packages is not set RUN pnpm --filter @mosaicstack/gateway --prod deploy --legacy /deploy FROM builder AS web-ui-disabled RUN mkdir -p /web-ui FROM builder AS web-ui-required RUN pnpm install --frozen-lockfile \ && pnpm --filter @mosaicstack/web build:vite \ && mkdir -p /web-ui \ && cp -R /app/apps/web/dist/. /web-ui/ FROM web-ui-${GATEWAY_WEB_UI_MODE} AS web-ui FROM base AS runner ARG GATEWAY_WEB_UI_MODE WORKDIR /app ENV NODE_ENV=production ENV GATEWAY_WEB_UI_MODE=${GATEWAY_WEB_UI_MODE} # WorkspaceService shells out to git at runtime and roots workspaces at # $MOSAIC_ROOT/.workspaces (apps/gateway/src/workspace/workspace.service.ts); # mount a volume over /opt/mosaic to persist workspaces across container restarts. # Intentionally unpinned: Alpine's signed repository is the trust anchor; pinning # git was declined so routine base-image security updates remain maintainable. RUN apk add --no-cache git \ && mkdir -p /opt/mosaic/.workspaces \ && chown -R node:node /opt/mosaic /app ENV MOSAIC_ROOT=/opt/mosaic # Use the pnpm deploy output — resolves all deps into a flat, self-contained node_modules COPY --chown=node:node --from=builder /deploy/node_modules ./node_modules COPY --chown=node:node --from=builder /deploy/package.json ./package.json # dist is declared in package.json "files" so pnpm deploy copies it into /deploy; # copy from builder explicitly as belt-and-suspenders COPY --chown=node:node --from=builder /app/apps/gateway/dist ./dist # resolveWebDistPath() maps runner /app/dist/web-ui.js to /web/dist. COPY --chown=node:node --from=web-ui /web-ui /web/dist # gateway defaults to port 14242 (apps/gateway/src/main.ts) EXPOSE 14242 USER node CMD ["node", "dist/main.js"]