images.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/v5/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. var created *time.Time
  82. if image.Created != "" {
  83. t, err := time.Parse(time.RFC3339Nano, image.Created)
  84. if err != nil {
  85. return err
  86. }
  87. created = &t
  88. }
  89. mux.Lock()
  90. defer mux.Unlock()
  91. summary[getCanonicalContainerName(c)] = api.ImageSummary{
  92. ID: id,
  93. Repository: repository,
  94. Tag: tag,
  95. Platform: platforms.Platform{
  96. Architecture: image.Architecture,
  97. OS: image.Os,
  98. OSVersion: image.OsVersion,
  99. Variant: image.Variant,
  100. },
  101. Size: image.Size,
  102. Created: created,
  103. LastTagTime: image.Metadata.LastTagTime,
  104. }
  105. return nil
  106. })
  107. }
  108. err = eg.Wait()
  109. return summary, err
  110. }
  111. func (s *composeService) getImageSummaries(ctx context.Context, repoTags []string) (map[string]api.ImageSummary, error) {
  112. summary := map[string]api.ImageSummary{}
  113. l := sync.Mutex{}
  114. eg, ctx := errgroup.WithContext(ctx)
  115. for _, repoTag := range repoTags {
  116. eg.Go(func() error {
  117. inspect, err := s.apiClient().ImageInspect(ctx, repoTag)
  118. if err != nil {
  119. if errdefs.IsNotFound(err) {
  120. return nil
  121. }
  122. return fmt.Errorf("unable to get image '%s': %w", repoTag, err)
  123. }
  124. tag := ""
  125. repository := ""
  126. ref, err := reference.ParseDockerRef(repoTag)
  127. if err == nil {
  128. // ParseDockerRef will reject a local image ID
  129. repository = reference.FamiliarName(ref)
  130. if tagged, ok := ref.(reference.Tagged); ok {
  131. tag = tagged.Tag()
  132. }
  133. }
  134. l.Lock()
  135. summary[repoTag] = api.ImageSummary{
  136. ID: inspect.ID,
  137. Repository: repository,
  138. Tag: tag,
  139. Size: inspect.Size,
  140. LastTagTime: inspect.Metadata.LastTagTime,
  141. }
  142. l.Unlock()
  143. return nil
  144. })
  145. }
  146. return summary, eg.Wait()
  147. }