images.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/v5/cmd/formatter"
  28. "github.com/docker/compose/v5/pkg/api"
  29. "github.com/docker/compose/v5/pkg/compose"
  30. )
  31. type imageOptions struct {
  32. *ProjectOptions
  33. Quiet bool
  34. Format string
  35. }
  36. func imagesCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
  37. opts := imageOptions{
  38. ProjectOptions: p,
  39. }
  40. imgCmd := &cobra.Command{
  41. Use: "images [OPTIONS] [SERVICE...]",
  42. Short: "List images used by the created containers",
  43. RunE: Adapt(func(ctx context.Context, args []string) error {
  44. return runImages(ctx, dockerCli, backendOptions, opts, args)
  45. }),
  46. ValidArgsFunction: completeServiceNames(dockerCli, p),
  47. }
  48. imgCmd.Flags().StringVar(&opts.Format, "format", "table", "Format the output. Values: [table | json]")
  49. imgCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
  50. return imgCmd
  51. }
  52. func runImages(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, opts imageOptions, services []string) error {
  53. projectName, err := opts.toProjectName(ctx, dockerCli)
  54. if err != nil {
  55. return err
  56. }
  57. backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
  58. if err != nil {
  59. return err
  60. }
  61. images, err := backend.Images(ctx, projectName, api.ImagesOptions{
  62. Services: services,
  63. })
  64. if err != nil {
  65. return err
  66. }
  67. if opts.Quiet {
  68. ids := []string{}
  69. for _, img := range images {
  70. id := img.ID
  71. if i := strings.IndexRune(img.ID, ':'); i >= 0 {
  72. id = id[i+1:]
  73. }
  74. if !slices.Contains(ids, id) {
  75. ids = append(ids, id)
  76. }
  77. }
  78. for _, img := range ids {
  79. _, _ = fmt.Fprintln(dockerCli.Out(), img)
  80. }
  81. return nil
  82. }
  83. if opts.Format == "json" {
  84. type img struct {
  85. ID string `json:"ID"`
  86. ContainerName string `json:"ContainerName"`
  87. Repository string `json:"Repository"`
  88. Tag string `json:"Tag"`
  89. Platform string `json:"Platform"`
  90. Size int64 `json:"Size"`
  91. Created *time.Time `json:"Created,omitempty"`
  92. LastTagTime time.Time `json:"LastTagTime,omitzero"`
  93. }
  94. // Convert map to slice
  95. var imageList []img
  96. for ctr, i := range images {
  97. lastTagTime := i.LastTagTime
  98. imageList = append(imageList, img{
  99. ContainerName: ctr,
  100. ID: i.ID,
  101. Repository: i.Repository,
  102. Tag: i.Tag,
  103. Platform: platforms.Format(i.Platform),
  104. Size: i.Size,
  105. Created: i.Created,
  106. LastTagTime: lastTagTime,
  107. })
  108. }
  109. json, err := formatter.ToJSON(imageList, "", "")
  110. if err != nil {
  111. return err
  112. }
  113. _, err = fmt.Fprintln(dockerCli.Out(), json)
  114. return err
  115. }
  116. return formatter.Print(images, opts.Format, dockerCli.Out(),
  117. func(w io.Writer) {
  118. for _, container := range slices.Sorted(maps.Keys(images)) {
  119. img := images[container]
  120. id := stringid.TruncateID(img.ID)
  121. size := units.HumanSizeWithPrecision(float64(img.Size), 3)
  122. repo := img.Repository
  123. if repo == "" {
  124. repo = "<none>"
  125. }
  126. tag := img.Tag
  127. if tag == "" {
  128. tag = "<none>"
  129. }
  130. created := "N/A"
  131. if img.Created != nil {
  132. created = units.HumanDuration(time.Now().UTC().Sub(*img.Created)) + " ago"
  133. }
  134. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
  135. container, repo, tag, platforms.Format(img.Platform), id, size, created)
  136. }
  137. },
  138. "CONTAINER", "REPOSITORY", "TAG", "PLATFORM", "IMAGE ID", "SIZE", "CREATED")
  139. }