✦ For everyone, free.

Practical knowledge for real and everyday life

Home

5.2.1.1 BuildKit Performance

A focused guide to BuildKit Performance, connecting core concepts with practical Docker and container operations.

BuildKit performance refers to the concrete build-speed improvements BuildKit provides over the legacy builder, arising primarily from its ability to execute independent build steps in parallel and from its more efficient handling of the build cache.

Parallel Execution of Independent Stages

When a Dockerfile defines multiple stages that do not depend on each other, BuildKit can build them concurrently rather than strictly in sequence, directly reducing total build time for Dockerfiles structured this way.

FROM node:20 AS frontend
RUN npm run build

FROM golang:1.22 AS backend
RUN go build -o server .

FROM alpine
COPY --from=frontend /app/dist /static
COPY --from=backend /app/server /server
time docker build -t myapp .

Measuring build time for a Dockerfile structured this way, compared to an equivalent single-stage version performing the same work sequentially, demonstrates the concrete time savings parallelism provides.

More Efficient Cache Handling

BuildKit's cache management is generally more precise about exactly what invalidates a given cached layer, sometimes preserving cache validity in situations where the legacy builder would have unnecessarily invalidated it.

docker build --progress=plain -t myapp .

Detailed build output can reveal exactly which steps were served from cache, useful for confirming BuildKit's caching behavior in a specific build.

Reduced Overhead From Streaming Build Context

BuildKit streams the build context more efficiently than the legacy builder, which can reduce the perceived overhead of transferring a large build context, particularly noticeable for projects with substantial context sizes.

docker build -t myapp .
Why BuildKit's Performance Improvements Matter

For Dockerfiles with genuine opportunities for parallelism — multiple independent build stages — BuildKit's performance benefit can be substantial and directly measurable, making it worthwhile to structure multi-stage builds with this parallelism potential specifically in mind rather than purely for the size or separation-of-concerns benefits multi-stage builds also provide.