---
title: "A Production-Ready Dockerfile for Node.js with Multi-Stage Builds"
description: "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."
url: https://sanderyaz.com/en/blog/nodejs-docker-multi-stage-build/
author: Sander Yazıcıoğlu
date: 2026-12-02
updated: 2026-12-02
lang: en
tags: ["Docker","Node.js","DevOps","Backend"]
---

# 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

```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`:

```text
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:

```js
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?

| Base | Pro | Con |
|---|---|---|
| `node:22-alpine` | Very small | musl libc; trouble with some native modules |
| `node:22-slim` | glibc, broad compatibility | Bigger than Alpine |
| Distroless | Smallest attack surface | No 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](/en/blog/typescript-nodejs-api-error-handling/).

---

About the author: [Sander Yazıcıoğlu](https://sanderyaz.com/en/about/). Sander Yazıcıoğlu is a Senior Full-Stack Developer in Istanbul. At LiveTesting he builds the backend, frontend, realtime systems and Appium automation of a real-device testing platform; before that he built enterprise Angular apps at Testinium and Loadium.
