Skip to content

dockerBuild: don't reread the whole image on every COPY; support cache mounts with uid, gid or mode - #14940

Merged
sipsma merged 3 commits into
mainfrom
sipsma/dockerfile-copy-hash-selection
Oct 29, 2026
Merged

sipsma merged 3 commits into
mainfrom
sipsma/dockerfile-copy-hash-selection

Conversation

@sipsma

@sipsma sipsma commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

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 --exclude keeps 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 with mkdir without makeParents is unsupported. That case, directly under / of an empty directory, now converts. A cache mount seeded with from= together with uid=, gid= or mode= 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 --exclude copies, symlinks and changes under a COPY; and Dockerfile cache mounts across an engine restart.

@grouville
grouville self-requested a review October 29, 2026 06:28
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
sipsma force-pushed the sipsma/dockerfile-copy-hash-selection branch from b1601d1 to 92adf33 Compare October 29, 2026 06:35
@sipsma
sipsma merged commit d57f71d into main Oct 29, 2026
93 checks passed
@sipsma
sipsma deleted the sipsma/dockerfile-copy-hash-selection branch October 29, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐞 Slow Dockerfile builds after restoring runner snapshots

2 participants