dockerprune

package
v0.175.0-rc.511 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dockerprune keeps the Docker images and build cache of a lab machine under a size: the unused images created or tagged longest ago go first, the ones a kind lab's node holds a copy of last, and the build cache is pruned by Docker's own least-recently-used order. An image a container uses, or one created or tagged since a keep time, is never removed; a trim that stays over its size says which images kept it there.

Index

Constants

View Source
const KindLabel = "io.x-k8s.kind.cluster"

KindLabel is the label kind puts on its node containers.

Variables

View Source
var ErrBusy = errors.New("an image import runs")

ErrBusy stops a trim: an image import or build began while it ran.

Functions

func Importer

func Importer(args []string) bool

Importer says an argument vector loads or builds images: kind load and kind create (which loads its node image), docker build, load, save and import, buildx, and containerd's ctr import. Global options and their values precede the command path; command arguments are never verbs.

func Normalize

func Normalize(ref string) string

Normalize names a reference as containerd does: docker.io/library/nginx:1 for nginx:1.

func ParseSize

func ParseSize(s string) (int64, error)

ParseSize reads a size as docker prints it: decimal units, "1.5GB", "512kB", "0B", with an optional percentage after a space.

Types

type Blocker

type Blocker struct {
	Image Image
	Why   string
}

Blocker is an image a trim left in place, and why.

func Kept

func Kept(images []Image, keep time.Time) []Blocker

Kept are the images no trim removes, with why: the ones a container uses and the ones created or tagged after keep.

type CacheRecord added in v0.174.0

type CacheRecord struct {
	ID string
	// Size is what the record lists; shared layers may free less.
	Size int64
	// Reclaimable is Docker saying no build or container uses the record.
	// A record that is not reclaimable stays, whoever asks.
	Reclaimable bool
	// UsedAgo is how long before the reading the record was last used;
	// -1 when Docker's age is unreadable, which keeps the record.
	UsedAgo time.Duration
}

CacheRecord is one build cache record of `docker builder du`.

func CacheRecords added in v0.174.0

func CacheRecords(ctx context.Context, d Docker) ([]CacheRecord, error)

CacheRecords reads the build cache's records with their sizes and ages from `docker builder du`.

type CacheTrim added in v0.174.0

type CacheTrim struct {
	// Freed is the listed size of the records removed; shared layers may
	// free less, so the freed bytes of a run come from the df re-read.
	Freed int64
	// Removed names how many records went.
	Removed int
	// Kept counts the records that stay above the target because no
	// removal may take them (a build or container uses them), and KeptBytes
	// is what they hold.
	Kept      int
	KeptBytes int64
}

CacheTrim is one build cache trim's result.

func PruneBuildCache

func PruneBuildCache(ctx context.Context, d Docker, keep int64) (t CacheTrim, err error)

PruneBuildCache brings the build cache down to keep bytes, removing the reclaimable record used longest ago first. Docker's own `builder prune --max-used-space` cannot be asked for this: it is a best-effort pass of the daemon's GC policy and frees nothing while the policy's reserved or min-free-space floor sits above the target, which on a nearly full disk is always. A record Docker does not call reclaimable is never touched and is counted in Kept. A successful prune can remove nothing, so fresh records confirm progress and remaining size. Records blocked by children are retried while a pass removes a record.

type Docker

type Docker func(ctx context.Context, args ...string) ([]byte, error)

Docker runs one docker CLI call and returns its stdout.

type Image

type Image struct {
	ID string
	// Refs are its repository:tag names, none for a dangling image.
	Refs    []string
	Digest  string
	Created time.Time
	// Tagged is when a tag was last put on it (docker build, tag or load):
	// an image rebuilt from cache or retagged keeps its old Created.
	Tagged time.Time
	// Size is all its layers, the ones it shares with other images
	// included: at most what removing it frees, once every image sharing
	// them is gone. Unique is the layers no other image shares, what
	// removing it alone frees for certain.
	Size, Unique int64
	Containers   int
}

Image is one image of the machine's Docker.

func Candidates

func Candidates(images []Image, held InUse, keep time.Time) []Image

Candidates are the images a trim may remove, in the order it removes them: dangling ones first, then the ones created or tagged longest ago, and the ones a kind node holds (held) after all others, by the same order. An image a container uses or one created or tagged after keep is no candidate (Kept).

func ListImages

func ListImages(ctx context.Context, d Docker) ([]Image, error)

ListImages reads every image with its size and what removing it frees, from docker system df -v, and its complete references and last tag time from docker image inspect. Verbose df lists only the first tag.

func (Image) Active added in v0.156.0

func (i Image) Active() time.Time

Active is when the image was last produced: created or tagged, the later.

func (Image) Name added in v0.156.0

func (i Image) Name() string

Name is how a removal names the image: its references, or its ID for a dangling one.

type InUse

type InUse map[string]bool

InUse are the images kind labs' nodes hold: by ID, normalized reference and digest.

func KindImages

func KindImages(ctx context.Context, d Docker) (InUse, error)

KindImages are the images every running kind node holds. A node that is not running cannot say what it holds and is skipped: what it holds only ranks a trim's candidates, and its own copy stays whatever the host removes.

func (InUse) Has

func (u InUse) Has(img Image) bool

Has says a kind node holds img. The node's containerd has its own copy, so the host's may go, though a lab created again would load it anew.

type Trimmed

type Trimmed struct {
	Removed []string
	Refused []Blocker
}

Trimmed is what a trim did: the names of the images it removed, and the candidates Docker refused to remove.

func TrimImages

func TrimImages(ctx context.Context, d Docker, candidates []Image, total, target int64, size func() (int64, error), busy func() bool) (Trimmed, error)

TrimImages removes candidates in order until the images' size, from total, is at most target, asking busy before each removal. It counts each removal by the image's whole size, more than it may free (a layer another image shares stays), so the count never says more than Docker does; once the count says the target may be reached it reads the size again (size, nil for none) and goes on while Docker still says more. An image Docker refuses to remove (a container started on it since, a child image built on it) is skipped; while the images stay over target the refused ones are tried again, the newest first so a child goes before its parent, as long as a pass removes one. A failed removal can already have untagged references; retries read the remaining ones first.

type Usage

type Usage struct {
	Images, BuildCache int64
}

Usage is the size of the images and the build cache, as docker system df says them.

func ReadUsage

func ReadUsage(ctx context.Context, d Docker) (Usage, error)

ReadUsage reads the totals of docker system df.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL