From f18e239a53fae289ddbddc53f797ea1859c7cf1d Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 10 Mar 2025 20:03:39 +0100 Subject: [PATCH] docker images --tree: hide both untagged and dangling images by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this patch, `docker image ls` / `docker image ls` would always show untagged images, but hide "dangling" images (which effectively only were produced by the legacy builder) unless `-a` / `--all` was used. This often resulted in many `:` or `` images to be shown, which had little value to interact with, other than to garbage collect (`docker system prune`). In future, we want to take more advantage of containerd's garbage-collecting features (removing unused images automatically), and this UX change is a stepping stone toward that. For now, this patch only changes the behavior for `docker image ls --tree`, but we should make this the same for "non" --tree as well. This patch: - changes `docker image ls` to hide both "untagged" and "dangling" images by default. - changes the behavior of `--all` on the client side to make them visible The API response remains the same for now, but this is something we can consider changing in future (possibly more granular than a single boolean). Before this patch; docker image ls --tree i Info → U In Use IMAGE ID DISK USAGE CONTENT SIZE EXTRA docker:cli 28fb556c1ea1 276MB 69.8MB ├─ linux/amd64 828f4f57525d 0B 0B ├─ linux/arm/v6 563c0b58e54b 0B 0B ├─ linux/arm/v7 6045d4846c59 0B 0B └─ linux/arm64/v8 11e8dfd68841 276MB 69.8MB alpine:latest a8560b36e8b8 12.8MB 3.99MB U ├─ linux/amd64 1c4eef651f65 0B 0B ├─ linux/arm/v6 903bfe2ae994 0B 0B ├─ linux/arm/v7 9c2d245b3c01 0B 0B ├─ linux/arm64/v8 757d680068d7 12.8MB 3.99MB U ├─ linux/386 2436f2b3b7d2 0B 0B ├─ linux/ppc64le 9ed53fd3b831 0B 0B ├─ linux/riscv64 1de5eb4a9a67 0B 0B └─ linux/s390x fe0dcdd1f783 0B 0B c6c1bcb0fd8d 12.8MB 3.99MB └─ linux/arm64 cb171c618ae8 12.8MB 3.99MB 7361ef970703 12.8MB 3.99MB └─ linux/arm64 07033f43e44a 12.8MB 3.99MB 0c62c63b81ec 12.8MB 3.99MB └─ linux/arm64 94742272117f 12.8MB 3.99MB 91dd947eebd0 12.8MB 3.99MB └─ linux/arm64 ee55d203e26f 12.8MB 3.99MB 382d9f57e8d8 12.8MB 3.99MB └─ linux/arm64 5256d47804e3 12.8MB 3.99MB 56fa17d2a7e7 12.8MB 3.99MB ├─ linux/amd64 483f502c0e6a 0B 0B ├─ linux/arm/v6 c79529000bdf 0B 0B ├─ linux/arm/v7 cc455d4b2c47 0B 0B ├─ linux/arm64/v8 508c1b94e1d2 12.8MB 3.99MB ├─ linux/386 f32403957113 0B 0B ├─ linux/ppc64le 23dbce23b88f 0B 0B ├─ linux/riscv64 f9d2da150cee 0B 0B └─ linux/s390x 6bb03952a007 0B 0B After this patch docker image ls --tree i Info → U In Use IMAGE ID DISK USAGE CONTENT SIZE EXTRA docker:cli 28fb556c1ea1 276MB 69.8MB ├─ linux/amd64 828f4f57525d 0B 0B ├─ linux/arm/v6 563c0b58e54b 0B 0B ├─ linux/arm/v7 6045d4846c59 0B 0B └─ linux/arm64/v8 11e8dfd68841 276MB 69.8MB alpine:latest a8560b36e8b8 12.8MB 3.99MB U ├─ linux/amd64 1c4eef651f65 0B 0B ├─ linux/arm/v6 903bfe2ae994 0B 0B ├─ linux/arm/v7 9c2d245b3c01 0B 0B ├─ linux/arm64/v8 757d680068d7 12.8MB 3.99MB U ├─ linux/386 2436f2b3b7d2 0B 0B ├─ linux/ppc64le 9ed53fd3b831 0B 0B ├─ linux/riscv64 1de5eb4a9a67 0B 0B └─ linux/s390x fe0dcdd1f783 0B 0B Signed-off-by: Sebastiaan van Stijn --- cli/command/image/list.go | 8 ++++++++ cli/command/image/tree.go | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/cli/command/image/list.go b/cli/command/image/list.go index cfe73c5ade..ce2238dc82 100644 --- a/cli/command/image/list.go +++ b/cli/command/image/list.go @@ -135,6 +135,14 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions return nil } +// isDangling is a copy of [formatter.isDangling]. +func isDangling(img image.Summary) bool { + if len(img.RepoTags) == 0 && len(img.RepoDigests) == 0 { + return true + } + return len(img.RepoTags) == 1 && img.RepoTags[0] == ":" && len(img.RepoDigests) == 1 && img.RepoDigests[0] == "@" +} + // printAmbiguousHint prints an informational warning if the provided filter // argument is ambiguous. // diff --git a/cli/command/image/tree.go b/cli/command/image/tree.go index 804b80679e..bbcea80b13 100644 --- a/cli/command/image/tree.go +++ b/cli/command/image/tree.go @@ -1,8 +1,12 @@ +// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16: +//go:build go1.22 + package image import ( "context" "fmt" + "slices" "sort" "strings" @@ -38,6 +42,9 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error if err != nil { return err } + if !opts.all { + images = slices.DeleteFunc(images, isDangling) + } view := treeView{ images: make([]topImage, 0, len(images)),