Skip to content

Docker caching

Arvid Jakobsson requested to merge arvid@caching into master

Now builds on !460 (merged) adding merge request pipelines and a workflow definition.


An attempt to revive the work David started here:

This MR attempts to enable Docker buildkit's inline layer cache. By passing --build-arg=BUILDKIT_INLINE_CACHE=1 when building images, a cache is created within the image itself. Then, using --cache-from IMG we specify a previous image from where to fetch the given cache. This seems to work, in the sense that I see some instance of CACHED in the log.

In particular, we use the images from CI_MERGE_REQUEST_DIFF_BASE_SHA as cache. This corresponds to the images built for the commit from which the current branch was created. For a rebased branch, this is HEAD on master. To get access to this variable, I enable merge request pipelines. Mysteriously, this variable is sometimes empty on merge request branches. When this happens, or on branch pipelines, the --cache-from argument will refer to a non-existing tag, which emits a warning with no adverse effects.

I've tested the caching in !453 (closed). It is far from perfect. I get cache misses that I cannot seem to explain. For instance, here the opam package install stage is not-cached in build-runtime-arm64, yet it's a cache hit in the amd64 counterpart. Yet, we can also see that it sometimes work. Not sure whether we want to have something that works intermittently for mysterious reasons (even though nothing bad happens when it doesn't work).

Disabling the cache

We need to a way to manually force rebuilds and skip the cache. For instance, to obtain package updates.

This can be achived either by adding the label ci--no-cache to an MR, or by setting the DOCKER_NO_CACHE environment variable.

Normal caching

I also tried normal layer caching, exactly as described here with no results. Here is a pipeline where the first job creates and pushes and image. The second job pulls the same image, and then rebuilds it with the previous image as --cache-from. Before both builds, I clean all local caches. Yet, we see no instances of CACHED in the logs of the second job.

Impact on image size

Difference in image size seems to be negligible.

Edited by Arvid Jakobsson

Merge request reports