User Agents

Web browsers and everyday web tech, explained

Docker Build Cache Works Locally but Not in CI: Why

· 6 min read · Cookies and Cache

In this article
  1. Background: How Docker Build Cache Works
  2. Why the Cache Misses in CI
  3. How to Fix the Cache Miss
  4. Common Misconception: Cache is Shared Between Local and CI
  5. Comparing BuildKit vs Legacy Builder
  6. How to Verify What's Invalidating the Cache
  7. Frequently Asked Questions

Your Docker build cache works locally but not in CI because the cache keys are computed from the build context, the base image digest, and the exact build arguments, and any difference in those inputs—such as a different file modification time, a different Git checkout, or a different base image tag—invalidates the cache. The most common cause is that your CI checks out the repository with a fresh timestamp or includes files in the build context that change on every run, like a lockfile with a timestamp or a version file. Fixing it means making your build context deterministic and pinning your base images.

Background: How Docker Build Cache Works

Docker's build cache is a layer-based system. Each instruction in your Dockerfile (FROM, RUN, COPY, etc.) creates a layer, and Docker stores a cache entry for that layer keyed by a hash of the instruction plus its inputs. When you run docker build again, Docker checks whether the hash for each instruction matches a cached layer. If it does, it reuses the layer and skips re-executing the instruction. If it does not, it invalidates that layer and all subsequent layers, forcing a rebuild.

The cache key for a COPY or ADD instruction includes the contents of the files being copied. For a RUN instruction, the key includes the command string and the parent layer's ID. For a FROM instruction, the key includes the base image's digest (not just the tag). This means any change in the files, the command, or the base image will break the cache.

Locally, you often build in the same directory with the same files, so the cache hits. In CI, the build context is often a fresh checkout, and subtle differences—file timestamps, line endings, or extra files—can change the hash.

Why the Cache Misses in CI

There are several concrete reasons your cache works locally but not in CI:

  • Different file timestamps: Docker's cache key for COPY includes file metadata, including modification time. A fresh Git checkout in CI sets new timestamps, so the hash differs from your local copy. Even if file contents are identical, the timestamp change invalidates the cache.
  • Different build context: CI often runs git clone into a fresh directory, and the build context may include files you don't have locally, like CI-specific config files or a different .dockerignore.
  • Base image tag drift: If your Dockerfile uses FROM node:20 (a tag), the base image may have been updated since your last local build. The digest changes, so the cache misses. Locally, you might have the old image cached, so it hits.
  • Build arguments: If you pass build args with dynamic values (like a timestamp or a Git commit SHA), the cache key changes every run. Locally, you might not pass them, or you pass a static value.
  • CI-specific environment: Some CI systems set environment variables that affect the build, or they use a different platform (e.g., ARM vs x86), which changes the base image digest.

How to Fix the Cache Miss

To make your CI build cache work, you need to make the cache key deterministic. Here are the steps:

  1. Pin your base images to a specific digest, not just a tag. For example, use FROM node:20@sha256:abc123.... This ensures the base image is identical across runs.
  2. Use a .dockerignore file to exclude files that change often or are not needed, like .git, node_modules, or CI config files. This reduces the build context size and avoids accidental changes.
  3. Normalize file timestamps in CI before building. You can use git checkout --no-optional-locks or set a fixed timestamp with touch -t on all files. Some CI systems have a setting to preserve timestamps from the Git checkout.
  4. Make build args static or use a value that only changes when the source changes, like the Git commit SHA. Avoid timestamps or random values.
  5. Use BuildKit (the modern Docker builder) which has a more robust cache and supports cache mounts. Enable it with DOCKER_BUILDKIT=1 or use docker buildx build.
  6. Leverage CI cache features like GitHub Actions' cache action or GitLab's cache, which can persist the Docker layer cache between runs. This is often the real fix—your CI is not reusing the cache from previous CI runs, not just failing to match your local cache.

Common Misconception: Cache is Shared Between Local and CI

Many developers assume that because the cache works locally, it should also work in CI. But the cache is not shared—each environment has its own local Docker cache. The CI runner starts with an empty cache unless you explicitly restore it. So the question isn't really "why does it work locally but not in CI?" but "why doesn't the CI cache hit from previous CI runs?" The answer is usually the same: the cache key changed because of the reasons above.

Comparing BuildKit vs Legacy Builder

Docker's legacy builder (pre-BuildKit) had a simpler cache that was easily invalidated. BuildKit, introduced in Docker 18.09, uses a content-based cache that is more efficient. BuildKit also supports RUN --mount=type=cache for package manager caches, which can speed up builds significantly. If your CI uses the legacy builder, switching to BuildKit can improve cache hit rates. Most CI providers now support BuildKit, but you may need to enable it explicitly.

How to Verify What's Invalidating the Cache

To debug, run the build in CI with verbose output: docker build --progress=plain (with BuildKit) or docker build --no-cache=false --rm=false and look for the line that says "CACHED" or "DONE". The first instruction that is not cached tells you what changed. For example, if the COPY instruction is not cached, compare the file checksums between your local and CI contexts. You can also use docker history to see the cache keys.

Frequently Asked Questions

Why does my Docker build cache miss on every CI run even though nothing changed?

The most likely cause is that your build context includes a file that changes on every run, such as a timestamp or a version file generated by CI. Check your .dockerignore and ensure you're not copying files like .git or any generated files. Also, if you're using a base image tag, it may have been updated between runs.

Should I use the Git commit SHA as a build argument?

Yes, using the Git commit SHA as a build argument is a good practice because it changes only when the source changes. It also helps with traceability. However, if you pass it as a build arg, it will invalidate the cache for the RUN instruction that uses it, which is fine because you want that to change when the code changes.

Does Docker cache the base image pull?

Docker caches the base image layers locally, but if you use a tag, it may re-pull the image to check for updates. Using a digest avoids re-pulling if the image is already present. In CI, you need to ensure the cache is persisted between runs; otherwise, it will pull the image every time.

Can I use a cache mount for npm or pip in CI?

Yes, with BuildKit you can use RUN --mount=type=cache,target=/root/.npm to cache package manager downloads. This is separate from the layer cache and can speed up builds even when the layer cache misses. In CI, you need to configure the cache to be persisted, often via a volume or a cache action.

Why does my local build use cache but CI doesn't, even with the same Dockerfile?

Because the cache is local to each machine. Your local Docker has a cache from previous builds, while CI starts fresh. The real issue is that the CI cache is not being restored from previous CI runs. Set up CI cache persistence to reuse layers across runs.

Does Docker cache work with multi-stage builds?

Yes, Docker caches each stage independently. If an earlier stage is invalidated, later stages that depend on it will also be invalidated. To maximize cache hits, order your Dockerfile so that the most frequently changed instructions (like COPY of source code) come after the less frequently changed ones (like RUN apt-get install).