Repository navigation
dockerBuild: don't reread the whole image on every COPY; support cache mounts with uid, gid or mode - #14940
Merged
Conversation
grouville
self-requested a review
October 29, 2026 06:28
grouville
approved these changes
Oct 29, 2026
dockerBuild gives each COPY a content-based cache identity, so that a change to build-context files a COPY doesn't select doesn't rebuild the steps after it (#13639). It computed that identity by performing the copy and content hashing the result. The result is the stage's whole root filesystem, so every COPY that ran read every file of the image built so far. A COPY runs whenever its build context changed, which in CI is every commit. Key an unfiltered COPY instead the way BuildKit does: on its parent's identity, its arguments, and the content it copies. Its sources are resolved with the copy's own wildcard resolver, and each is hashed whole, together with its path. A source that doesn't exist is hashed as missing and left for the copy to report. The copy itself stays lazy. A filtered copy (--exclude) keeps hashing its result, as before: a source hash would have to apply the copy's filter rules exactly as the copy does. Extend the context-change layer cache test to wildcard and --exclude copies, a copied symlink's target, and a change under a COPY. Add a test that a COPY onto an image holding a terabyte sparse file finishes. Signed-off-by: Erik Sipsma <[email protected]>
A Dockerfile cache mount with uid, gid or mode and no from= is seeded from a /cache directory that BuildKit's frontend makes in an empty directory, without makeParents. Since dockerBuild moved to llbtodagger, any mkdir without makeParents failed to convert, so these builds failed with "mkdir without makeParents is unsupported". Dagger always makes parents, and sets the owner of a directory that already exists. A mkdir without makeParents differs only when a parent is missing or the directory exists, neither of which can happen directly under / of an empty directory. Convert that case. Mode 0 stays unsupported, as withNewDirectory takes it as unset, and so do mkdirs anywhere else, such as a cache mount seeded with from=. Signed-off-by: Erik Sipsma <[email protected]>
Build a Dockerfile whose RUNs mount caches seeded from an empty non-root stage and owned through uid=, restart the engine, and check that a change the RUNs don't depend on reuses them, and that a change to a file copied before them reruns them with the cache contents the first engine wrote. Signed-off-by: Erik Sipsma <[email protected]>
sipsma
force-pushed
the
sipsma/dockerfile-copy-hash-selection
branch
from
October 29, 2026 06:35
b1601d1 to
92adf33
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #14749.
Every COPY reread the whole image. #13639 gave each Dockerfile COPY a content-based cache identity, so that changing build-context files a COPY doesn't use doesn't rebuild the steps after it. It computed that identity by performing the copy and content hashing the result, which is the stage's whole root filesystem. So every COPY that ran read every file of the image built so far, and a COPY runs whenever the build context changed, which in CI is every commit. With a large image, or a disk that reads slowly the first time (such as an EBS volume restored from a snapshot), this dominated build time.
An unfiltered COPY is now keyed the way BuildKit keys it: on its parent's identity, its arguments, and the content of the sources it copies, hashed whole with their paths. The image under it isn't read, and the copy stays lazy. A COPY with
--excludekeeps hashing its result as before. Measured on a 4 GB image with two COPYs, a rebuild after a source change went from 8.4 s to 0.16 s.Cache mounts with uid, gid or mode failed to build. For
RUN --mount=type=cache,uid=…, the Dockerfile frontend seeds the cache from a directory it makes without parents, and dockerBuild rejected every such mkdir withmkdir without makeParents is unsupported. That case, directly under/of an empty directory, now converts. A cache mount seeded withfrom=together withuid=,gid=ormode=remains unsupported.Tests. New tests cover a COPY onto an image holding a terabyte sparse file, which timed out before; cache mount ownership; layer cache behavior for wildcard and
--excludecopies, symlinks and changes under a COPY; and Dockerfile cache mounts across an engine restart.