Docker BuildKit Optimizations Cheat Sheet

Leverage caching, secrets, and parallel steps for faster builds

Use BuildKit features like cache imports, secret mounts, and parallel build stages to shrink build time Quick reference guide with examples and best practices. Updated November 2025.

Last Updated: November 21, 2025

Optimization Focus

Feature Why use it
Cache mounts Mount type=cache for package downloads and tools.
Secret mounts Keep credentials out of images with RUN --mount=type=secret.
Multi-stage builds Separate builder dependencies from runtime artifacts.
Concurrent steps BuildKit executes independent steps in parallel.

Build Commands

DOCKER_BUILDKIT=1 docker build --secret id=npmrc,src=.npmrc .
Provide private registry credentials securely.
docker buildx build --cache-from=type=registry,ref=repo:cache --cache-to=type=inline
Push caches for CI reuse.
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
Speed up Python builds.
docker buildx du --progress=plain
Inspect cache layers and size.

Summary

BuildKit speeds up builds by caching downloads, isolating secrets, and running independent steps simultaneously.

💡 Pro Tip: Enable BuildKit by default with DOCKER_BUILDKIT=1 and monitor docker buildx du for cache usage.
← Back to Developer Tools | Browse all categories | View all cheat sheets