Cloud Native 7 min read

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.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Cut Docker Build Times to Under 5 Minutes with BuildKit, Buildx, and Remote Caching

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.0

Buildx

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 builder

Remote 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:latest

With 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.1

Inline 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:previous

Dockerfile 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.

Dockerfile --link layer illustration
Dockerfile --link layer illustration
Container OptimizationBuildKitbuildxRemote CacheDockerfile Best Practices
Senior Brother's Insights
Written by

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'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.