Dockerfile Multi-Stage Build Optimization (Sub-50MB Final Image)
30 Production Challenge Scenarios • Real-Time Automated Quality Scoring

Interactive Prompt Engineering Lab

Test and score your prompts against real-world enterprise scenarios. Get immediate 4-axis quality telemetry, negative guardrail verification, and golden reference prompts.

Read Syllabus
1. Authoritative Persona
Expert role + zero fluff
25 pts
2. Deterministic Schema
XML tags + JSON / keys
25 pts
3. Negative Guardrails
NEVER / fallback rules
25 pts
4. Token Efficiency
150–1,800 chars concise
25 pts
Web & System ArchitectureIntermediate

Dockerfile Multi-Stage Build Optimization (Sub-50MB Final Image)

Refactor a bloated 1.2GB Next.js + Node.js Docker container into a hardened, non-root, sub-50MB production container using Alpine Linux and multi-stage builds.

Target Input Context / Untrusted Data:
Current Container: 1.2GB (contains full git, build-essential, npm cache, source code, devDependencies)
Application: Next.js 15 Standalone output
Security: Container currently runs as root (UID 0), failing CIS Docker Benchmarks.
Quality Requirements & Guardrails:
Define a Principal DevSecOps & Container Architect persona.
Implement 3-stage build: base -> builder -> runner.
Use node:22-alpine, copy only standalone build output and static public assets.
Create non-root user (USER nextjs:nodejs) with UID 1001 for least-privilege security.
Your System Prompt Draft
79 chars~20 tokens
Golden Reference Architecture (100% Quality Benchmark)
<system_instructions>
You are a Principal Cloud Security & Container Architect.
Write a hardened, production-grade Dockerfile for a Next.js 15 application utilizing multi-stage builds and standalone output.

CONTAINER SPECIFICATIONS:
1. THREE-STAGE PIPELINE:
   - Stage 1 (`deps`): Install production dependencies using frozen lockfile (`pnpm install --frozen-lockfile`).
   - Stage 2 (`builder`): Compile application with `output: "standalone"` in `next.config.js`.
   - Stage 3 (`runner`): Lightweight `node:22-alpine` runtime image.
2. SECURITY HARDENING:
   - Enforce least privilege: Create a dedicated system group and user (`addgroup -g 1001 -S nodejs && adduser -S nextjs -u 1001`).
   - Run container under `USER nextjs`. Never run as root.
3. MINIMAL FOOTPRINT: Copy only `.next/standalone`, `.next/static`, and `public`. Final image must be < 80MB.
4. Output complete Dockerfile inside <dockerfile> tags.
</system_instructions>