Help us shape the future of Go! Take the 2026 Go Developer Survey

dockerprune

package
v0.147.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package dockerprune keeps the Docker images and build cache of a lab machine under a size: the unused images created longest ago go first, the build cache is pruned by Docker's own least-recently-used order. An image a container uses, or one a kind lab's node holds, is never removed.

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. The verb is one of the first three words that are no flag: docker image load, docker --context x build, ctr -n k8s.io images import.

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.

func PruneBuildCache

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

PruneBuildCache prunes the build cache down to keep bytes; the cache a build uses stays.

func TrimImages

func TrimImages(ctx context.Context, d Docker, candidates []Image, total, target int64, busy func() bool) (freed int64, removed int, err error)

TrimImages removes candidates in order until the images' size, from total, is at most target, asking busy before each removal. An image Docker refuses to remove (a container started on it since) is skipped. It returns what it freed by the images' unique sizes and how many it removed.

Types

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
	// Unique is what removing it frees: the layers no other image shares.
	Unique     int64
	Containers int
}

Image is one image of the machine's Docker.

func Candidates

func Candidates(images []Image, used InUse) []Image

Candidates are the images a trim may remove, in the order it removes them: dangling ones first, then the ones created longest ago.

func ListImages

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

ListImages reads every image with what removing it frees, from docker system df -v, which lists an image once per tag.

type InUse

type InUse map[string]bool

InUse are the images that must stay: by ID, normalized reference and digest.

func KindImages

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

KindImages are the images every kind node holds, and the node images themselves through the containers using them. A node that is not running cannot say what it holds: then nothing is known to be unused, and the call fails.

func (InUse) Has

func (u InUse) Has(img Image) bool

Has says img is in use: a container runs it, or a kind node holds it.

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