'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:
- where is that cache stored?
- how can one delete/inspect that?
- 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?
- the ID option is still ambiguous. If I specify the same ID in different dockerfiles, will they refer to the same cache?
Solution 1:[1]
Yes, it is somewhat vague in docker 20.10.5. Could use a pull request or two to update documentation.
- The cache uses the same storage driver as used for image layers. Metadata is stored in databases at
/var/lib/docker/buildkit. When usingoverlay2driver the layer itself is in/var/lib/docker/overlay2/<ID>/diff/. For<ID>, see below./var/lib/dockercan vary depending ondata-rootin yourdockerdconfiguration. docker buildx du --verboselists build cache. You can also inspect it fromdocker system df -v --format '{{ .BuildCache | json }}'. The cache typeexec.cachemountis theRUN --mount type=cache. You can find the layer using theID, which is not the same as used in--mount id. The mount type is implemented by buildkit, so thedocker run --mountdoes not recognize it. To get rid of it eitherdocker buildx pruneordocker build --no-cache.- The cache key is the value from
id=.iddefaults to value oftarget. You need to specifyidwhen you need different cache at the sametarget. - Yes. They are the same cache regardless of the
targetor Dockerfile. Different builders have their own caches, which keeps for example caches for different architectures separate.
Solution 2:[2]
For #3 it seems when you don't specify an id to the --mount option, the same cache will be used, regardless of any other parameters, like the mountpoint or the Dockerfile contents/path.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | user582175 |
