Repository navigation
[backport-0.21] dockerBuild: don't hash and keep the whole image on every COPY - #15040
Merged
sipsma merged 3 commits intoNov 3, 2026
Merged
Conversation
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. Backported from #14940. Besides the reads, each of those result hashes kept a hash record of the whole image in the engine's memory for as long as the copy's snapshot stayed cached, so engines running many Dockerfile builds grew by one such record per COPY. Signed-off-by: Erik Sipsma <[email protected]>
Checksumming a path that is not yet in a ref's cache context walked the
path's parent directory recursively. For a file at the root of a
container filesystem, such as Container.file("/stamp") on an exec
output, that is a walk of the whole filesystem: about 36,000 entries
for a Go toolchain image, once per exec snapshot.
When the resolved path is not a directory, record only that path. The
record and its digest are the same ones the walk produced. Directory
records are still only added by walks, so a directory in the tree still
has all of its contents, and later directory or sibling checksums scan
as before.
Backported from #14555. On 0.21, the whole-filesystem walk also stayed in
the engine's memory as a hash record for as long as the snapshot stayed
cached: a Dockerfile COPY --from of one file at the root of a build stage
recorded that stage's whole filesystem.
Signed-off-by: Erik Sipsma <[email protected]>
grouville
self-requested a review
November 3, 2026 02:04
grouville
approved these changes
Nov 3, 2026
The committed token was revoked, so the private Git tests failed to authenticate. The new fine-grained token only has read access to the contents of grouville/daggerverse-private. Backported from #14963. 0.21 has no private-repo test in lockfile_test.go, so only these two files carry the token here. Signed-off-by: Guillaume de Rouville <[email protected]> Signed-off-by: Erik Sipsma <[email protected]>
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.
Backport to 0.21 of two fixes from main, plus a test-token update, one commit each:
COPYon what it copies, not its whole result.What this changes for 0.21 users
Since 0.21.8 (#13765), every Dockerfile
COPYgets its cache identity by content hashing the directory it produces. That directory is the stage's whole root filesystem. So eachCOPYthat runs reads every file of the image built so far. It also leaves a hash record of the whole image in the engine's memory, which stays there for as long as the copy stays in the cache. Engines that run many Dockerfile builds grow by one whole-image record perCOPY, until the cache prunes the copy or the engine restarts. Go's GC can't free these records, and the engine's cache metadata estimate doesn't count them. A user on 0.21.11 reported engines on Kubernetes growing to about 35 GB over a day and getting OOM-killed. In their heap profile, these records were 6.1 GB of a 6.9 GB live heap.With this backport:
COPYis keyed on its parent, its arguments and the content of its sources, as BuildKit does. It no longer reads or records the image it copies onto.COPY --fromof a single file no longer records the source stage's whole filesystem.A filtered
COPY --excludestill hashes its result, as it does on main.Measurements
Three Dockerfile targets build in parallel per round on a
node:22-bookwormbase, with one copied file changed each round. Each reading is the engine's live heap after a forced GC.COPYper target, 10 roundsCOPY --from=builder /out.txtfrom a stage that changes each round, 6 roundsTests
TestDockerBuildCopyDoesNotReadImagefails on its deadline.-race.sdkcore.NewQuery.