# Production image for the Nuxt CV site.
#
# Shape: a runnable Node server (SSR). `nuxt build` emits a self-contained
# Nitro server (.output/server/index.mjs plus its traced dependencies and
# .output/public), so the runtime stage needs no dependency installation.
# This answers the open question in OKF_Nuxt/open-questions.md; the gateway
# proxies to this server instead of embedding static files (see
# OKF_Nuxt/decisions/production-image.md).
#
# Layer order follows OKF_Nuxt/decisions/build-layer-order.md: manifests
# before source, so dependency changes invalidate everything but source-only
# edits invalidate almost nothing.
#
# Base image is digest-pinned to the same node:26-slim digest as the dev
# container (OKF_Nuxt/decisions/dev-container-shape.md) - verified for the
# runner's arm64 and this workstation's x86_64.
# syntax: no BuildKit-only features used, so the default frontend suffices.

FROM node:26-slim@sha256:d62f13676d530f7edf97782729df69713c8ccfe6631e7f62f9ccab65968719a8 AS build

# pnpm pinned to the same version as CI (pr-checker.yml pins major 11) and
# the local toolchain (11.26.0). Installed via npm from the node image - no
# corepack, which current node images no longer bundle.
RUN npm install -g pnpm@11.26.0

WORKDIR /app

# Manifests first: their layer only invalidates when dependencies change.
# --ignore-scripts skips the `nuxt prepare` postinstall (the source is not
# copied yet); `nuxt build` prepares what it needs itself. Native binaries
# (esbuild, unrs-resolver) come from platform-specific optional packages,
# not install scripts, so skipping scripts is safe here.
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
RUN pnpm install --frozen-lockfile --ignore-scripts

# Source last: the expensive layers above stay cached on source-only edits.
COPY . .
RUN pnpm build


FROM node:26-slim@sha256:d62f13676d530f7edf97782729df69713c8ccfe6631e7f62f9ccab65968719a8

# pnpm is NOT installed in the runtime image: nothing here runs pnpm.
ENV NODE_ENV=production \
    HOST=0.0.0.0 \
    PORT=3000

WORKDIR /app

# Nitro's traced output includes .output/server/node_modules - the only
# thing the server needs. Runs as the image's non-root `node` user.
COPY --from=build --chown=node:node /app/.output ./.output

USER node

EXPOSE 3000

# node has no curl/wget in slim; use node's own fetch. The root path is
# served by the app itself, so a 2xx means the server is genuinely up.
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
  CMD node -e "fetch('http://127.0.0.1:3000/').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"

CMD ["node", ".output/server/index.mjs"]
