Add a Trivy job to the Docker build workflow that scans the built image for CRITICAL/HIGH CVEs, uploads SARIF to GitHub code scanning, and fails only on CRITICAL findings so a solo maintainer isn't blocked by HIGH-only noise. Split top-level workflow permissions down to contents:read and elevate packages:write / security-events:write per job instead of at the workflow level. Strip npm/npx/yarn/corepack from the Docker runtime stage since they are never invoked at runtime (node server.js only) and were the source of the only CRITICAL/HIGH findings in the current image. Co-authored-by: James Maes <jmaes@dmdbrands.com>
49 lines
1.3 KiB
Docker
49 lines
1.3 KiB
Docker
# Stage 1: Install dependencies
|
|
FROM node:22-alpine AS deps
|
|
RUN apk add --no-cache libc6-compat
|
|
WORKDIR /app
|
|
|
|
COPY package.json package-lock.json ./
|
|
RUN npm ci
|
|
|
|
# Stage 2: Rebuild source code
|
|
FROM node:22-alpine AS builder
|
|
WORKDIR /app
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
COPY . .
|
|
|
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
ENV NODE_ENV=production
|
|
|
|
RUN npm run build
|
|
|
|
# Stage 3: Production runner
|
|
FROM node:22-alpine AS runner
|
|
WORKDIR /app
|
|
|
|
ENV NODE_ENV=production
|
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
ENV PORT=3000
|
|
ENV HOSTNAME="0.0.0.0"
|
|
|
|
RUN addgroup --system --gid 1001 nodejs && \
|
|
adduser --system --uid 1001 nextjs
|
|
|
|
# Strip build-time package-manager tooling inherited from the base image.
|
|
# The runtime entrypoint is `node server.js` only; npm/npx/yarn/corepack are
|
|
# never invoked here, and removing them drops unused CVE surface (e.g. the
|
|
# tar version bundled with npm) from the shipped image.
|
|
RUN rm -rf /usr/local/lib/node_modules/npm /usr/local/lib/node_modules/corepack \
|
|
/usr/local/bin/npm /usr/local/bin/npx /usr/local/bin/corepack \
|
|
/usr/local/bin/yarn /usr/local/bin/yarnpkg
|
|
|
|
COPY --from=builder /app/public ./public
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
|
|
|
USER nextjs
|
|
|
|
EXPOSE 3000
|
|
|
|
CMD ["node", "server.js"]
|