From 7187c78554e8a0a4352b0885d6eb8ab4bbad5190 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sat, 19 Oct 2024 19:42:29 +0200 Subject: [PATCH 1/2] image ls: show each tag for an image as a separate entry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A single image can be tagged under multiple names. While they are the same image under the hood (same digest), we always presented these as separate images in the list. This patch applies the same behavior for the tree view; we can consider having some "compact" presentation in future where we collapse these iamges (perhaps introducing a "names" column?) Before this patch: $ docker pull --quiet alpine:3.20 docker.io/library/alpine:3.20 $ docker pull --quiet alpine:latest docker.io/library/alpine:latest $ docker image ls --tree IMAGE ID DISK USAGE CONTENT SIZE USED alpine:3.20 alpine:latest beefdbd8a1da 13.6MB 4.09MB ├─ linux/arm64/v8 9cee2b382fe2 13.6MB 4.09MB ├─ linux/amd64 33735bd63cf8 0B 0B ├─ linux/arm/v6 50f635c8b04d 0B 0B ├─ linux/arm/v7 f2f82d424957 0B 0B ├─ linux/386 b3e87f642f5c 0B 0B ├─ linux/ppc64le c7a6800e3dc5 0B 0B ├─ linux/riscv64 80cde017a105 0B 0B └─ linux/s390x 2b5b26e09ca2 0B 0B With this patch applied: $ docker image ls --tree IMAGE ID DISK USAGE CONTENT SIZE USED alpine:3.20 beefdbd8a1da 13.6MB 4.09MB ├─ linux/arm64/v8 9cee2b382fe2 13.6MB 4.09MB ├─ linux/amd64 33735bd63cf8 0B 0B ├─ linux/arm/v6 50f635c8b04d 0B 0B ├─ linux/arm/v7 f2f82d424957 0B 0B ├─ linux/386 b3e87f642f5c 0B 0B ├─ linux/ppc64le c7a6800e3dc5 0B 0B ├─ linux/riscv64 80cde017a105 0B 0B └─ linux/s390x 2b5b26e09ca2 0B 0B alpine:latest beefdbd8a1da 13.6MB 4.09MB ├─ linux/arm64/v8 9cee2b382fe2 13.6MB 4.09MB ├─ linux/amd64 33735bd63cf8 0B 0B ├─ linux/arm/v6 50f635c8b04d 0B 0B ├─ linux/arm/v7 f2f82d424957 0B 0B ├─ linux/386 b3e87f642f5c 0B 0B ├─ linux/ppc64le c7a6800e3dc5 0B 0B ├─ linux/riscv64 80cde017a105 0B 0B └─ linux/s390x 2b5b26e09ca2 0B 0B Signed-off-by: Sebastiaan van Stijn --- cli/command/image/tree.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/cli/command/image/tree.go b/cli/command/image/tree.go index ec4ef7b4e4..6c939eb131 100644 --- a/cli/command/image/tree.go +++ b/cli/command/image/tree.go @@ -82,12 +82,30 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error details.ContentSize = units.HumanSizeWithPrecision(float64(totalContent), 3) - view.images = append(view.images, topImage{ - Names: img.RepoTags, - Details: details, - Children: children, - created: img.Created, - }) + if len(img.RepoTags) == 0 { + // Untagged image + view.images = append(view.images, topImage{ + Names: img.RepoTags, + Details: details, + Children: children, + created: img.Created, + }) + } else { + // Sort same images alphabetically to keep a consistent sort order. + // We can remove this if we decide to sort the list by name, instead + // of by "created" date. + sort.Strings(img.RepoTags) + + // Present images tagged under multiple names as separate images. + for _, n := range img.RepoTags { + view.images = append(view.images, topImage{ + Names: []string{n}, // Consider changing Names to be a single name for purpose of this presentation. + Details: details, + Children: children, + created: img.Created, + }) + } + } } sort.Slice(view.images, func(i, j int) bool { From be3646b87ce47e1f6e1b807364d112de83abf679 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 20 Oct 2024 23:54:57 +0200 Subject: [PATCH 2/2] image ls --tree: order images alphabetically MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The tree output currently uses the same sort order as the existing non-tree output, and orders the images by "created" time in descending order; docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE 8262a6d8c38a 7 minutes ago 13.6MB docker-cli-dev latest f5f0547476ee 12 minutes ago 762MB nginx alpine 2140dad235c1 2 weeks ago 76.7MB alpine latest beefdbd8a1da 6 weeks ago 24.2MB However, the `--tree` view does not have a `CREATED` column, which makes the output order seem "random". With the tree view being more verbose, it may also be harder to find back images in the list when they're not sorted in an easy to discover way. This patch changes the sort order: - alphabetically (natural sort) for tagged images - untagged images are sorted last, as they're likely less relevant to the user, and should not be "polluting" th top of the list. - if multiple untagged images exist, they are sorted by created date (descending) to get a stable order. Before this patch: $ docker image ls --tree IMAGE ID DISK USAGE CONTENT SIZE USED 20ad73eca911 13.6MB 4.09MB ✔ └─ linux/arm64 1ab6fc68586e 13.6MB 4.09MB ✔ b3e87f642f5c 13.6MB 4.09MB └─ linux/arm64 1ab6fc68586e 13.6MB 4.09MB docker-cli-dev:latest f5f0547476ee 762MB 179MB ✔ └─ linux/arm64 18ca7881145d 762MB 179MB ✔ nginx:alpine 2140dad235c1 76.7MB 21.5MB ├─ linux/arm64/v8 d1f949a77b81 76.7MB 21.5MB ├─ linux/amd64 ae136e431e76 0B 0B ├─ linux/arm/v6 ae1ee4b63c14 0B 0B ├─ linux/arm/v7 20ad73eca911 0B 0B ├─ linux/386 1e69bfb21757 0B 0B ├─ linux/ppc64le 7fef8bcf8b6c 0B 0B └─ linux/s390x 8c310bf29cfa 0B 0B alpine:latest beefdbd8a1da 24.2MB 7.46MB ├─ linux/riscv64 80cde017a105 10.6MB 3.37MB ├─ linux/arm64/v8 9cee2b382fe2 13.6MB 4.09MB ├─ linux/amd64 33735bd63cf8 0B 0B ├─ linux/arm/v6 50f635c8b04d 0B 0B ├─ linux/arm/v7 f2f82d424957 0B 0B ├─ linux/386 b3e87f642f5c 0B 0B ├─ linux/ppc64le c7a6800e3dc5 0B 0B └─ linux/s390x 2b5b26e09ca2 0B 0B With this patch: $ docker image ls --tree IMAGE ID DISK USAGE CONTENT SIZE USED alpine:latest beefdbd8a1da 24.2MB 7.46MB ├─ linux/riscv64 80cde017a105 10.6MB 3.37MB ├─ linux/arm64/v8 9cee2b382fe2 13.6MB 4.09MB ├─ linux/amd64 33735bd63cf8 0B 0B ├─ linux/arm/v6 50f635c8b04d 0B 0B ├─ linux/arm/v7 f2f82d424957 0B 0B ├─ linux/386 b3e87f642f5c 0B 0B ├─ linux/ppc64le c7a6800e3dc5 0B 0B └─ linux/s390x 2b5b26e09ca2 0B 0B docker-cli-dev:latest f5f0547476ee 762MB 179MB ✔ └─ linux/arm64 18ca7881145d 762MB 179MB ✔ nginx:alpine 2140dad235c1 76.7MB 21.5MB ├─ linux/arm64/v8 d1f949a77b81 76.7MB 21.5MB ├─ linux/amd64 ae136e431e76 0B 0B ├─ linux/arm/v6 ae1ee4b63c14 0B 0B ├─ linux/arm/v7 20ad73eca911 0B 0B ├─ linux/386 1e69bfb21757 0B 0B ├─ linux/ppc64le 7fef8bcf8b6c 0B 0B └─ linux/s390x 8c310bf29cfa 0B 0B 20ad73eca911 13.6MB 4.09MB ✔ └─ linux/arm64 1ab6fc68586e 13.6MB 4.09MB ✔ b3e87f642f5c 13.6MB 4.09MB └─ linux/arm64 1ab6fc68586e 13.6MB 4.09MB Signed-off-by: Sebastiaan van Stijn --- cli/command/image/tree.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/cli/command/image/tree.go b/cli/command/image/tree.go index 6c939eb131..038f56a841 100644 --- a/cli/command/image/tree.go +++ b/cli/command/image/tree.go @@ -14,6 +14,7 @@ import ( imagetypes "github.com/docker/docker/api/types/image" "github.com/docker/docker/pkg/stringid" "github.com/docker/go-units" + "github.com/fvbommel/sortorder" "github.com/morikuni/aec" ) @@ -91,11 +92,6 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error created: img.Created, }) } else { - // Sort same images alphabetically to keep a consistent sort order. - // We can remove this if we decide to sort the list by name, instead - // of by "created" date. - sort.Strings(img.RepoTags) - // Present images tagged under multiple names as separate images. for _, n := range img.RepoTags { view.images = append(view.images, topImage{ @@ -108,8 +104,25 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error } } + // Sort images alphabetically using natural-sort, with untagged images last. sort.Slice(view.images, func(i, j int) bool { - return view.images[i].created > view.images[j].created + iUntagged, jUntagged := len(view.images[i].Names) == 0, len(view.images[j].Names) == 0 + if iUntagged || jUntagged { + switch { + case iUntagged && jUntagged: + // Both untagged images; sort by created date (desc) + return view.images[i].created > view.images[j].created + case iUntagged: + // Sort untagged images last + return false + case jUntagged: + // Sort untagged images last + return true + } + } + + // Sort alphabetically, ascending + return sortorder.NaturalLess(view.images[i].Names[0], view.images[j].Names[0]) }) return printImageTree(dockerCLI, view)