Dockerfile Best Practices: Layer Caching, Small Images and Fast Builds

Write better Dockerfiles: how layer caching works, ordering instructions, multi-stage builds, slim base images, .dockerignore, BuildKit cache mounts.

Published December 2, 2026

A Dockerfile that works is easy to write. One that builds in seconds instead of minutes, produces a 60 MB image instead of 1.2 GB, and does not ship compilers and secrets to production takes a little more care. Most improvements come from understanding one mechanism: layer caching.

The concept

Each instruction in a Dockerfile creates a layer. BuildKit caches layers and reuses them as long as the instruction and its inputs are unchanged; once a layer changes, every layer after it is rebuilt. That is why you copy dependency manifests and install dependencies before copying the rest of your source code: editing application code then reuses the expensive dependency layer.

Multi-stage builds separate building from running. A builder stage contains compilers, dev dependencies and build tools; the final stage copies only the built artefacts onto a slim runtime image. Combined with a .dockerignore file, slim or distroless bases, a non-root user and BuildKit cache mounts for package managers, images become smaller, faster and safer.

Ordering for cache hits

FROM node:22-slimbase (cached)COPY package*.jsonchanges rarelyRUN npm ciexpensive, cached if lockfile unchangedCOPY . .changes oftenRUN npm run buildrebuilds when code changes
Order from least to most frequently changing.

Image size by approach

node:22 single stage≈1.1 GBnode:22-slim≈300 MBmulti-stage, slim runtime≈180 MBdistroless runtime≈140 MB
Illustrative image sizes for a typical Node.js API.

Checklist

PracticeBenefit
Copy lockfiles before sourceDependency layer cached across code changes
Multi-stage buildsNo build tools in production image
.dockerignore (node_modules, .git, .env)Smaller context, no leaked secrets
Pin base image versionsReproducible builds
USER non-rootReduced impact of compromise
RUN --mount=type=cacheFaster package installs in CI and locally

Practical examples

Multi-stage Node.js Dockerfile

Builder compiles; runtime only has what it needs.

# syntax=docker/dockerfile:1
FROM node:22-slim AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN --mount=type=cache,target=/root/.npm npm ci
COPY . .
RUN npm run build && npm prune --omit=dev

FROM node:22-slim
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build --chown=node:node /app/node_modules ./node_modules
COPY --from=build --chown=node:node /app/dist ./dist
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]

.dockerignore

Keep the build context small and secret-free.

node_modules
.git
.env*
dist
coverage
*.log

Use registry cache in CI

Reuse layers across CI runners.

docker buildx build \
  --cache-from type=registry,ref=registry.example.com/api:buildcache \
  --cache-to type=registry,ref=registry.example.com/api:buildcache,mode=max \
  -t registry.example.com/api:$GIT_SHA --push .

Common mistakes

  • COPY . . before installing dependencies, invalidating the cache on every change.
  • Shipping compilers, dev dependencies and source maps in production images.
  • No .dockerignore, sending node_modules and .env into the build context.
  • Passing secrets with ARG or ENV, which persist in image layers (use build secrets).

Key takeaways

  • Layer caching rewards ordering from stable to volatile.
  • Multi-stage builds keep production images small.
  • Use .dockerignore, pinned bases and non-root users.
  • BuildKit cache mounts and registry caches speed up CI.

FAQ

How do I pass secrets to a build?

Use BuildKit secrets: RUN --mount=type=secret,id=npmrc ... and docker build --secret id=npmrc,src=.npmrc. They are not stored in layers.

Alpine or slim?

Alpine is smaller but uses musl libc, which can cause compatibility issues with native modules; Debian slim is a safer default.

Does the number of layers matter?

Less than it used to; focus on cache-friendly ordering and not leaving large files in intermediate layers.

More DevOps articles