# One reviewed Node runtime for build, web, migration, import, and extraction jobs.
FROM node:22-alpine@sha256:12f54cafa58d0e96554ae05871e29663ff7241836377dc9a7916449b8adb062c AS build
WORKDIR /app
COPY package.json package-lock.json ./
COPY prisma ./prisma
RUN npm ci
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN npm run build

FROM node:22-alpine@sha256:12f54cafa58d0e96554ae05871e29663ff7241836377dc9a7916449b8adb062c AS runtime
# The finite release Job uses psql to provision only the new application database.
RUN apk add --no-cache postgresql-client
WORKDIR /app
# Keep Prisma's CLI and the complete PDF/OCR packages. The compiled worker
# imports their fonts, language data, native code, and WASM files at runtime.
COPY --from=build --chown=node:node /app /app
ENV NODE_ENV=production \
    NEXT_TELEMETRY_DISABLED=1 \
    PORT=3000
USER node
EXPOSE 3000 8080
# The same image serves Web or the retained API endpoint, each on its declared port.
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 CMD ["node", "--input-type=module", "-e", "const port = process.env.PORT || '3000'; const path = port === '8080' ? '/health' : '/api/health'; try { const response = await fetch('http://127.0.0.1:' + port + path, { signal: AbortSignal.timeout(4000) }); process.exit(response.ok ? 0 : 1); } catch { process.exit(1); }"]
# Migrations are a separate finite release Job; replicas never migrate on startup.
CMD ["node", "node_modules/next/dist/bin/next", "start", "--hostname", "0.0.0.0", "--port", "3000"]
