Multi-Stage Production Node.js Dockerfile with Distroless
# Stage 1: Dependency resolution and compilation
FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm \
npm ci --omit=dev --ignore-scripts
# Stage 2: Application builder (if TypeScript/bundler required)
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# RUN npm run build
# Stage 3: Ultra-minimal hardened runtime
FROM gcr.io/distroless/nodejs20-debian12 AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app ./
USER nonroot
EXPOSE 3000
CMD ["server.js"]