Dockerfile Builder for Node.js

Generate a production-ready multi-stage Dockerfile for Node.js apps

Build a production-ready multi-stage Dockerfile for Node.js. Configure Node version, package manager, port, build step and a /health HEALTHCHECK, with a non-root user for Express, NestJS or Next.js.

Why use a multi-stage build?

A multi-stage build compiles your app and installs dev dependencies in a builder stage, then copies only the runtime artifacts and production dependencies into a slim final image. This keeps the shipped image small and free of build tooling and source.

A builder for a production-ready, multi-stage Dockerfile for Node.js apps — Express, NestJS, Fastify or Next.js. Choose your Node version, package manager and port, and the tool emits an image that compiles in a builder stage and ships only the runtime in a slim final stage, running as a non-root user.

How it works

The generated Dockerfile uses three stages. The builder stage installs all dependencies (npm ci, yarn install --frozen-lockfile or pnpm install --frozen-lockfile), copies your source and runs the build script if you have one. A separate deps stage installs production-only dependencies so dev tooling never reaches the final image. The runner stage copies the production node_modules and the compiled dist/ output, sets NODE_ENV=production, switches to the built-in non-root node user, exposes your port and defines the start command.

Health checks and security

When enabled, the HEALTHCHECK instruction calls your /health endpoint with Node’s built-in fetch, exiting non-zero on failure so the orchestrator can restart the container. Running as USER node is a baseline hardening step: it ensures a compromised process does not have root inside the container.

Tips

Always add a .dockerignore next to the Dockerfile. A minimal one looks like:

node_modules
.git
.env
dist
npm-debug.log

This keeps the build context small and prevents secrets and local artifacts from being baked into the image. For Next.js standalone output, point the start command at node server.js and copy the .next/standalone and .next/static folders instead of dist/.