images.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "maps"
  19. "slices"
  20. "strings"
  21. "time"
  22. "github.com/containerd/platforms"
  23. "github.com/docker/cli/cli/command"
  24. "github.com/docker/docker/pkg/stringid"
  25. "github.com/docker/go-units"
  26. "github.com/spf13/cobra"
  27. "github.com/docker/compose/v2/cmd/formatter"
  28. "github.com/docker/compose/v2/pkg/api"
  29. )
  30. type imageOptions struct {
  31. *ProjectOptions
  32. Quiet bool
  33. Format string
  34. }
  35. func imagesCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  36. opts := imageOptions{
  37. ProjectOptions: p,
  38. }
  39. imgCmd := &cobra.Command{
  40. Use: "images [OPTIONS] [SERVICE...]",
  41. Short: "List images used by the created containers",
  42. RunE: Adapt(func(ctx context.Context, args []string) error {
  43. return runImages(ctx, dockerCli, backend, opts, args)
  44. }),
  45. ValidArgsFunction: completeServiceNames(dockerCli, p),
  46. }
  47. imgCmd.Flags().StringVar(&opts.Format, "format", "table", "Format the output. Values: [table | json]")
  48. imgCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
  49. return imgCmd
  50. }
  51. func runImages(ctx context.Context, dockerCli command.Cli, backend api.Service, opts imageOptions, services []string) error {
  52. projectName, err := opts.toProjectName(ctx, dockerCli)
  53. if err != nil {
  54. return err
  55. }
  56. images, err := backend.Images(ctx, projectName, api.ImagesOptions{
  57. Services: services,
  58. })
  59. if err != nil {
  60. return err
  61. }
  62. if opts.Quiet {
  63. ids := []string{}
  64. for _, img := range images {
  65. id := img.ID
  66. if i := strings.IndexRune(img.ID, ':'); i >= 0 {
  67. id = id[i+1:]
  68. }
  69. if !slices.Contains(ids, id) {
  70. ids = append(ids, id)
  71. }
  72. }
  73. for _, img := range ids {
  74. _, _ = fmt.Fprintln(dockerCli.Out(), img)
  75. }
  76. return nil
  77. }
  78. if opts.Format == "json" {
  79. // Convert map to slice
  80. var imageList []api.ImageSummary
  81. for _, img := range images {
  82. imageList = append(imageList, img)
  83. }
  84. return formatter.Print(imageList, opts.Format, dockerCli.Out(), nil)
  85. }
  86. return formatter.Print(images, opts.Format, dockerCli.Out(),
  87. func(w io.Writer) {
  88. for _, container := range slices.Sorted(maps.Keys(images)) {
  89. img := images[container]
  90. id := stringid.TruncateID(img.ID)
  91. size := units.HumanSizeWithPrecision(float64(img.Size), 3)
  92. repo := img.Repository
  93. if repo == "" {
  94. repo = "<none>"
  95. }
  96. tag := img.Tag
  97. if tag == "" {
  98. tag = "<none>"
  99. }
  100. created := units.HumanDuration(time.Now().UTC().Sub(img.LastTagTime)) + " ago"
  101. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
  102. container, repo, tag, platforms.Format(img.Platform), id, size, created)
  103. }
  104. },
  105. "CONTAINER", "REPOSITORY", "TAG", "PLATFORM", "IMAGE ID", "SIZE", "CREATED")
  106. }