Cut Docker Build Times to Under 5 Minutes with BuildKit, Buildx, and Remote Caching
This guide explains how to dramatically speed up Docker image builds by applying Dockerfile best practices, enabling BuildKit, using the Buildx plugin, leveraging remote cache storage, and employing the new --link option for adding files, all achievable within a few minutes.
Dockerfile Best Practices
Key guidelines for writing efficient Dockerfiles:
Containers should be short‑lived.
Minimize the number of image layers.
Use multi‑stage builds to discard build‑time dependencies.
Choose minimal base images (e.g., alpine, distroless).
Avoid installing unnecessary packages.
Run a single process per container.
Sort multi‑line arguments to improve layer caching.
Leverage build cache effectively.
BuildKit
BuildKit is the modern backend that replaces the classic Docker builder. It has been the default in Docker Engine 23.0 and later, providing improved caching, parallel layer construction, and delayed base‑image pulling.
DOCKER_BUILDKIT=1 docker build --platform linux/amd64 . -t myimage:1.0
DOCKER_BUILDKIT=1 docker push myimage:1.0Buildx
Buildx is a Docker CLI plugin that fully exposes BuildKit features and adds the ability to manage multiple builder instances, which is useful in CI/CD pipelines.
docker buildx create --bootstrap --name builder
docker buildx use builderRemote Build Cache
Storing build cache in a remote registry enables different machines (e.g., CI agents) to reuse unchanged layers, dramatically reducing build time.
Basic cache‑from usage (pull the latest image and reuse its layers):
docker pull myimage:latest || true
docker build --platform linux/amd64 . \
-t myimage:1.1 \
-f Dockerfile \
--cache-from myimage:latestWith Buildx you can push and pull cache metadata to/from a registry. The mode=max option stores cache for every layer, while the default mode=min stores only layers present in the final image.
docker buildx build --platform linux/amd64 . \
-t myimage:1.1 --push \
--cache-to type=registry,ref=mycache:1.1,mode=max \
--cache-from type=registry,ref=mycache:1.1Inline cache embeds the cache data directly in the image. This works with plain BuildKit but can be tricky with multi‑stage builds.
docker buildx build --platform linux/amd64 . \
-t myimage:1.1 --push \
--cache-to type=inline,mode=max \
--cache-from myimage:previousDockerfile 1.4 Syntax – --link Option for Adding Files
Dockerfile syntax version 1.4 (declared with #syntax=docker/dockerfile:1.4) adds a --link flag to COPY and ADD. This flag creates a separate snapshot for the added file, eliminating dependency on previous layers and allowing parallel copying.
#syntax=docker/dockerfile:1.4
FROM baseimage:latest
COPY [--chown=<user>:<group>] [--chmod=<perms>] --link binary /opt/Because the file resides in its own layer, unchanged files can be reused even when parent layers change, which speeds up builds, especially in multi‑stage scenarios.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
