Docker BuildKit: 7 Proven Tips and Secret Leaks to Avoid

Docker BuildKit is the build engine behind docker build, and most of its speed comes from a few habits, not from secret switches. The old version of this post promised a 10x speedup and showed configuration without output. This rewrite measures instead. I ran Docker 29.9.2 (buildx v0.38.1, BuildKit v0.34.0) in a docker:29.9.2-dind container and tested seven features. All numbers and messages below come from that lab; your times will differ with your network and CPU.

1. Docker BuildKit is already on

The old post told you to set DOCKER_BUILDKIT=1 or add "features": {"buildkit": true} to daemon.json. On Docker 29 neither is needed. The lab had no /etc/docker/daemon.json, and the default builder was BuildKit:

$ docker buildx ls
NAME/NODE     DRIVER/ENDPOINT   STATUS    BUILDKIT   PLATFORMS
default*      docker                                 
 \_ default    \_ default       running   v0.34.0    linux/amd64 (+3)

The old builder still exists, but it warns you:

$ DOCKER_BUILDKIT=0 docker build --no-cache -t par:0 .
DEPRECATED: The legacy builder is deprecated and will be removed in a future release.
            BuildKit is currently disabled; enable it by removing the DOCKER_BUILDKIT=0

The Docker BuildKit documentation describes the same default.

2. Parallel stages: the real speedup

I wrote a Dockerfile with three independent stages that each run sleep 6, and a final stage that copies from all three. The legacy builder runs every step in order. Docker BuildKit builds the graph and runs independent stages at the same time:

$ DOCKER_BUILDKIT=0 docker build --no-cache -t par:0 .
Step 12/12 : COPY --from=three /three /three
Successfully tagged par:0
exit 0, 22 s

$ DOCKER_BUILDKIT=1 docker build --no-cache -t par:1 .
#12 naming to docker.io/library/par:1 done
exit 0, 8 s
Docker BuildKit lab: three parallel stages build in 8 s instead of 22 s, and a build secret leaves no trace in the image history
Speed from parallel stages, safety from secret mounts.

22 seconds against 8. That gain comes from the structure of your Dockerfile: a single-stage file has nothing to run in parallel. Split long builds into independent stages, and Docker BuildKit will use the free CPU for you.

3. Order your layers for the Docker BuildKit cache

I built the same small Python app with two Dockerfiles. The "bad" one copies everything before pip install; the "good" one copies requirements.txt first:

FROM python:3.12-alpine
WORKDIR /app
COPY requirements.txt .
RUN pip install -q --root-user-action=ignore -r requirements.txt
COPY . .

After a change to app.py only, the bad file ran pip install again (step #8 [4/4] RUN pip install ... executed), while the good file showed #8 CACHED for the same step. The first bad build took 31.6 seconds, mostly in pip. This simple rule saves more time than any flag. I also tried a pip cache mount (RUN --mount=type=cache,target=/root/.cache/pip); with one small package and --no-cache it did not help in this lab (18.9 s, then 22.0 s), so measure it on your own project before you rely on it.

4. Docker BuildKit secrets: the only safe way

This is the feature that matters most for security. Two common methods leak. A build argument ends up in the image metadata, and an ENV set from it is visible to anyone who pulls the image:

$ docker build -q --build-arg TOKEN=LAB-SECRET-VALUE-789 -f Dockerfile.arg -t s:arg .
$ docker history --no-trunc s:arg | grep -c LAB-SECRET
3
$ docker inspect -f "{{.Config.Env}}" s:arg
[PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin T2=LAB-SECRET-VALUE-789

Copying a file and deleting it in the next step does not help either. The file stays in the earlier layer, and extracting the saved image printed LAB-SECRET-VALUE-789. A secret mount is never written to a layer:

# syntax=docker/dockerfile:1
FROM alpine:3.22
RUN --mount=type=secret,id=mysecret sh -c 'wc -c /run/secrets/mysecret; echo fingerprint $(sha256sum /run/secrets/mysecret | cut -c1-8)' > /build.log
RUN --mount=type=secret,id=api,env=API_TOKEN sh -c 'echo len=${#API_TOKEN}' >> /build.log
$ API_TOKEN=abc123 docker build --secret id=mysecret,src=mysecret.txt --secret id=api,env=API_TOKEN -f Dockerfile.ok -t s:ok .
$ docker run --rm s:ok cat /build.log
21 /run/secrets/mysecret
fingerprint 1cd338a8
len=6

The build could read both secrets, yet docker history found the value 0 times and a search of the saved image found 0 files. The build secrets page documents both the file and the env= form.

One trap: if you forget --secret, the file is simply missing. My step printed wc: /run/secrets/mysecret: No such file or directory and the build went on. Add required=true to make it fail loudly:

#7 ERROR: secret mysecret: not found
ERROR: failed to build: failed to solve: secret mysecret: not found

5. SSH agent forwarding for private repositories

RUN --mount=type=ssh lets a step use your SSH agent without copying a private key into the image. Without an agent, the build stops before it starts:

ERROR: failed to build: failed to convert agent config {default [] false}: invalid empty ssh agent socket: make sure SSH_AUTH_SOCK

With an agent and --ssh default, ssh-add -l inside the step listed my test key (256 SHA256:... lab@buildkit (ED25519)). Without the mount on that step, the same command printed Could not open a connection to your authentication agent. The mount is per step, so add it only where you clone.

6. Export the Docker BuildKit cache for CI

CI runners often start empty, so a local cache is lost after each job. On Docker 29 with the containerd image store, the default driver accepted a local cache export (#11 exporting cache to client directory). With a separate docker-container builder (BuildKit v0.33.1 in the lab) I exported the cache with mode=max, which used 28.5 MB, cleared the builder, and imported it again:

$ docker buildx create --name kit --driver docker-container --use
$ docker buildx build --cache-to type=local,dest=/tmp/bc,mode=max --load -f Dockerfile.good -t e:1 .
$ docker buildx prune -af
$ docker buildx build --cache-from type=local,src=/tmp/bc --load -f Dockerfile.good -t e:2 .
#4 importing cache manifest from local:3127753402948959910
#7 CACHED
#8 CACHED

Every step came from the imported cache, which is exactly what you want from Docker BuildKit in a fresh CI runner.

7. Build files, not only images

Docker BuildKit can write the result of a stage to a folder or a tar stream, which is handy for compiled binaries:

FROM scratch AS export
COPY --from=build /hello /hello

$ docker build --target export --output type=local,dest=./out .
$ ./out/hello
hi
$ docker build --target export -o - . | tar -tv
-rwxr-xr-x 0/0     76480 2026-11-02 07:04:46 hello

Heredocs also work: a RUN <<EOF block with three lines and a COPY <<EOF /etc/app.conf block built fine and wrote the expected files.

Docker BuildKit errors worth knowing

ERROR: failed to build: failed to solve: failed to compute cache key: failed to calculate checksum of ref ...
the --mount option requires BuildKit. Refer to https://docs.docker.com/go/buildkit/ to learn how to build images with Bu...

The first is BuildKit's way to say that a file in COPY does not exist (here /nope.txt). The second appears when someone builds a BuildKit Dockerfile with the legacy builder. Build caches grow fast: the lab had 33 cache records and 193.1 MB after a few builds, and docker builder prune -af cleared 432.9 MB at the end. For Compose-based apps built with these rules, see the docker-mailserver setup post.

FAQ

Will Docker BuildKit make every build 10x faster? No. In this lab, parallel stages gave 22 s to 8 s, and good layer order turned a pip install into a cached step. A single-stage build with no cache hits gains little.

Is DOCKER_BUILDKIT=1 still needed? Not on Docker 29: the default builder already used BuildKit.

Are build arguments safe for passwords? No. The value was found three times in docker history. Use secret mounts.

Comments

Popular posts from this blog

Linux Desktop Frozen? 5 Proven Fixes Before a Hard Reboot

Minecraft Bedrock on Linux: 4 Proven Ways to Play in 2026

DXVK in Wine on Linux: 7 Easy Setup Steps, Tested