images.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. type img struct {
  80. ID string `json:"ID"`
  81. ContainerName string `json:"ContainerName"`
  82. Repository string `json:"Repository"`
  83. Tag string `json:"Tag"`
  84. Platform string `json:"Platform"`
  85. Size int64 `json:"Size"`
  86. LastTagTime time.Time `json:"LastTagTime"`
  87. }
  88. // Convert map to slice
  89. var imageList []img
  90. for ctr, i := range images {
  91. imageList = append(imageList, img{
  92. ContainerName: ctr,
  93. ID: i.ID,
  94. Repository: i.Repository,
  95. Tag: i.Tag,
  96. Platform: platforms.Format(i.Platform),
  97. Size: i.Size,
  98. LastTagTime: i.LastTagTime,
  99. })
  100. }
  101. json, err := formatter.ToJSON(imageList, "", "")
  102. if err != nil {
  103. return err
  104. }
  105. _, err = fmt.Fprintln(dockerCli.Out(), json)
  106. return err
  107. }
  108. return formatter.Print(images, opts.Format, dockerCli.Out(),
  109. func(w io.Writer) {
  110. for _, container := range slices.Sorted(maps.Keys(images)) {
  111. img := images[container]
  112. id := stringid.TruncateID(img.ID)
  113. size := units.HumanSizeWithPrecision(float64(img.Size), 3)
  114. repo := img.Repository
  115. if repo == "" {
  116. repo = "<none>"
  117. }
  118. tag := img.Tag
  119. if tag == "" {
  120. tag = "<none>"
  121. }
  122. created := units.HumanDuration(time.Now().UTC().Sub(img.LastTagTime)) + " ago"
  123. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
  124. container, repo, tag, platforms.Format(img.Platform), id, size, created)
  125. }
  126. },
  127. "CONTAINER", "REPOSITORY", "TAG", "PLATFORM", "IMAGE ID", "SIZE", "CREATED")
  128. }