Dockerizing Node.js Apps

Difficulty: Intermediate

Question

How do you write an optimized Dockerfile for a Node.js application so builds are fast and the image is small and production ready?

Answer

The biggest win is ordering instructions for layer caching: copy package.json and the lockfile first, run npm ci, and only then copy the rest of the source. Docker reuses the cached dependency layer as long as those two files do not change, so editing application code no longer triggers a full reinstall. Combine that with a slim or Alpine base, a .dockerignore, production-only dependencies, a non-root user and exec-form CMD for a fast, small and safe image.

Why instruction order matters

Each instruction creates a layer, and Docker's cache is invalidated from the first instruction whose inputs changed; every instruction after it re-runs. If COPY . . comes before npm install, any change to any file (even a README) invalidates the install layer. Putting the dependency manifest first means the expensive step only re-runs when dependencies really change.

Checklist for a production Node image

  1. Pin a base image: node:24-alpine or node:24-bookworm-slim, not node:latest. Alpine uses musl libc, so native modules (like sharp or bcrypt) sometimes need extra build tools or prebuilt musl binaries; -slim (Debian, glibc) avoids that at a slightly larger size.
  2. Use npm ci, not npm install: ci installs exactly what package-lock.json says, fails if the lockfile and package.json disagree, and is designed for automated builds.
  3. Install only production deps in the final image: npm ci --omit=dev (the older flag --only=production is deprecated).
  4. Multi-stage for builds: if you compile TypeScript or bundle, do it in a build stage and copy only dist/ plus production node_modules to the runtime stage.
  5. Cache mounts: RUN --mount=type=cache,target=/root/.npm npm ci keeps npm's download cache across builds without storing it in the image.
  6. .dockerignore: exclude node_modules, .git, .env, logs and build output so the context is small and host modules are never copied in.
  7. Run as non-root: the official image ships a node user; add USER node.
  8. Start node directly: CMD ["node", "dist/server.js"], not npm start, so signals reach your process.
  9. Set NODE_ENV=production: frameworks like Express enable production behaviour based on it.

Common mistakes

  • Copying a host node_modules built on Windows or macOS into a Linux image; native binaries break.
  • Forgetting the lockfile in the first COPY, which makes npm ci fail.
  • Installing dev tools like nodemon in the production image.
  • Baking secrets like an .npmrc with a private registry token into a layer; use RUN --mount=type=secret instead.
  • Using pnpm or yarn without enabling them; Corepack is no longer bundled starting with Node 25, so install the package manager explicitly in recent images.

How it looks in CI

In a GitHub Actions or GitLab pipeline, combine the ordering above with a remote build cache (docker buildx build --cache-from type=registry,... --cache-to ...) so even fresh CI runners reuse the dependency layer. On a typical service this turns repeated multi-minute builds into ones where only the final copy and build steps run.

Cache-friendly Node.js image layers. Rarely changing layers sit low; frequently changing code sits on top.
  1. CMD ["node", "dist/server.js"]: Metadata only, no filesystem change.
  2. COPY dist or source: Changes on almost every commit.
  3. RUN npm ci --omit=dev: Rebuilt only when the lockfile changes.
  4. COPY package.json package-lock.json: Small, changes rarely.
  5. FROM node:24-alpine: Base layers shared across all Node services.

Code examples

Production Dockerfile for a TypeScript Node API

# syntax=docker/dockerfile:1
FROM node:24-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm npm ci
COPY . .
RUN npm run build

FROM node:24-alpine AS runtime
ENV NODE_ENV=production
WORKDIR /app
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]

The build stage has dev dependencies for TypeScript; the runtime stage has only production deps and compiled output, runs as the node user and starts node directly.

.dockerignore for a Node project

node_modules
npm-debug.log*
dist
coverage
.git
.github
.env
.env.*
Dockerfile
compose*.yaml
*.md

Keeps the build context small, prevents host node_modules from overwriting container deps, and keeps .env secrets out of the image.

Key points

  • Copy package.json and the lockfile before source code so npm ci is cached.
  • Use npm ci and npm ci --omit=dev for reproducible, production-only installs.
  • Alpine is small but uses musl; slim images avoid native module issues.
  • Run as USER node and start with CMD ["node", ...], not npm start.
  • Use BuildKit cache mounts and secret mounts instead of baking caches or tokens into layers.

Common follow-up questions

  • What should go in a .dockerignore file for a Node project?
  • Why use npm ci instead of npm install in Docker?
  • What problems can Alpine cause for Node native modules?
  • How do you pass a private npm token during the build without leaking it?
  • How would you speed up Docker builds in CI where there is no local cache?

Interview tips

  • Explain cache invalidation in one sentence: everything after the first changed instruction re-runs.
  • Go past the basic ordering trick to multi-stage, non-root and signal handling; it shows production maturity.
  • If you have numbers from your own project, such as build time before and after, share them; otherwise describe the change qualitatively rather than inventing figures.

Concepts covered

Layer Caching, npm ci, BuildKit Cache Mounts, .dockerignore, Non-root User

More Docker & Containers questions