Skip to content

[backport-0.21] dockerBuild: don't hash and keep the whole image on every COPY - #15040

Merged
sipsma merged 3 commits into
backport-0.21from
sipsma/backport-0.21-dockerfile-copy-memory
Nov 3, 2026
Merged

sipsma merged 3 commits into
backport-0.21from
sipsma/backport-0.21-dockerfile-copy-memory

Conversation

@sipsma

@sipsma sipsma commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Backport to 0.21 of two fixes from main, plus a test-token update, one commit each:

What this changes for 0.21 users

Since 0.21.8 (#13765), every Dockerfile COPY gets its cache identity by content hashing the directory it produces. That directory is the stage's whole root filesystem. So each COPY that 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 per COPY, 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:

  • A plain COPY is 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.
  • A COPY --from of a single file no longer records the source stage's whole filesystem.

A filtered COPY --exclude still hashes its result, as it does on main.

Measurements

Three Dockerfile targets build in parallel per round on a node:22-bookworm base, with one copied file changed each round. Each reading is the engine's live heap after a forced GC.

Workload Engine Hash records in memory Live heap after the last round
2 plain COPY per target, 10 rounds v0.21.11 +41.2 MB per round 726 MB
same v1.0.0-beta.23 (has #14940) none 53 MB
adds COPY --from=builder /out.txt from a stage that changes each round, 6 rounds v0.21.11 +68.7 MB per round 787 MB
same this branch, first commit only +2 MB per round 101 MB
same this branch none 57 MB

Tests

  • The TestDockerfile suite passes on this branch. With the first commit's code reverted, the new TestDockerBuildCopyDoesNotReadImage fails on its deadline.
  • The engine/contenthash unit tests pass with -race.
  • Both commits applied cleanly. The only adaptation is in the integration test, which uses 0.21's Go SDK client instead of sdkcore.NewQuery.

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
grouville self-requested a review November 3, 2026 02:04
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]>
@sipsma
sipsma merged commit 3040ab3 into backport-0.21 Nov 3, 2026
77 checks passed
@sipsma
sipsma deleted the sipsma/backport-0.21-dockerfile-copy-memory branch November 3, 2026 03:36
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.

2 participants