✦ For everyone, free.

Practical knowledge for real and everyday life

Home

5.3.1.5 Builder Temporary Files

A focused guide to Builder Temporary Files, connecting core concepts with practical Docker and container operations.

Builder temporary files are intermediate artifacts a builder stage produces along the way to its final output — object files, downloaded archives, partially compiled state — that are never needed by any later stage and can therefore be left entirely behind without any need for explicit cleanup, since the entire builder stage is discarded once its needed output has been copied forward.

Why Cleanup Often Isn't Necessary in a Builder Stage

Because a builder stage's entire filesystem is discarded once a later stage has copied out whatever it actually needs, temporary files left behind in the builder stage have no effect on the final image's size, unlike temporary files left behind in a single-stage build.

FROM golang:1.22 AS builder
WORKDIR /src
COPY . .
RUN go build -o /out/app .

FROM scratch
COPY --from=builder /out/app /app

Any intermediate object files, module caches, or other artifacts the Go build process generates inside the builder stage simply never reach the final image at all, regardless of whether they were explicitly cleaned up.

When Cleanup Within a Builder Stage Still Matters

Cleanup within a builder stage can still matter for build performance — keeping a stage's own layer cache reasonably sized, or avoiding running out of disk space during a particularly large build — even though it has no bearing on the final image's size.

RUN go build -o /out/app . && go clean -cache
Avoiding Unnecessary Effort Spent on Builder Stage Cleanup

Time spent carefully minimizing a builder stage's intermediate footprint is generally better redirected toward ensuring the final stage copies forward exactly (and only) what it needs — the builder stage's own internal tidiness matters far less than what crosses the boundary into the final image.

COPY --from=builder /out/app /app
Why Understanding This Distinction Matters

Recognizing that a builder stage's temporary files are inherently harmless to final image size removes an unnecessary source of premature optimization effort, letting attention focus instead on correctly identifying and copying forward only the genuinely needed final artifact.