ysskrishna logoysskrishna

Nextjs Multi-Stage Docker

See the real size gap between one-stage and multi-stage Next.js Docker builds.

Get it
Built with
Starter Kits & Boilerplates
DevOps & Infrastructure
Docker
Next.js
TypeScript
React
JavaScript
Bash
Node.js
Details
FreeMIT licenseCreated June 8, 2025Updated November 12, 2026
Share this product

Your Next.js container ships the kitchen, not just the meal.

The image pulls slow. Registry storage creeps up. Someone on the team asks why production still carries build tools. You know multi-stage builds are supposed to help, but you want proof on a real app before you touch the pipeline.

This repo is that proof. Same Next.js sample, two Docker paths, and a size gap you can measure yourself.

One codebase. Two images. Let the numbers settle the argument.

What you get

  • A side-by-side you can run on your machine. One single-stage Dockerfile and one multi-stage Dockerfile, both wired through Docker Compose so you are not guessing from a blog post.
  • Documented size gap on the bundled sample. The README records about 1.22GB for single-stage and about 192MB for multi-stage, roughly 84% smaller on that snapshot.
  • A production-minded multi-stage pattern. Dependency, builder, and runner stages, production-only dependencies in the final image, and a non-root user in the runner.
  • Honest tradeoffs in plain language. The README calls single-stage simpler and faster to build, and multi-stage slightly slower with more moving parts.
  • A comparison demo, not a platform. Use it to decide, teach, or defend a team standard. You still own base images, secrets, and whatever runs in production.

Picture your Monday

You are pitching a slimmer Docker standard before the next deploy window. You clone the repo, stand up both variants locally, and pull the size column from your own Docker images list. In an illustrative case, you walk into the meeting with two numbers from the same app instead of a screenshot from someone else's stack.

Ready to try it?

  • Open the repo on GitHub for both Dockerfiles, the compose flow, and the documented size comparison
  • MIT licensed. Free to fork for internal examples or talks.

Keep exploring

Frequently asked questions

Why not just copy the official Next.js Docker example?

You can. This repo is for when you need the before and after on the same codebase. It keeps a single-stage path next to a multi-stage path and documents how far apart they landed on the bundled sample.

How much smaller is multi-stage than single-stage here?

On the sample documented in the README, single-stage landed around 1.22GB and multi-stage around 192MB, roughly 84% smaller. Your app can differ, especially after dependency or framework upgrades, so treat the README figures as a snapshot to re-measure, not a promise for every project.

How do I compare both builds on my laptop?

Copy the sample environment template to a local env file, then use the Docker Compose flow in the repository. Single-stage serves on port 3000 and multi-stage on 3001 so you can run both without port clashes. Compare final image sizes with your local Docker images list when you are done.

Does multi-stage slow the build down?

The README's comparison table says single-stage is faster to build and multi-stage is slightly slower. That is the usual trade: a bit more build time for a smaller, cleaner runtime image.

What security practice does the multi-stage path show?

The multi-stage runner uses a non-root user and leaves build tools and development dependencies out of the final image. Broader security still depends on your base images, patching, and how you handle secrets.

Is it free?

Yes. The project is open source under the MIT license.

Do I need to be a developer?

You need Docker installed and comfort following a README. The value is in running and reading the comparison, not in a managed service.

Continue exploring

ProductFastAPI UV vs Requirements File

FastAPI UV vs Requirements File

Two Docker builds, one FastAPI app. Compare packaging paths on your machine.
DevOps & Infrastructure
Backend & APIs
+6
ProductFlask Nginx Docker Starter

Flask Nginx Docker Starter

Day one should not be proxy wiring. Clone a layout that already puts a front server ahead of your app.
DevOps & Infrastructure
Backend & APIs
+5
BlogCover image for OSC 7501 in Python: Tell the Terminal When Your CLI Is Working, Blocked or Done

OSC 7501 in Python: Tell the Terminal When Your CLI Is Working, Blocked or Done

Python
Python CLI
+1
3 min read
BlogCover image for caffeinate for Windows: Keep a Locked-Down Laptop Awake Without Installing Anything

caffeinate for Windows: Keep a Locked-Down Laptop Awake Without Installing Anything

DevOps & Infrastructure
PowerShell
4 min read
ProductPyPIDocker Detector

Docker Detector

One call tells your Python app whether it is running inside Docker.
DevOps & Infrastructure
PyPI Packages
+6
ProductGitHub MarketplaceRelease from Changelog or Tag

Release from Changelog or Tag

The changelog section you already wrote becomes the GitHub Release.
DevOps & Infrastructure
GitHub Marketplace Actions
+2