55 "errors"
66 "fmt"
77 "io"
8+ "slices"
89
910 "github.com/docker/cli/cli"
1011 "github.com/docker/cli/cli/command"
@@ -98,20 +99,30 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
9899 if options .format != "" {
99100 return errors .New ("--format is not yet supported with --tree" )
100101 }
102+ }
101103
102- return runTree ( ctx , dockerCLI , treeOptions {
103- all : options .all ,
104- filters : filters ,
105- })
104+ listOpts := client. ImageListOptions {
105+ All : options .all ,
106+ Filters : filters ,
107+ Manifests : options . tree ,
106108 }
107109
108- images , err := dockerCLI .Client ().ImageList (ctx , client.ImageListOptions {
109- All : options .all ,
110- Filters : filters ,
111- })
110+ res , err := dockerCLI .Client ().ImageList (ctx , listOpts )
112111 if err != nil {
113112 return err
114113 }
114+ images := res .Items
115+ if ! options .all {
116+ images = slices .DeleteFunc (images , isDangling )
117+ }
118+
119+ if options .tree {
120+ return runTree (ctx , dockerCLI , treeOptions {
121+ images : images ,
122+ all : options .all ,
123+ filters : filters ,
124+ })
125+ }
115126
116127 format := options .format
117128 if len (format ) == 0 {
@@ -130,10 +141,10 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
130141 },
131142 Digest : options .showDigests ,
132143 }
133- if err := formatter .ImageWrite (imageCtx , images . Items ); err != nil {
144+ if err := formatter .ImageWrite (imageCtx , images ); err != nil {
134145 return err
135146 }
136- if options .matchName != "" && len (images . Items ) == 0 && options .calledAs == "images" {
147+ if options .matchName != "" && len (images ) == 0 && options .calledAs == "images" {
137148 printAmbiguousHint (dockerCLI .Err (), options .matchName )
138149 }
139150 return nil
0 commit comments