0%
Containers and Kubernetes: From cgroups to Operators
What a Container Actually Is
Kubernetes Architecture
Workloads
Scheduling and Resources
Networking and Storage
Extending Kubernetes
The Builder, Cache Mounts and Multi-Stage Builds
The modern builder does not execute a build file top to bottom. It converts the file into a dependency graph, works out what the requested output needs, and evaluates only that, in parallel where it can. Almost every surprising build behaviour follows from this.
From instructions to a graph
The build file is parsed by a frontend into an intermediate representation, which is a directed acyclic graph of operations.
Each node is an operation with inputs: execute a command on a filesystem state, copy a path from one state into another, fetch an image, fetch a remote file. Edges are the dependencies between them. The graph is then solved, meaning the builder walks backwards from the requested output, determines which nodes are needed, checks the cache for each, and executes what is missing.
This is why the file reads like a script and behaves like a declaration. The order of instructions defines dependencies, not execution order, and the builder is free to reorder anything whose dependencies allow it.
Unneeded work is simply not done
Lazy evaluation has visible consequences that confuse people coming from the older builder.
A stage no other stage copies from, and which is not the requested target, is never built. A remote fetch whose result nothing uses is never performed. An instruction in a stage that the target does not depend on does not run, however expensive or important it looks.
So a build file containing a test stage that nothing depends on silently never runs the tests, which is a real and quietly dangerous failure. Making tests run requires either targeting that stage explicitly or having the final stage depend on it, and the second is the robust choice because it cannot be forgotten.
Parallelism comes free, and so does contention
Independent branches of the graph execute concurrently, which is usually a large win and occasionally a problem.
A build with three stages that each fetch dependencies and compile, feeding one final stage, runs all three compilations at once. On a builder with enough cores and memory that cuts wall-clock time substantially. On a constrained builder it causes memory exhaustion that would not happen with sequential execution, and the failure looks random because which stage gets killed depends on timing.
The lever is the builder's concurrency, which can be limited, and per-stage resource awareness, which means not assuming a compilation has the whole machine. A build that passes locally and fails in a constrained pipeline is often this rather than anything about the pipeline's image.
The frontend is selectable, per file
The first line of a build file can name the frontend that interprets it, which decouples build file features from the builder version.
A syntax directive at the top of the file names an image implementing the frontend, and the builder fetches and uses it. That means newer build file features are available without upgrading the builder, and a file can pin the exact frontend version it was written against.
Practically, this is how cache mounts, secret mounts, heredocs and the newer copy options became usable widely. A file using one of those features without the directive fails with a parse error that says nothing about the directive, which is the single most common first encounter with this mechanism.
What the builder considers an input
Knowing exactly what the builder sees explains both cache behaviour and a class of security mistake.
The inputs are the build context, which is the set of files sent to the builder subject to the ignore file, the build arguments, the base images as resolved to digests, and any remote resources fetched. Nothing else. The builder does not see the rest of the repository, environment variables that were not passed as arguments, or the host's filesystem.
The corollary is that anything the build needs must be an explicit input, and anything that is an input is visible to the build and recorded in the result unless specifically handled. That is why passing a credential as a build argument leaks it into the image's configuration, and why the dedicated secret mechanism exists.
Reading the plan before running it
The graph is inspectable, and looking at it is the fastest way to understand a confusing build.
The plain progress output is the important one in a pipeline, because it labels every step with whether it came from cache. A build everyone believes is cached, and whose log shows no cached steps, ends that argument in one command.
The context, and sending less of it
How the context reaches the builder affects both speed and what the build can see, and there are three arrangements.
A local directory is archived and streamed to the builder, so the transfer is proportional to what the ignore file permits. A version control reference tells the builder to fetch the repository itself, so nothing is transferred from the invoking machine and the build has the repository's history available. A named context lets a build take a second context, which is how a build consumes an artefact directory alongside its source.
The arrangement worth knowing is that a build against a version control reference is reproducible in a way a local one is not, because the context is identified by a commit rather than by whatever happens to be in a working tree. For a release build that property is worth the loss of local iteration.
Output is a choice, not just an image
The builder's result does not have to be an image, and the alternatives are genuinely useful.
It can produce an image in the local store, push directly to a registry without a local copy, write an archive, or write the resulting filesystem to a local directory. That last one turns the builder into a general-purpose hermetic execution environment: a build file that compiles something and a local output produces the artefact on disk with no image involved.
The one that saves most time in a pipeline is pushing directly, because it avoids writing the image to a local store and reading it back, which on a large image is a measurable fraction of the pipeline. The one that surprises people is that a build producing no output at all still runs, which is how a test or lint stage is invoked.
Why the older builder behaved differently
A short comparison is worth having, because plenty of material online describes the old behaviour.
The older builder executed instructions in order, created a container per instruction, committed it as a layer, and matched cache by comparing the instruction string and the parent layer. It had no graph, so no parallelism and no lazy evaluation; no cache mounts, so every build re-downloaded; no secret mounts, so credentials went through arguments; and no multi-platform support in one invocation.
Knowing this matters because advice written for it is still common and is often actively wrong now, particularly advice about chaining commands into one instruction to reduce layers. With content-addressed layers, cache mounts and multi-stage builds, the modern answer is usually more instructions rather than fewer.
Where the build actually runs
The builder is a separate service from the client, and knowing that explains several errors.
The client sends the context and the build request to a builder, which may be a container on the same machine, a remote instance, or a pool. The builder has its own filesystem, its own cache and its own network view. A build command referencing a path that exists on the client and not in the context fails, because the builder cannot see the client's filesystem at all.
The same separation explains why a build that needs to reach an internal service needs the builder to have that access rather than the developer's machine, and why a build behind a proxy needs the proxy configured on the builder. Treating the builder as a remote machine, even when it is local, prevents a recurring class of confusion.
A strong answer explains that the builder solves a graph backwards from the requested target rather than executing instructions in order, and derives the consequences: unreferenced stages never build, independent stages run concurrently, and instruction order expresses dependencies rather than sequence. The practical payoff is being able to say why a test stage silently never ran and why a build that works locally fails on a constrained builder.