I've used BoringCache to build BoringCache from the start. The current version builds the next one, so slow or awkward cache behaviour tends to show up in my own work first. I hit a limit in my cache setup when I started building the Rust CLI inside Docker. I'd mostly avoided Docker on my laptop because I found its cache hard to inspect and the disk usage kept growing. But the CLI release needed Linux binaries linked against musl for Alpine and similar systems, so I built that part inside Docker.
A Docker build isn't always expensive. A small application may only install a few dependencies and compile some assets. It's different when one step compiles a Rust binary, FFmpeg, libvips or another large piece of software, because that one command can take most of the build time. Outside Docker, I could already reuse Cargo target state and sccache. Inside Docker, the same compile was slow again whenever its layer had to run. I wanted to reuse the tool's own cache when a Docker layer missed, so the command wouldn't have to repeat all its work.
The expensive work was inside one layer
When a Docker layer hits, BuildKit skips the instruction and uses the result it already has. When it misses, the command runs again, and later layers that depend on it may have to run again too. That's the correct behaviour, and Docker's cache invalidation guide explains the rules in detail. It's usually fine for a quick command, but you notice it when one instruction holds most of the build time:
RUN cargo build --release --locked
Cargo is one example. The same thing happens when a build compiles FFmpeg, libvips or another C library, or when Gradle runs a large task graph. A source or build-file change may correctly invalidate the Docker layer, but that doesn't make every compiler result or task output inside the command useless. If the command takes a few seconds, I'd let it run. If it takes five or ten minutes, the work inside it is worth looking at separately.
The first attempt invalidated the layer
I already had the BoringCache archive commands, so my first attempt was to put the CLI inside the Dockerfile and restore a cache around the slow command. It worked, but every new BoringCache release changed the binary in the Dockerfile, which changed the layer key and made the dependent layers rebuild. I was saving a smaller piece of work by throwing away the bigger Docker layer cache. I wrote about that experiment in Docker build done. Still exporting cache…. What I needed was a way to work with BuildKit without making BoringCache part of the image or its layer keys.
Layer, mount and tool caches
I had been treating Docker cache as one thing. It helped to separate three different questions.
- Docker layer cache
- Can BuildKit skip this instruction and reuse its complete filesystem result?
- BuildKit cache mount
- When the instruction runs, can a directory such as Cargo
target/or a package-manager store keep useful state from an earlier build? - Native tool cache
- When the tool asks for a particular compiler invocation or task result, can sccache, ccache, Gradle or another supported protocol serve it?
The layer cache is still the first thing I want to hit. The other two matter after BuildKit decides that the command has to run.
Cache mounts stay with the builder
BuildKit cache mounts already let a RUN reuse a mutable directory between invocations on the same builder. That works very well on a persistent machine, because the directory is already there, but an ephemeral runner loses the builder when the job ends. BuildKit's mount contents are also not included in its exported layer cache, so the next fresh builder can't get them back through ordinary cache-to and cache-from alone. --mount-cache makes selected cache mounts portable: BuildKit keeps them on one builder, and BoringCache makes them available to the next one. You keep the normal RUN --mount=type=cache in the Dockerfile.
Tool caches inside the build
The other part was the cache protocol the tool already understands. sccache and ccache can reuse compiler results, and Gradle, Nx, Turbo, Maven, Bazel and Go can ask for their own build or task results. They work in smaller units than a Docker layer, because the tool asks for the exact work it needs while the command runs. For Rust, target state and sccache solve different parts of the build, and Rust build cache is three caches, not one goes into that in more detail.
$ boringcache docker --mount-cache --tool-cache sccache
The Docker adapter starts the selected tool cache beside the build and injects its settings only while the build runs, so the credentials stay out of the Dockerfile, the layer keys and the final image. BuildKit still checks the layer, Cargo still checks freshness and sccache still makes the compiler key. BoringCache just makes the reusable data available where those tools expect it.
Three tests
I didn't want to add more cache options just because they sounded useful, so I tried to answer three smaller questions.
Can the build stay warm with layers off?
I wanted to know whether the inner caches saved build time when Docker couldn't skip the instructions through layer reuse. Mastodon was a good test, because its image builds Ruby and frontend assets and also compiles libvips and FFmpeg. In one paired run I turned off Docker layer reuse for the inner-cache lane. The mount caches still restored package state, and ccache served the native compilation. The upstream control also had layer reuse turned off, but no portable mount or compiler cache.
linux/amd64 upstream cache:false 8m56s no layers + ccache + mount caches 3m11s ccache 2,216 / 2,412 hits linux/arm64 upstream cache:false 6m20s no layers + ccache + mount caches 2m14s
Those are provider-step times from one Mastodon run, not a promise for every Dockerfile. Every Docker instruction was allowed to run, and the native compilation still reused cached results. The benchmarks have current numbers for the same idea on llama.cpp, whose server image compiles C and C++ in one layer.
Do inner caches replace the layer cache?
The Immich base images answered that. The run built four cells, dev and prod images on AMD64 and ARM64. The optimized BoringCache lane used layers, ccache and mount caches. Then I repeated the same rebuild with Docker layers turned off but kept ccache and the mounts.
layers + ccache + mounts 24m40s no layers + ccache + mounts 29m45s · 21% slower GHCR registry cache 39m14s · no-layer lane 24% faster
The no-layer lane didn't beat a warm layer cache, and I wouldn't expect it to. It came within about five minutes across the four build cells, and it finished roughly nine and a half minutes ahead of the registry-cache run. The comparison recorded all four cells, and final-image push was turned off in all three lanes. So inner caches aren't a replacement for Docker layers. They let tools reuse cached results when a valid change makes the layer miss.
Does this work beyond compiler caches?
The idea isn't limited to compiler caches. Gradle can reuse task outputs too. In a Stirling-PDF build, the Docker adapter started a Gradle cache beside the build:
$ boringcache docker --tool-cache gradle:stirling-gradle -- docker buildx build ... > Task :compileRestartHelper FROM-CACHE > Task :common:compileJava FROM-CACHE > Task :proprietary:compileJava FROM-CACHE > Task :stirling-pdf:compileJava FROM-CACHE 27 actionable tasks: 21 executed, 4 from cache, 2 up-to-date
In the ultra-lite image job, Gradle answered from cache inside Docker. Twenty-one tasks still executed, and the rest of the image still had to build.
Measure cache mounts
I'd check the complete build time before turning on a cache mount. A mount takes time to restore and, on a trusted build, time to save again. If the command can recreate the directory in a few seconds, moving it over the network may be slower than doing the work. I found that with small package-manager caches: some were technically reusable but didn't improve the complete build, so they stay off by default. Moving a mount between builders makes more sense when rebuilding the directory would take minutes: a large Cargo target, an expensive native build or another workload where the measurements show restoring takes less time.
Tool-cache hits also only remove that tool's work. ccache doesn't remove configure or link time, sccache doesn't stop Cargo resolving dependencies, and Gradle may restore some tasks and still run many others. A hit count is useful, but the overall build time is still the number that matters.
Layer cache stays on
I started this because BoringCache's own musl builds were slow inside Docker. I didn't need to replace Docker's layer cache. I needed to stop treating the layer as the only reusable unit in the build. The normal path is still simple: keep the BuildKit layer cache on, add a portable mount when a directory takes long enough to rebuild, and add a native tool cache when its hits cut total build time. When the layer has to run again, the cached results from the tools inside it can still be reused.