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.
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.
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.
How to use
- Enable BuildKit in Docker or use it standalone.
- Write multi-stage Dockerfiles.
- Use cache mounts and remote caching.
- 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 .
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.
Related on TokRepo
- DevOps tools — Container and build tools
- Automation tools — CI/CD automation
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:1header is required for advanced BuildKit features like cache mounts. - BuildKit garbage collects old cache by default. Tune
--max-old-ageand--max-used-spacefor your needs. - Rootless mode has limitations with certain network operations and volume mounts.
Frequently Asked Questions
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.
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.
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.
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.
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)
- BuildKit GitHub— BuildKit is the modern container image builder behind Docker
- Docker BuildKit Docs— BuildKit cache management and remote caching
- Docker Buildx Docs— Multi-platform builds with Docker Buildx
Related on TokRepo
Discussion
Related Assets
NAPI-RS — Build Node.js Native Addons in Rust
Write high-performance Node.js native modules in Rust with automatic TypeScript type generation and cross-platform prebuilt binaries.
Mamba — Fast Cross-Platform Package Manager
A drop-in conda replacement written in C++ that resolves environments in seconds instead of minutes.
Plasmo — The Browser Extension Framework
Build, test, and publish browser extensions for Chrome, Firefox, and Edge using React or Vue with hot-reload and automatic manifest generation.