Speed Up Docker Build Times Using BuildKit Cache Export and Inline Build Arguments

User avatar placeholder
Written by Tamzid Ahmed

June 10, 2026

Developers constantly battle slow Docker builds, especially in CI environments where each minute adds cost. Speed up Docker build times by leveraging BuildKit’s cache export and inline build arguments, two techniques that turn a sluggish pipeline into a lean, repeatable process.

Understanding Docker BuildKit and Why It Matters for Speed

What is BuildKit?

BuildKit is Docker’s next‑generation build engine, introduced in Docker 18.09. It runs builds in parallel, isolates each step, and offers sophisticated caching mechanisms that far outpace the classic builder.

How BuildKit Improves Caching

Traditional Docker builds reuse layers only when the exact Dockerfile instruction and its context match. BuildKit adds a content‑addressable cache, allowing you to reuse intermediate results across separate builds, even when the Dockerfile changes slightly.

Exporting the Build Cache to Reuse Across Builds

Enable BuildKit and Export Cache

First, enable BuildKit by setting the environment variable DOCKER_BUILDKIT=1. Then use the --cache-to flag to push the cache to a remote location (e.g., a local directory or a registry).

Step‑by‑Step Cache Export Command

  1. Run the build while exporting the cache:
    DOCKER_BUILDKIT=1 docker build 
      --target final 
      --cache-to=type=local,dest=./build-cache 
      -t myapp:latest .
  2. In subsequent builds, import the cache:
    DOCKER_BUILDKIT=1 docker build 
      --cache-from=type=local,src=./build-cache 
      -t myapp:latest .
  3. Optional: Export to a registry for team‑wide sharing:
    DOCKER_BUILDKIT=1 docker build 
      --cache-to=type=registry,ref=myrepo/cache:latest 
      -t myapp:latest .

By persisting the cache, identical layers are fetched instantly instead of being rebuilt, shaving minutes off each run.

Using Inline Build Arguments for Faster Layer Reuse

Inline ARG vs. Dockerfile ARG

Typical Dockerfiles declare ARG values near the top, causing a cache miss whenever the argument changes. Inline arguments—passed directly with --build-arg on the command line—let you keep static layers untouched while only the variable portion rebuilds.

Practical Example

Suppose you have a Node.js project that downloads dependencies based on an APP_ENV flag. Instead of hard‑coding the ARG:

ARG APP_ENV=production
RUN npm install --only=${APP_ENV}

run the build like this:

DOCKER_BUILDKIT=1 docker build 
  --build-arg APP_ENV=development 
  -t myapp:dev .

The earlier layers (base image, OS packages) stay cached, and only the npm install step re‑executes when you switch environments.

Combine Cache Export with Inline Arguments: Full Workflow

  1. Enable BuildKit globally in your CI config: export DOCKER_BUILDKIT=1.
  2. Export the cache after the first successful build (optional registry push).
    docker build --cache-to=type=registry,ref=myrepo/cache:latest -t myapp:latest .
  3. When rebuilding, import the cache and supply inline args that differ per branch or feature:
    docker build
    --cache-from=type=registry,ref=myrepo/cache:latest
    --build-arg APP_ENV=staging
    -t myapp:staging .
  4. Verify cache hit rate with the BuildKit log (--progress=plain) to ensure layers are being reused.
  5. Repeat for subsequent commits; only layers affected by changed args will re‑run.

Best Practices and Trade‑offs

  • Use a dedicated cache location. Mixing build caches with other artifacts can cause accidental invalidation.
  • Keep Dockerfile steps deterministic. Random timestamps or non‑cached commands defeat the cache export.
  • Limit cache size. Remote registries may charge storage fees; prune old caches with docker builder prune.
  • Prefer multistage builds. Export only the final stage’s layers to avoid bloating the cache with build‑time tools.
  • Test locally before CI. Run a full export/import cycle on your workstation to spot missing dependencies.

Conclusion

By mastering speed up Docker build times through BuildKit cache export and inline build arguments, you turn each build into a near‑instant operation. Export the cache once, import it on every CI run, and push only the changed arguments. The result is a leaner pipeline, lower cloud costs, and happier developers. Start exporting your cache today and watch your build minutes drop dramatically.

Leave a Comment