Pull request https://github.com/moby/moby/pull/37846 and https://github.com/docker/cli/pull/1327 added support for configuring BuildKit's garbage-collection policies in the daemon configuration and when using docker builder prune.
Both of these options are currently undocumented, as well as (bash) completion scripts missing.
We should add proper documentation for this feature, accompanied by some examples.
Related PR's and issues:
Things to resolve:
dockerd reference) with (syntax) examplesdocker builder prune reference docs to--filter, --keep-storage, -f / --force, and -a / --all options that were added in https://github.com/docker/cli/pull/1327)--filter, --keep-storage, -f / --force, and -a / --all options that were added in https://github.com/docker/cli/pull/1327)@tiborvass @tonistiigi could you either work on this, or provide input if someone wants to work on this?
/cc @chris-crone @nebuk89 (for planning, if needed), @usha-mandya (FYI)
@tiborvass @tonistiigi could you either work on this, or provide input if someone wants to work on this?
Hi, i would love to work on this @tiborvass @tonistiigi as input been provided for it? @thaJeztah
ping @tiborvass @tonistiigi could one of you help providing the details?
Hi, @thaJeztah I haven't received any details yet from @tiborvass and @tonistiigi, if you don't mind, where is the best method of communication is?
I tried asking them on our internal Slack (but timezones are sometimes a pain); if you're on the Docker Community slack, you could try the #buildkit channel
Ok, I'm sorry but can I private ping them for better communication or just throw it open on the channel? Thanks.
Feel free to post it in the channel there!
Thank you @adeniyistephen for offering your help on this!
Let me give you a brain dump of the situation.
First, when the BuildKit garbage collector is enabled it runs periodically in the background and follows an ordered list of policies. Each policy is a prune operation so the order matters. But pruning can also be done manually with docker builder prune and when testing it, it's probably easier to run things manually first.
Therefore, what needs to be done here is:
DefaultKeepStorage (the easiest way to configure GC without learning all the details of policies)DefaultKeepStorage is approximately 10% of free disk space: https://github.com/moby/moby/blob/fb7883e980f6a4b5c5d40f673dd539499c328885/builder/builder-next/worker/gc_unix.go#L15Most people should simply enable GC and set the desired DefaultKeepStorage in the GC config.
Below I'll try to describe the detailed part of how pruning works.
The prune operation deletes build cache records and the main logic is in the pruneOnce() function at https://github.com/moby/buildkit/blob/d168063ee5b73510b07d80e8eef1dfd86adcb853/cache/manager.go#L606
Note that pruneOnce() calls a recursive prune() method.
Build cache records can have the following attributes:
id: ID of cache recordparent: ID of parent of cache recordtype: type (see below)description: a metadata stringinuse: if true, it cannot be reclaimed (aka deleted)shared: if true, the same content is also referenced by an image in docker images -a. By default record will not be removed as it is impossible to know whether the user also intended to remove the referencing image (be it untagged or not).private: not sharedThe type attribute can be one of the following:
regular: intermediate snapshotssource.local: cache of local context enabling fast incremental uploadsexec.cachemount: cache type for RUN --mount=type=cache,...frontend: cache for BuildKit frontend images (# syntax = user/somefrontendimage)internal: cache for helper tools internal to BuildKitThese build cache records can be inspected with docker system df -v (but shows more than just build cache records) or with buildx via buildx du --verbose with a bit different output.
The prune operation's main inputs are:
filter: list of AND-ed filters each containing one of the cache record attributes aboveall: if true, will delete cache records that are either marked as shared, as internal or as a frontendkeepDuration: duration between now and the cutoff time after which cache records should be keptkeepBytes: threshold of cache disk usage, above which the prune operation starts deleting cache records until the threshold is no longer exceeded. Below that threshold nothing happens. If 0, then all cache records matching the other criteria are deleted.The builder prune/buildx prune CLI maps these inputs a bit differently than BuildKit: keepBytes is --keep-storage, all is --all, and filter is a set of --filter flags but, in addition to the cache attributes detailed above, it can also contain an until key which maps to keepDuration. Implementation of this mapping is in toBuildkitPruneInfo().
Hope this helps.
I see there's some discussion in https://github.com/moby/buildkit/issues/1385 as well that may be relevant
@tiborvass @thaJeztah Thanks, working on it. will ping you if i need any help.