Skip to Content
Mengty Learns
Containers

Image Layers and Build Cache

How Docker caches read-only filesystem layers and why COPY order determines build speed.

explanationbeginnerTested with docker 29.4.3Updated Nov 26, 2026

TL;DR

Every RUN, COPY, and ADD instruction in a Dockerfile creates an immutable layer. Once a layer's inputs change, Docker invalidates that layer and every instruction after it—so ordering instructions from least-frequently changed to most-frequently changed keeps builds fast.

Why it exists

Without layer caching, rebuilding a container after editing a single line of application code would re-download the base image, re-install OS packages, and re-run npm install from scratch. Layer caching lets Docker reuse unchanged work across builds.

Mental model

Think of a Dockerfile as a linear stack of checkpoints:

  1. Base image (FROM node:22-alpine3.21) — changes rarely.
  2. Dependency manifest (COPY package.json ./ + RUN npm install) — changes only when dependencies change.
  3. Application source (COPY server.mjs ./) — changes on almost every commit.

When Docker evaluates COPY, it checksums the copied files. If you write COPY . . before RUN npm install, editing server.mjs changes the COPY . . checksum, which busts the npm install layer below it. Splitting COPY package.json ./ from COPY server.mjs ./ isolates source edits to the final layer.

Run it: labs/docker-multistage-next

Inspect the COPY order and two-stage Dockerfile in the minimal Node app lab.

docker build -t mt-multistage-next:lab labs/docker-multistage-next

Do it

Compare the slow order versus the cache-friendly order:

# Avoid — cache-busting order: any edit to server.mjs re-runs npm install
FROM node:22-alpine3.21
WORKDIR /app
COPY . .
RUN npm install --omit=dev

# Good — cache-friendly order: source edits skip npm install
FROM node:22-alpine3.21
WORKDIR /app
COPY package.json ./
RUN npm install --omit=dev --ignore-scripts
COPY server.mjs ./

Run two consecutive builds on labs/docker-multistage-next to observe CACHED layers in BuildKit output:

docker build -t mt-multistage-next:lab labs/docker-multistage-next
# Edit server.mjs, then rebuild:
docker build -t mt-multistage-next:lab labs/docker-multistage-next

Gotchas

  • Missing .dockerignore: Without .dockerignore, COPY . . sends local node_modules, .git, and log files into the build context, slowing context transfer and causing surprise cache misses.
  • Unpinned base tags (:latest): Even if your Dockerfile is unchanged, pulling a moving tag can silently change the base layer and invalidate everything below it.
  • Deleting files in a later RUN: Removing a large temp file in a separate RUN instruction does not shrink earlier layers; clean up in the same RUN or use multi-stage builds.

Production checklist

  • Order COPY instructions from least-frequently changed (package.json, lockfile) to most-frequently changed (source code).
  • Add a .dockerignore excluding .git, node_modules, README.md, and local build artifacts.
  • Pin base image tags (for example node:22-alpine3.21) instead of :latest.
  • Keep RUN package installs and cache cleanups in the same layer or drop build tools in a multi-stage build.

Check yourself

Check Yourself

1. You edit server.mjs in a project whose Dockerfile runs COPY package.json ./, then RUN npm install, then COPY server.mjs ./. Which layers rebuild?

2. Why is a .dockerignore file important even when you only COPY specific files?

Sources