Inspired by βMy Next.js Docker Image Was 1.97 GB. Standalone Output and Three Stages Took It to 287 MB.β by Waseem Anjum on Medium. Rewritten and expanded by MCB Editorial.
Learning Objectives & Key Takeaways
0 / 4 Completed- Understand Next.js 16 & React 19 core architecture and runtime execution model
- Audit real-world latency, throughput benchmarks, and memory footprint
- Implement zero-downtime deployment patterns with resilience guards
- Verify code snippets and architect blueprints against production SLOs
- Output file tracing prunes node_modules down to only what each route actually needs.
- Standalone output never includes public or .next/static, you must copy both yourself.
- A three-stage Dockerfile keeps the full source tree and dependency install out of the final image.
- Binding to 0.0.0.0 and chowning files before switching to a non-root user prevent silent failures.
- NEXT_PUBLIC_ values are baked in at build time, real secrets belong in runtime environment variables.
The Image That's Bigger Than It Needs To Be
A common first Dockerfile for a Next.js app copies the entire repository, runs npm ci, runs the build, and starts the server with npm start, all in a single stage on top of a full Node image. It works, the app boots, tests pass. But the resulting image carries the whole source checkout, every dev dependency, and often a duplicated node_modules across build layers, which makes every push and pull slower than it needs to be.
Next.js 16 with React 19 doesn't change this problem, and a newer framework version alone won't fix it. The fix is two separate ideas working together: output file tracing, which figures out the minimal set of files a production server actually needs, and a multi-stage Dockerfile, which makes sure only that minimal set crosses into the final image.
- What Output File Tracing Actually Puts In standalone
- Three Stages: deps, builder, runner
- The 404 That Standalone Output Is Guaranteed To Give You
- Binding and Permissions: 0.0.0.0, the node user, and EACCES
- Build-Time Config, Runtime Secrets, and a Cheap Health Check
Checking your membershipβ¦
Members read every Tech Buzz story in full, with code examples, audio and the discussion, plus every course, quiz and interview set in the Learning Dashboard.


