images.go 4.2 KB

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