Senior LevelNode JS
How do you architect ultra-lean 50MB production Docker images for Node.js using multi-stage builds and distroless?
❓ Interview Prompt: Explain Docker Multi-Stage Builds, builder stage (compiling TS and native addons) vs runner stage (distroless/alpine), non-root user execution, and 50MB image sizing.
Production Node.js Docker images should be lean and secure:
Multi-Stage Architecture:
•
Stage 1 (Builder): Install ALL dependencies, compile TypeScript, build native C++ addons.
•
Stage 2 (Runner): Copy ONLY compiled 'dist/' and production 'node_modules' into a 'gcr.io/distroless/nodejs20' or 'node:20-alpine' base image running as a non-root user (Produces a tiny 50MB secure container!).
Code Implementation Example
bash
# Dockerfile snippet
FROM node:20-alpine AS builder
WORKDIR /app
COPY . .
RUN npm ci && npm run build
FROM gcr.io/distroless/nodejs20-debian11 AS runner
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
USER nonroot
CMD ["dist/server.js"]💡Multi-stage Dockerfile producing ultra-lean distroless Node.js image.
🎯 Interview Key Takeaway
Use Docker multi-stage builds with distroless or alpine runner stages to build 50MB production Node.js images.
#Docker#Multi-Stage#Distroless#DevOps#Senior
Back to all Node JS Questions→