Guides
How to fix Docker cache invalidation
Docker layers rebuild even when the relevant files did not change.
5 min read
Docker layers rebuild even when the relevant files did not change.
Why it happens
A COPY earlier in the Dockerfile than it needs to be invalidates everything after it.
How to fix it
- Copy the lockfile and install dependencies before copying application source
- Use .dockerignore so unrelated files do not bust the cache
- Pin the base image by digest so an upstream rebuild does not invalidate everything
- Use cache mounts for package manager directories
Your next build could be twice as fast, at half the price
Start free. Migrating away is the same one line, and we publish that diff too.
Related
How to make CI caches actually persistCaches are configured but hit rate is low.How to work around the 10 GB cache limitYour caches exceed 10 GB per repository and start silently evicting.How to share a build cache across repositoriesEach repository maintains its own cache for the same shared dependencies.How to share a Docker cache between jobsEach matrix leg rebuilds the same layers.