16. BuildKit: Next-Generation Docker Builds
Faster, More Secure, More Flexible Builds
BuildKit is Docker's modern build engine, enabled by default since Docker 23.0. It brings parallel builds, secret mounts (no secrets in layers!), cache mounts, SSH forwarding, and multi-platform builds.
Enabling BuildKit and Basic Usage
BuildKit is now the default build engine in Docker 23.0+. If you need to enable it explicitly, set `DOCKER_BUILDKIT=1` environment variable. BuildKit's output is cleaner and more informative than the classic builder. It also supports `--progress=plain` for raw output (useful for CI logs) and `--progress=auto` for fancy TTY output. BuildKit also introduces new Dockerfile syntax via the `#syntax` directive, allowing you to use experimental features.
# Enable BuildKit (if not already default) export DOCKER_BUILDKIT=1 docker build -t myapp . # Build with plain output (for CI logs) docker build --progress=plain -t myapp . # Use specific BuildKit syntax version # syntax=docker/dockerfile:1.4 # Build using docker buildx (BuildKit's CLI) docker buildx build -t myapp .
Secret Mounts: No More Secrets in Layers
Traditional Docker builds required secrets (NPM tokens, SSH keys) to be present in the build context or passed as build args — both of which end up in image layers. BuildKit secret mounts inject secrets during the build without persisting them. Secrets are passed at build time with `--secret` flag and mounted in the container at `/run/secrets/`. This is the only secure way to use private NPM registries, GitHub Packages, or any authentication during build.
# Dockerfile with secret mount
# syntax=docker/dockerfile:1.4
FROM node:20-alpine
# Secret mount for .npmrc (never persisted to image)
RUN --mount=type=secret,id=npmrc \
cp /run/secrets/npmrc /root/.npmrc && \
npm ci --only=production && \
rm /root/.npmrc
COPY . .
CMD ["node", "app.js"]
# Build with secret file echo "//registry.npmjs.org/:_authToken=secret" > .npmrc docker build --secret id=npmrc,src=.npmrc -t myapp . rm .npmrc # Delete after build
Cache Mounts: Speeding Up Builds Dramatically
Layer caching helps, but package managers like `npm`, `pip`, and `apt` store their own caches that are invalidated when dependencies change. Cache mounts allow you to persist package manager caches across builds, even when the dependency files change. This dramatically speeds up builds in CI environments where the builder is ephemeral.
# npm cache mount
RUN --mount=type=cache,target=/root/.npm npm ci
# pip cache mount
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
# apt cache mount (Debian/Ubuntu)
RUN --mount=type=cache,target=/var/lib/apt/lists apt-get update && \
apt-get install -y curl
# Go module cache mount
RUN --mount=type=cache,target=/go/pkg/mod go mod download
Multi-Platform Builds with Buildx
Your development machine is likely x86_64 (amd64), but production might run on ARM (AWS Graviton, Raspberry Pi, Apple Silicon). Buildx can build images for multiple platforms simultaneously, using QEMU emulation if needed. For CI, use a builder that supports both platforms and `--push` to the registry directly, avoiding the need to transfer large images between builder machines.
# Create a builder that supports both amd64 and arm64 docker buildx create --name multiarch --use docker buildx inspect --bootstrap # Build and push multi-platform image docker buildx build --platform linux/amd64,linux/arm64 \ -t myregistry/myapp:latest \ --push . # Build specific platform only docker buildx build --platform linux/arm64 -t myapp:arm64 .
Knowledge Check
Ready to test your understanding of 16. BuildKit: Next-Generation Docker Builds?