docker / buildx

Docker CLI plugin for extended build capabilities with BuildKit
Apache License 2.0
3.54k stars 478 forks source link

[Feature] Ability to inspect cache mounts #931

Closed Aposhian closed 2 years ago

Aposhian commented 2 years ago

While docker buildx prune can be used to delete cache mounts with docker buildx prune --filter type=exec.cachemount, there is no way to look at the active list of cache mounts from the buildx CLI, or a good way to manage and delete cache mounts individually.

Yes, builds should not be dependent on the contents of the cache mounts, but in practice when debugging and creating a new build, mistakes can introduce bad data into the cache mounts.

tonistiigi commented 2 years ago

docker buildx du --verbose, you can delete individually by filtering by ID

Aposhian commented 2 years ago

Ok, that is helpful, thank you, although all the cache mount ids are random strings. Shouldn't they default to a machine readable version of the target directory, as is suggested in the docs?

Optional ID to identify separate/different caches. Defaults to value of target.

tonistiigi commented 2 years ago

Shouldn't they default to a machine readable version of the target directory, as is suggested in the docs?

That's not the same thing. id key in mount doesn't actually need to be unique. For example, with sharing=private you can have multiple mounts with the same id if they are used in parallel.

Aposhian commented 2 years ago

Ok. So how do I correlate the cache mounts in my Dockerfile with the output of docker buildx du --verbose? How do I get from --mount=type=cache,target=/var/cache/apt,sharing=locked to something like

ID:             0em48gkezupcayt83f9c9pzmr
Parent:         qpyv30sgntv1k4vr1b2xm6nyz
Created at:     2022-01-31 17:21:44.833173217 +0000 UTC
Mutable:        false
Reclaimable:    true
Shared:         true
Size:           0B
Description:    fileop target
Usage count:    1
Last used:      2022-01-31 17:22:35.445416281 +0000 UTC
Type:           regular
tonistiigi commented 2 years ago

"Type: regular" and "Description: fileop target" should tell you that this is not the cache mount you are looking for.

Aposhian commented 2 years ago

Ok thanks. So I can do docker buildx du --verbose | grep -B 9 exec.cachemount so see all my cache mounts. And then the target name is buried in there. I suppose that works.

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 <target-directory> 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
agirault commented 5 months ago

you can delete individually by filtering by ID

Been trying to do this for a while now. Can you indicate how?

Or another way? or to delete per type (ex: NOT exec.cachemount)?

tonistiigi commented 5 months ago

Can you indicate how?

docker buildx prune --filter 'id=8slvquo3v2gfyczzms23fgn8m'
docker buildx prune --filter 'type=exec.cachemount'

ex: NOT exec.cachemount

For negative filter you need https://github.com/docker/buildx/pull/2473

agirault commented 5 months ago

docker buildx prune --filter 'id=8slvquo3v2gfyczzms23fgn8m'

Edit: doesn't seem to work:

$ docker buildx du --verbose | grep y4dorrugwl7ylt6wgnnwyqdib -A10
ID:             y4dorrugwl7ylt6wgnnwyqdib
Parent:         bxcnk6jdticdlpo2hujaya7ni
Created at:     2024-05-24 23:49:17.936868848 +0000 UTC
Mutable:        false
Reclaimable:    true
Shared:         true
Size:           11.04GB
Description:    ****
Usage count:    1
Last used:      About an hour ago
Type:           regular
$ docker buildx prune --filter 'id=y4dorrugwl7ylt6wgnnwyqdib'
WARNING! This will remove all dangling build cache. Are you sure you want to continue? [y/N] y
Total:  0B

Docker version 26.1.2, build 211e74b


For negative filter you need https://github.com/docker/buildx/pull/2473

Thank you @tonistiigi , seems adequate to address exactly my issue:

ERROR: rpc error: code = Unknown desc = failed to parse prune filters [type!==exec.cachemount]: filters: parse error: [type >|!|< ==exec.cachemount]: unsupported operator "!==": invalid argument

would be nice if --filter could be applied on docker builder du fwiw, would be more robust than grep -B 9 exec.cachemount

tonistiigi commented 5 months ago

would be nice if --filter could be applied on docker builder du fwiw,

You can, same options.

docker builder du --filter 'type=regular' --verbose
docker builder du --filter 'type!=regular' --verbose
tonistiigi commented 5 months ago

Edit: doesn't seem to work:

A common for such record could be that this record has a child record on top of it. In that case you can only delete it if you delete the child as well.

agirault commented 5 months ago

docker builder du --filter 'type=regular' --verbose

ty, not sure what I did wrong the first time... docker builder du --filter 'type=exec.cachemount' --verbose is probably better @Aposhian

docker builder du --filter 'type!=regular' --verbose

I get this, which I assume expected without https://github.com/docker/buildx/pull/2473

ERROR: failed to call diskusage: failed to parse diskusage filters [type!==regular]: filters: parse error: [type >|!|< ==regular]: unsupported operator "!==": invalid argument

A common for such record could be that this record has a child record on top of it

Any good way to check? grep doesn't show any other record referencing this one (was looking for Parent: y4dorrugwl7ylt6wgnnwyqdib based on your point:

$ docker builder du --verbose | grep y4dorrugwl7ylt6wgnnwyqdib
ID:             y4dorrugwl7ylt6wgnnwyqdib