images.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "slices"
  18. "strings"
  19. "sync"
  20. "time"
  21. "github.com/containerd/errdefs"
  22. "github.com/containerd/platforms"
  23. "github.com/distribution/reference"
  24. "github.com/docker/docker/api/types/container"
  25. "github.com/docker/docker/api/types/filters"
  26. "github.com/docker/docker/api/types/versions"
  27. "github.com/docker/docker/client"
  28. "golang.org/x/sync/errgroup"
  29. "github.com/docker/compose/v2/pkg/api"
  30. )
  31. func (s *composeService) Images(ctx context.Context, projectName string, options api.ImagesOptions) (map[string]api.ImageSummary, error) {
  32. projectName = strings.ToLower(projectName)
  33. allContainers, err := s.apiClient().ContainerList(ctx, container.ListOptions{
  34. All: true,
  35. Filters: filters.NewArgs(projectFilter(projectName)),
  36. })
  37. if err != nil {
  38. return nil, err
  39. }
  40. var containers []container.Summary
  41. if len(options.Services) > 0 {
  42. // filter service containers
  43. for _, c := range allContainers {
  44. if slices.Contains(options.Services, c.Labels[api.ServiceLabel]) {
  45. containers = append(containers, c)
  46. }
  47. }
  48. } else {
  49. containers = allContainers
  50. }
  51. version, err := s.RuntimeVersion(ctx)
  52. if err != nil {
  53. return nil, err
  54. }
  55. withPlatform := versions.GreaterThanOrEqualTo(version, "1.49")
  56. summary := map[string]api.ImageSummary{}
  57. var mux sync.Mutex
  58. eg, ctx := errgroup.WithContext(ctx)
  59. for _, c := range containers {
  60. eg.Go(func() error {
  61. image, err := s.apiClient().ImageInspect(ctx, c.Image)
  62. if err != nil {
  63. return err
  64. }
  65. id := image.ID // platform-specific image ID can't be combined with image tag, see https://github.com/moby/moby/issues/49995
  66. if withPlatform && c.ImageManifestDescriptor != nil && c.ImageManifestDescriptor.Platform != nil {
  67. image, err = s.apiClient().ImageInspect(ctx, c.Image, client.ImageInspectWithPlatform(c.ImageManifestDescriptor.Platform))
  68. if err != nil {
  69. return err
  70. }
  71. }
  72. var repository, tag string
  73. ref, err := reference.ParseDockerRef(c.Image)
  74. if err == nil {
  75. // ParseDockerRef will reject a local image ID
  76. repository = reference.FamiliarName(ref)
  77. if tagged, ok := ref.(reference.Tagged); ok {
  78. tag = tagged.Tag()
  79. }
  80. }
  81. created, err := time.Parse(time.RFC3339Nano, image.Created)
  82. if err != nil {
  83. return err
  84. }
  85. mux.Lock()
  86. defer mux.Unlock()
  87. summary[getCanonicalContainerName(c)] = api.ImageSummary{
  88. ID: id,
  89. Repository: repository,
  90. Tag: tag,
  91. Platform: platforms.Platform{
  92. Architecture: image.Architecture,
  93. OS: image.Os,
  94. OSVersion: image.OsVersion,
  95. Variant: image.Variant,
  96. },
  97. Size: image.Size,
  98. Created: created,
  99. LastTagTime: image.Metadata.LastTagTime,
  100. }
  101. return nil
  102. })
  103. }
  104. err = eg.Wait()
  105. return summary, err
  106. }
  107. func (s *composeService) getImageSummaries(ctx context.Context, repoTags []string) (map[string]api.ImageSummary, error) {
  108. summary := map[string]api.ImageSummary{}
  109. l := sync.Mutex{}
  110. eg, ctx := errgroup.WithContext(ctx)
  111. for _, repoTag := range repoTags {
  112. eg.Go(func() error {
  113. inspect, err := s.apiClient().ImageInspect(ctx, repoTag)
  114. if err != nil {
  115. if errdefs.IsNotFound(err) {
  116. return nil
  117. }
  118. return fmt.Errorf("unable to get image '%s': %w", repoTag, err)
  119. }
  120. tag := ""
  121. repository := ""
  122. ref, err := reference.ParseDockerRef(repoTag)
  123. if err == nil {
  124. // ParseDockerRef will reject a local image ID
  125. repository = reference.FamiliarName(ref)
  126. if tagged, ok := ref.(reference.Tagged); ok {
  127. tag = tagged.Tag()
  128. }
  129. }
  130. l.Lock()
  131. summary[repoTag] = api.ImageSummary{
  132. ID: inspect.ID,
  133. Repository: repository,
  134. Tag: tag,
  135. Size: inspect.Size,
  136. LastTagTime: inspect.Metadata.LastTagTime,
  137. }
  138. l.Unlock()
  139. return nil
  140. })
  141. }
  142. return summary, eg.Wait()
  143. }