Docker · Node.js · DevOps · Backend

A Production-Ready Dockerfile for Node.js with Multi-Stage Builds

Small, fast and secure Docker images for Node.js: multi-stage builds, layer caching, a non-root user, signal handling and health checks, explained line by line.

Sander Yazıcıoğlu Published: 2 min read Türkçe oku

A Production-Ready Dockerfile for Node.js with Multi-Stage Builds

Most Dockerfile examples on the internet work, but they produce 1 GB images, reinstall dependencies on every code change and run the app as root. This article walks through the production-ready Dockerfile I use for a Node.js service.

The final Dockerfile

# 1) Dependencies
FROM node:22-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci

# 2) Build
FROM deps AS build
COPY . .
RUN npm run build && npm prune --omit=dev

# 3) Runtime
FROM node:22-alpine AS runtime
ENV NODE_ENV=production
WORKDIR /app
COPY --from=build --chown=node:node /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/dist ./dist
COPY --chown=node:node package.json ./
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s CMD wget -qO- http://127.0.0.1:3000/health || exit 1
CMD ["node", "dist/server.js"]

Why multi-stage matters

Building needs TypeScript, test tools and dev dependencies; running doesn't. A multi-stage build compiles in a separate stage and copies only the result into the final image. For a typical TypeScript service the image shrinks by hundreds of megabytes, and a smaller image means faster deploys and a smaller attack surface.

Layer caching: order is everything

Docker caches every instruction as a layer, and when one layer changes, everything after it reruns. Copying package.json and the lockfile before the source code means npm ci comes from cache when only code changes. Build times drop from minutes to seconds.

Don't forget .dockerignore:

node_modules
dist
.git
.env
coverage

Otherwise your local node_modules, or even your .env file, can end up in the image.

A non-root user

Official Node images include an unprivileged user called node. Running the app with USER node means an attacker exploiting a vulnerability doesn't become root inside the container.

Signals and graceful shutdown

Kubernetes and Docker send SIGTERM when stopping a container. Using CMD ["node", ...] (exec form) lets Node receive the signal directly; starting through npm start can keep it from reaching your app. Catch the signal in the app and finish in-flight requests:

process.on('SIGTERM', () => {
  server.close(() => process.exit(0));
  setTimeout(() => process.exit(1), 10000).unref();
});

Health checks

HEALTHCHECK plus a /health endpoint lets the orchestrator detect a broken container and restart it. Keep the check light; don't run heavy database queries on every call.

Alpine, slim or distroless?

BaseProCon
node:22-alpineVery smallmusl libc; trouble with some native modules
node:22-slimglibc, broad compatibilityBigger than Alpine
DistrolessSmallest attack surfaceNo shell, harder to debug

If you use native modules (such as sharp or bcrypt), slim usually brings fewer surprises.

Secrets

Don't bake API keys into the image with ENV; anyone who pulls the image can read them. Provide them at runtime through environment variables or a secrets manager. If the build itself needs a secret, use BuildKit's --mount=type=secret.

Conclusion

Multi-stage builds, the right layer order, .dockerignore, an unprivileged user, exec-form CMD and a health check: these six things make a Node.js image small, fast and secure. For graceful shutdown and error handling on the app side, see error handling in TypeScript Node.js APIs.

The Node.js Event Loop Explained with Examples

December 2, 2026 · 2 min read

The Node.js Event Loop Explained with Examples

The phases of the Node.js event loop, microtasks versus macrotasks, process.nextTick and setImmediate, and how to find code that blocks the loop.

Node.jsJavaScriptBackendPerformance

All articles →