ScriptsApr 16, 2026·3 min read

BuildKit — Concurrent, Cache-Efficient OCI Image Builder

BuildKit is the modern container image builder behind docker build and buildx, providing a concurrent DAG-based frontend, cross-platform builds, remote caching, and rootless operation.

TL;DR
BuildKit powers docker build with concurrent DAG-based builds and advanced caching.
§01

What it is

BuildKit is the modern container image builder that powers docker build and docker buildx. It replaces the legacy Docker builder with a concurrent DAG-based build engine that executes independent build steps in parallel. Features include cross-platform builds, remote caching (to registries, S3, or local directories), rootless operation, and efficient layer management.

This tool is for DevOps engineers and platform teams who need faster container builds, multi-architecture images, or advanced caching strategies in CI/CD pipelines.

§02

How it saves time or tokens

BuildKit's concurrent execution builds independent stages simultaneously, reducing total build time. Layer caching avoids rebuilding unchanged layers. Remote caching shares cache across CI runners, so the first build on a fresh runner is still fast. Multi-stage builds with BuildKit are optimized to only execute stages that contribute to the final image.

§03

How to use

  1. Enable BuildKit in Docker or use it standalone.
  2. Write multi-stage Dockerfiles.
  3. Use cache mounts and remote caching.
  4. Build multi-platform images.
# Enable BuildKit in Docker
export DOCKER_BUILDKIT=1

# Or use buildx (BuildKit is the default)
docker buildx build -t myapp:latest .

# Multi-platform build
docker buildx build --platform linux/amd64,linux/arm64 \
  -t myapp:latest --push .

# With remote cache
docker buildx build \
  --cache-from type=registry,ref=myregistry/myapp:cache \
  --cache-to type=registry,ref=myregistry/myapp:cache \
  -t myapp:latest .
§04

Example

A Dockerfile optimized for BuildKit:

# syntax=docker/dockerfile:1

# Stage 1: Dependencies (cached separately)
FROM node:20-slim AS deps
WORKDIR /app
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm \
    npm ci --production

# Stage 2: Build (runs in parallel with deps if independent)
FROM node:20-slim AS build
WORKDIR /app
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm \
    npm ci
COPY . .
RUN npm run build

# Stage 3: Production image
FROM node:20-slim
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
CMD ["node", "dist/index.js"]

The --mount=type=cache directive caches npm packages across builds.

§05

Related on TokRepo

§06

Common pitfalls

  • Cache mount targets are local to the builder. They do not automatically share across CI runners. Use remote caching for CI.
  • Multi-platform builds require QEMU emulation for non-native architectures, which is slower. Native builders are faster.
  • The # syntax=docker/dockerfile:1 header is required for advanced BuildKit features like cache mounts.
  • BuildKit garbage collects old cache by default. Tune --max-old-age and --max-used-space for your needs.
  • Rootless mode has limitations with certain network operations and volume mounts.

Frequently Asked Questions

Is BuildKit the same as docker build?+

BuildKit is the build engine behind docker build since Docker 23.0+. Older Docker versions use the legacy builder by default. Set DOCKER_BUILDKIT=1 or use docker buildx to ensure BuildKit is used.

What is concurrent DAG-based building?+

BuildKit analyzes your Dockerfile as a directed acyclic graph. Independent stages and steps are executed in parallel. For example, if two stages do not depend on each other, they build simultaneously.

How does remote caching work?+

BuildKit can push and pull cache layers to a container registry, S3 bucket, or local directory. CI runners fetch cache from the remote location, avoiding full rebuilds on fresh machines.

Can I build multi-architecture images?+

Yes. Use `docker buildx build --platform linux/amd64,linux/arm64` to build for multiple architectures in one command. BuildKit produces a manifest list that Docker pulls the correct image for each platform.

What are cache mounts?+

Cache mounts persist directories across builds. For package managers (npm, pip, go modules), cache mounts avoid re-downloading dependencies on every build. They are specified with --mount=type=cache in RUN instructions.

Citations (3)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets