Skip to main content

Docker BuildKit

Docker BuildKit is the modern build engine behind docker build. It runs independent build steps in parallel, caches by content, and adds secret mounts, SSH forwarding, and cache mounts. It is the default builder since Docker Engine 23.0.

What BuildKit changes

BuildKit turns a Dockerfile into a dependency graph instead of running steps strictly one by one. Independent stages build in parallel, unused stages are skipped, and caching is based on content. It also introduces build-only features that keep credentials out of image layers.

Enable it on older versions

Bash
DOCKER_BUILDKIT=1 docker build . # single build
docker buildx build . # always BuildKit

On Docker Engine 23.0 and later, docker build uses BuildKit by default.

Features

Dockerfile
# syntax=docker/dockerfile:1
# Secret mount: never stored in a layer or in docker history
RUN --mount=type=secret,id=github_token \
GITHUB_TOKEN=$(cat /run/secrets/github_token) npm ci
# Cache mount: package cache reused between builds, not in the image
RUN --mount=type=cache,target=/root/.npm npm ci --omit=dev
# SSH mount: use your agent for private repositories
RUN --mount=type=ssh git clone git@github.com:myorg/private-repo.git
Bash
docker buildx build --secret id=github_token,src=$HOME/.secrets/github-token -t myapp:1.0 .
docker buildx build --ssh default=$SSH_AUTH_SOCK -t myapp:1.0 .

Exporting cache for CI

Bash
docker buildx build \
--cache-from type=registry,ref=registry.example.com/payment-api:cache \
--cache-to type=registry,ref=registry.example.com/payment-api:cache,mode=max \
-t registry.example.com/payment-api:v3.1.0 --push .
Tip

Add # syntax=docker/dockerfile:1 as the first line when using mounts, so the current Dockerfile frontend is used.

Remember

Use secret mounts for build credentials. Never pass tokens through ENV or ARG, because both can be exposed in image metadata and history.

Frequently Asked Questions

What does BuildKit improve over the legacy builder?

It builds a dependency graph, so independent stages run in parallel and unused stages are skipped. It has more precise content-based caching and adds secret, SSH, and cache mounts that keep credentials and package caches out of layers.

Do I still need to enable BuildKit?

Not on Docker Engine 23.0 or later, where it is the default. On older engines set DOCKER_BUILDKIT=1 or use docker buildx build. Classic Step 1/8 output means the legacy builder is running.

Is BuildKit the same as Buildx?

No. BuildKit is the build engine, and Buildx is the CLI plugin that exposes its full feature set, including multi-platform builds and external cache backends.

Found a mistake, an outdated step or a broken link?