[Feature] Ability to inspect specific cache mounts
Description
Following up from: https://github.com/docker/buildx/issues/931
-
Why can't we have something like,
docker buildx du --filter /var/cache/apt? It would allow direct inspecting/clearing of cache mounts. I'm writing some logic to allow deleting of a particular build cache mount if desired. I'm not able to have a clear script for this because of the current support. -
Is it also possible to add more details with
docker buildx du --verbose --filter type=exec.cachemountin order to know which image that particular cache mount is associated with? I don't even understand why there is an "id" field configurable if you can't key off of it in any way. I just changed one of the mounts for a java program's .m2 directory and all I get is 3 entries here and none of them discernable.
~ ❯ docker buildx du --verbose --filter type=exec.cachemount | grep -B 6 -F "/root/.m2" 06:28:51 PM
ID: qd98bx4cmfe7de8fln5nx65ut
Created at: 2023-06-29 21:09:39.551389639 +0000 UTC
Mutable: true
Reclaimable: true
Shared: false
Size: 19.31MB
Description: cached mount /root/.m2 from exec /bin/sh -c ./mvnw test
--
ID: glydhkz1vo25rado1zoq7ajbs
Created at: 2023-06-29 21:35:45.00107996 +0000 UTC
Mutable: true
Reclaimable: true
Shared: false
Size: 1.897MB
Description: cached mount /root/.m2 from exec /bin/sh -c ./mvnw dependency:resolve-plugins
--
ID: 2iq3u28y28xlsagy1esqkpfu6
Created at: 2023-06-29 22:30:07.203308554 +0000 UTC
Mutable: true
Reclaimable: false
Shared: false
Size: 0B
Description: cached mount /root/.m2 from exec /bin/sh -c ./mvnw dependency:resolve-plugins
/assign ✋🏻
Thanks @Azhovan ! This would be fantastic, it's a limitation that has been frustrating.
Shall we have some command like docker buildx cache ls, docker buildx cache inspect qd98bx4cmfe7de8fln5nx65ut docker buildx cache rm qd98bx4cmfe7de8fln5nx65ut things?
I have a build host build images all the day, docker buildx du will take eternal
Sometimes a cache being corrupt, but I cannot just remove that single cache unless build a dockerfile with rm -rf
Shall we have some command like
docker buildx cache ls,docker buildx cache inspect qd98bx4cmfe7de8fln5nx65utdocker buildx cache rm qd98bx4cmfe7de8fln5nx65utthings?I have a build host build images all the day,
docker buildx duwill take eternalSometimes a cache being corrupt, but I cannot just remove that single cache unless build a dockerfile with
rm -rf
I have the exact same use case. All I really need is docker builder prune --filter id=<my_id_defined_in_the_dockerfile> and I'm set.
@Azhovan curious how the progress is going here? This came up again today as a much needed feature.
If there was a --output option to get JSON with other related metadata like the options set for the mount (at least setting/changing mode, gid, uid all seem to create a new mount) in addition to id which uses the target as a fallback value.
So the docker builder du output for cache mounts with ID presumably is computed from that mix of values since you can have more than one defined with the same id but not the same cache mount backing (maybe that's a bug though). If you have the ability to output JSON with that additional metadata, you can identify the associated ID (not id) and target it with prune. The might be a simpler interim solution? 🤔
Sometimes a cache being corrupt, but I cannot just remove that single cache unless build a dockerfile with
rm -rf
If the cache mount doesn't have extra options such as mode, uid, gid assigned (id is fine though), any cache mounts in the Dockerfile would be removed via build --no-cache, otherwise they persist.
If you do have any of those options configured for the mount, since they persist the cache mount across builds but --no-cache invalidates the layers:
- The cache mounts usage count and last used timestamp will be updated... nevermind it doesn't seem that
--filtersupports that an equivalent ofuntil=for last used, so you can only filter by creation date or type? (there is no--filterdocs fordocker buildx du). This would be possible with JSON output 🤷♂️ (with a normalized unit, not human time to compare against) - You could also use and change the
idoption for the cache mount reference in theDockerfile, perhaps via anARGto use--builder-arg? It would not help with removing the specific cache mount, but would allow you to continue with a fresh one at least without requiring you to prune all existing cache mounts.
You could also solve some cache/storage management requirement with GC policies? (along with docker builder prune --keep-storage <bytes> to trim to min size)
There is an active feature request from 2019 for --no-cache-filter to support cache mount filtering, but we only got stage filtering with that feature in Nov 2021.
I missed the example from: https://github.com/docker/buildx/issues/931#issuecomment-1027155165
$ docker buildx du --verbose | grep -B 9 exec.cachemount
ID: oh5dzk0n897717mllhegbo78y
Created at: 2022-01-28 23:49:10.035242944 +0000 UTC
Mutable: true
Reclaimable: true
Shared: false
Size: 90.41kB
Description: cached mount /my/target/path from exec /bin/sh -c <Dockerfile RUN command>
Usage count: 3
Last used: 2022-01-31 17:48:31.401871678 +0000 UTC
Type: exec.cachemount
- Use the
--filteroption to only get cache mounts - Then grep against the
Descriptionline for the target path you want to filter for (matching white-space on each end to avoid false-positives).- NOTE: That doesn't account for other options where the regex pattern may accidentally match a path for a different option.
- Filter that again to just the ID field(s) returned and use
awkto split each line into fields by white-space (-F' ') and output the last field (the cache mount id value,'{print $NF}').
docker builder du --builder default --filter 'type=exec.cachemount' --verbose \
| grep -B7 'Description.* /my/target/path .*' \
| grep ID | awk -F' ' '{print $NF}'
# In this case it will output only one ID that can be given to prune
oh5dzk0n897717mllhegbo78y
# Remove:
docker builder prune --filter 'id=oh5dzk0n897717mllhegbo78y' --force
Note removal may not occur if there is a child dependent upon it?: https://github.com/docker/buildx/issues/931#issuecomment-2130636520
@tonistiigi after seeing the completion notice, I checked out the PR, but I couldn't see any of the support in it.
- Can users now filter for directories such as
/var/cache/apt? - Can users associated cache mounts with images?
@tonistiigi can this issue be reopened? I do not have permissions to do so, and I am pretty sure that PR you linked does not address anything in this issue.
@anthonyalayo Following should work:
docker builder du --filter 'type=exec.cachemount' --filter 'description~=cached\smount\s/var/cache/apt'
Thank you!