Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To make Docker builds faster, put stable, expensive steps—especially dependency installation—before frequently changing source files. Docker reuses matching build results; when an instruction misses the cache, that instruction and every later step must run again. For repeat builds on ephemeral CI workers, import and export a BuildKit cache, and use cache mounts for package-manager or compiler data that can safely be recreated.

How Docker’s build cache works

Docker processes Dockerfile instructions in order and checks whether it can reuse the result of each instruction from an earlier build. As Docker puts it, “If no cached layer matches the instruction exactly, the cache is invalidated.” Once an instruction misses, the later instructions are rebuilt too. A source copy near the start of a Dockerfile can therefore force dependency installation and compilation to run again even when those expensive steps did not need to change. Docker’s cache invalidation documentation explains the matching rules.

For COPY and ADD, Docker uses file metadata checksums; modification time alone does not invalidate the cache. A RUN instruction is generally matched using its command and the preceding build state, rather than by inspecting whether files inside the container have changed since the previous run. This distinction explains why changing a copied dependency manifest can invalidate installation, while changing a remote package repository does not by itself make a cached installation command run again.

Arrange Dockerfile steps to preserve useful cache

Place steps that change infrequently before steps that change often. Copy dependency manifests first, install dependencies, then copy the application source and run the build. Keep the build context small with a .dockerignore file so irrelevant or frequently changing files do not enter broad early COPY instructions. Docker’s build cache optimization guidance covers layer ordering and build-context practices.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
# syntax=docker/dockerfile:1
FROM node:22-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
  • COPY package.json package-lock.json ./ isolates dependency inputs. Source edits that leave the manifests unchanged can reuse the install step.
  • The cache mount gives npm a reusable package-data directory between builds. It is not an image layer and can be removed independently.
  • COPY . . comes after installation, so ordinary source changes do not invalidate the earlier dependency step.

Adapt the manifest names, package-manager command, mount target, and build steps to the application. For multi-stage builds, keep compilers, test tools, and other build-only dependencies in a build stage where practical, then copy only required runtime artifacts into the final stage. This can reduce the final image without changing the cache-ordering principle.

Use cache mounts for reusable package or compiler data

BuildKit supports cache mounts on RUN instructions. They preserve useful data—such as downloaded packages or compiler outputs—outside the image layer, allowing a command that must execute again to reuse that data and avoid some repeated work. Docker describes this feature in its cache optimization documentation.

A cache mount is an optimization, not an input your build may require to be correct. BuildKit may prune or replace its contents, so dependency installation and compilation must work when the mount is empty. Treat it as disposable storage, not as a substitute for declaring dependencies or copying required files into the build.

Make CI cache survive short-lived workers

A local builder can reuse its own cache, but a replacement CI worker may start without that local state. Buildx can import and export cache with --cache-from and --cache-to. Docker documents inline, local, registry, and GitHub Actions (gha) cache backends for supported drivers; check that the chosen backend works with your builder driver and CI environment. Its external cache documentation describes backend options and configuration.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
docker buildx build 
  --cache-from type=registry,ref=registry.example.com/team/app:buildcache 
  --cache-to type=registry,ref=registry.example.com/team/app:buildcache,mode=max 
  -t registry.example.com/team/app:latest .

This example uses the same registry reference to import and export a registry cache. Replace it with a cache location your organization controls. Before adopting a backend, account for builder-driver support, registry access and retention policies, storage and transfer costs, and who can read or write the cache. Cache is disposable acceleration: a build should still be correct if it is unavailable.

Do not put credentials in COPY inputs or build arguments merely to make them available during a build. Use dedicated BuildKit secret mounts, and review cache-export permissions so cached data is not exposed across trust boundaries. See Docker’s cache backend guidance and build secret documentation.

Balance cache reuse against freshness and reproducibility

A cached RUN command does not rerun simply because a package repository now offers newer packages. If you need a deliberate refresh, change an earlier input that affects the step, run docker builder prune to remove builder cache, use --no-cache for a build without cache reuse, or use --no-cache-filter <stage> to disable cache for a selected stage. Choose a refresh policy intentionally: rebuilding dependencies for freshness can cost time, while reusing an old result can preserve older dependency state.

When reproducibility matters, pin base images to explicit versions or digests rather than relying on mutable tags. A cache hit improves speed, but it does not by itself guarantee that a build uses current upstream packages or that a mutable base-image tag always resolves to the same image. Docker discusses cache invalidation and freshness controls in its cache invalidation documentation; base-image guidance is in Dockerfile best practices.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose an optimization by measuring the build you have

BuildKit’s concurrent build-graph solver can run independent operations in parallel, and its content-addressed cache can be exported for reuse on another host. The payoff varies with how much work is independent, storage and network speed, cache hit rate, dependency-change frequency, and the cost of moving cache data. There is no universal speedup percentage. Docker explains BuildKit’s capabilities in its BuildKit documentation.

Compare representative cold and warm builds rather than judging a cache setup by configuration alone. Include the time to download or restore cache, not just the time spent running build steps.

Approach Where it helps What to account for
Instruction/layer cache Repeated builds on a builder that retains its cache Changes invalidate the changed instruction and following steps; order stable inputs first.
BuildKit cache mount Repeated package downloads or compiler work, including when a step must execute again Mount contents are disposable; the build must work with an empty cache.
External cache import/export CI workers that are replaced or do not retain local builder state Backend and driver compatibility, network transfer, storage retention, and cache access controls.

These techniques address different kinds of repeated work: layer caching skips an unchanged instruction, a cache mount supplies reusable data to a running instruction, and external caching carries reusable build results between workers. Often they can be combined, but their value depends on the build’s actual bottleneck.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.