buildx icon indicating copy to clipboard operation
buildx copied to clipboard

[Feature] Ability to inspect specific cache mounts

Open anthonyalayo opened this issue 2 years ago • 6 comments

Description

Following up from: https://github.com/docker/buildx/issues/931

  1. 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.

  2. Is it also possible to add more details with docker buildx du --verbose --filter type=exec.cachemount in 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

anthonyalayo avatar Jun 29 '23 22:06 anthonyalayo

/assign ✋🏻

Azhovan avatar Dec 29 '23 23:12 Azhovan

Thanks @Azhovan ! This would be fantastic, it's a limitation that has been frustrating.

anthonyalayo avatar Jan 25 '24 08:01 anthonyalayo

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

dixyes avatar Feb 05 '24 03:02 dixyes

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

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.

anthonyalayo avatar Feb 05 '24 03:02 anthonyalayo

@Azhovan curious how the progress is going here? This came up again today as a much needed feature.

anthonyalayo avatar May 08 '24 18:05 anthonyalayo

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 --filter supports that an equivalent of until= for last used, so you can only filter by creation date or type? (there is no --filter docs for docker 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 id option for the cache mount reference in the Dockerfile, perhaps via an ARG to 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 --filter option to only get cache mounts
  • Then grep against the Description line 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 awk to 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

polarathene avatar Aug 22 '24 04:08 polarathene

@tonistiigi after seeing the completion notice, I checked out the PR, but I couldn't see any of the support in it.

  1. Can users now filter for directories such as /var/cache/apt?
  2. Can users associated cache mounts with images?

anthonyalayo avatar Aug 29 '25 22:08 anthonyalayo

@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 avatar Aug 30 '25 19:08 anthonyalayo

@anthonyalayo Following should work:

docker builder du --filter 'type=exec.cachemount' --filter 'description~=cached\smount\s/var/cache/apt'

crazy-max avatar Aug 31 '25 06:08 crazy-max

Thank you!

anthonyalayo avatar Aug 31 '25 07:08 anthonyalayo