---
title: "Discussion on \"Production Next.js Dockerfile: Multi-Stage, Standalone and Non-Root\" | Hashnode"
description: "Discussion on \"Production Next.js Dockerfile: Multi-Stage, Standalone and Non-Root\". A production Next.js Dockerfile for Next.js 16 and Node 24: multi-stage build, standalone output, non-root user, sharp, env vars and health checks."
url: "https://hashnode.com/posts/nextjs-dockerfile/6adf7d1d26319dd161d10aa0"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Production Next.js Dockerfile: Multi-Stage, Standalone and Non-Root](https://fabwaseem.hashnode.dev/nextjs-dockerfile?utm_source=hashnode&utm_medium=feed)

A production Next.js Dockerfile for Next.js 16 and Node 24: multi-stage build, standalone output, non-root user, sharp, env vars and health checks.

Oct 26 · 15 min read · fabwaseem.hashnode.dev  
Read the article

-   [#docker](https://hashnode.com/tag/docker)
-   [#nextjs](https://hashnode.com/tag/nextjs)
-   [#devops](https://hashnode.com/tag/devops)
-   [#deployment](https://hashnode.com/tag/deployment)
-   [#self-hosting](https://hashnode.com/tag/self-hosting)

## Written by

[Waseem Anjum](https://hashnode.com/@fabwaseem)

@fabwaseem · Lahore, Pakistan

  
[View profile](https://hashnode.com/@fabwaseem)

Full Stack & AI Automation Engineer

I'm a full stack and AI automation engineer in Lahore, Pakistan. I build AI agents, MCP servers and automation pipelines, and the Next.js, React, Node.js and NestJS products around them, on PostgreSQL or MongoDB. Clear system design, fast pages and code that holds up in production.

## Responses 5

Markdown supported · ⌘↵ to post

[AI Developer](https://hashnode.com/@lycore)

[Oct 26](https://hashnode.com/posts/nextjs-dockerfile/6adf7d1d26319dd161d10aa0/comment/6adf89082263bbe898e1283a)

The measured size comparison is what makes this convincing, and the split between public build-time ARGs and runtime secrets is a detail many Dockerfile guides get wrong. One suggestion: the HEALTHCHECK calls /api/health via a node fetch, so it would be worth noting it should stay a cheap liveness check rather than touching the database, otherwise a DB blip makes orchestrators restart healthy containers. Also, did you compare node:24-slim against alpine or distroless for sharp compatibility and CVE surface?

[Waseem Anjum](https://hashnode.com/@fabwaseem)

[Oct 26](https://hashnode.com/posts/nextjs-dockerfile/6adf7d1d26319dd161d10aa0/comment/6adf89082263bbe898e1283a#reply-6adfa5cfba2907af020b0c4e)

Good call on keeping it a liveness check. The /api/health route in the post only returns a status and the process uptime, with no database call, and that's on purpose: if a DB blip fails the probe, Docker marks the container unhealthy and Coolify or Swarm can pull containers that are actually fine. If you want DB status, put it on a separate endpoint that your monitoring watches.

On base images, I measured slim and Alpine in the same test: 287 MB on node:24-slim vs 229 MB on node:24-alpine. I didn't build a distroless variant or run a CVE scan, so I don't have numbers there. Sharp ships prebuilt musl binaries, so Alpine works for it, but I keep slim as the default because glibc is the safer bet for other native modules. A distroless runner with a Trivy or Docker Scout comparison would make a good follow-up.

[AI Developer](https://hashnode.com/@lycore)

[Oct 27](https://hashnode.com/posts/nextjs-dockerfile/6adf7d1d26319dd161d10aa0/comment/6adf89082263bbe898e1283a#reply-6adfeb3c492df5eac02ea8ae)

Thanks, that separation makes sense: a liveness probe that only reflects the process keeps orchestrators from amplifying a database blip. A dependency-aware readiness endpoint, kept out of the restart path, is a good complement for monitoring. The slim vs Alpine numbers are helpful, and glibc as the safer default for other native modules is a sensible call.

[Abhinav Sharma](https://hashnode.com/@abhinavs1920)

[Oct 26](https://hashnode.com/posts/nextjs-dockerfile/6adf7d1d26319dd161d10aa0/comment/6adf82a197e881f41f9fa012)

The standalone tracing is the detail people miss: only the files the server actually imports make it into the runtime stage, which is where most of the size drop comes from. The non-root step pairs well with it and is an easy one to skip.

[Waseem Anjum](https://hashnode.com/@fabwaseem)

[Oct 26](https://hashnode.com/posts/nextjs-dockerfile/6adf7d1d26319dd161d10aa0/comment/6adf82a197e881f41f9fa012#reply-6adfa5e8ba2907af020b0d97)

Exactly. In my test the app files inside the standalone runner came to 59 MB, so most of the 287 MB is the Node base image itself. And non-root is only a couple of lines once the node user owns .next, so there's little reason to skip it.
