Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker buildkit cache location/size and ID

There's Docker buildkit: https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md which has an extra option to RUN (amongst others): the --mount=type=cache.

What I couldn't figure out from the documentation is:

  1. where is that cache stored?
  2. how can one delete/inspect that?
  3. how does buildkit decide what cache goes where? For example if I have the same Dockerfile in two locations, will the caches be the same? What's the key for the cache?
  4. the ID option is still ambiguous. If I specify the same ID in different dockerfiles, will they refer to the same cache?
like image 445
user582175 Avatar asked Aug 31 '25 04:08

user582175


1 Answers

Yes, it is somewhat vague in docker 20.10.5. Could use a pull request or two to update documentation.

  1. The docker driver cache uses the same storage driver as used for image layers. Metadata is stored in databases at /var/lib/docker/buildkit. When docker uses overlay2 storage driver, the layer is in /var/lib/docker/overlay2/<ID>/diff/. For <ID>, see below. /var/lib/docker can vary depending on data-root in your dockerd configuration. Builders using docker-container or kubernetes driver keeps the data on a volume.
  2. docker buildx [--builder name] du --verbose lists build cache. You can also inspect the docker driver caches from docker system df -v --format '{{ .BuildCache | json }}'. The cache type exec.cachemount is the RUN --mount type=cache. You can find the layer using the ID, which is not the same as used in --mount id. The mount type is implemented by buildkit, so the docker run --mount does not recognize it. To get rid of it either docker buildx prune or docker build --no-cache.
  3. The cache key is the value from id=. id defaults to value of target. You need to specify id when you need different cache at the same target.
  4. Yes. They are the same cache regardless of the target, Dockerfile or platform. Different builders have their own caches.
like image 50
Marko Kohtala Avatar answered Sep 02 '25 19:09

Marko Kohtala