Difficulty: Intermediate
How do you write an optimized Dockerfile for a Node.js application so builds are fast and the image is small and production ready?
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.
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.
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.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.npm ci --omit=dev (the older flag --only=production is deprecated).dist/ plus production node_modules to the runtime stage.RUN --mount=type=cache,target=/root/.npm npm ci keeps npm's download cache across builds without storing it in the image..dockerignore: exclude node_modules, .git, .env, logs and build output so the context is small and host modules are never copied in.node user; add USER node.CMD ["node", "dist/server.js"], not npm start, so signals reach your process.NODE_ENV=production: frameworks like Express enable production behaviour based on it.node_modules built on Windows or macOS into a Linux image; native binaries break.COPY, which makes npm ci fail..npmrc with a private registry token into a layer; use RUN --mount=type=secret instead.pnpm or yarn without enabling them; Corepack is no longer bundled starting with Node 25, so install the package manager explicitly in recent images.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.
# 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.
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.
Layer Caching, npm ci, BuildKit Cache Mounts, .dockerignore, Non-root User