images.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/moby/moby/api/types/container"
  25. "github.com/moby/moby/client"
  26. "github.com/moby/moby/client/pkg/versions"
  27. "golang.org/x/sync/errgroup"
  28. "github.com/docker/compose/v5/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, client.ContainerListOptions{
  33. All: true,
  34. Filters: 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.Items {
  43. if slices.Contains(options.Services, c.Labels[api.ServiceLabel]) {
  44. containers = append(containers, c)
  45. }
  46. }
  47. } else {
  48. containers = allContainers.Items
  49. }
  50. // The daemon validates the platform field in ImageInspect against the
  51. // negotiated API version from the request path, not the server's own max version.
  52. version, err := s.RuntimeAPIVersion(ctx)
  53. if err != nil {
  54. return nil, err
  55. }
  56. withPlatform := versions.GreaterThanOrEqualTo(version, apiVersion149)
  57. summary := map[string]api.ImageSummary{}
  58. var mux sync.Mutex
  59. eg, ctx := errgroup.WithContext(ctx)
  60. for _, c := range containers {
  61. eg.Go(func() error {
  62. image, err := s.apiClient().ImageInspect(ctx, c.Image)
  63. if err != nil {
  64. return err
  65. }
  66. id := image.ID // platform-specific image ID can't be combined with image tag, see https://github.com/moby/moby/issues/49995
  67. if withPlatform && c.ImageManifestDescriptor != nil && c.ImageManifestDescriptor.Platform != nil {
  68. image, err = s.apiClient().ImageInspect(ctx, c.Image, client.ImageInspectWithPlatform(c.ImageManifestDescriptor.Platform))
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. var repository, tag string
  74. ref, err := reference.ParseDockerRef(c.Image)
  75. if err == nil {
  76. // ParseDockerRef will reject a local image ID
  77. repository = reference.FamiliarName(ref)
  78. if tagged, ok := ref.(reference.Tagged); ok {
  79. tag = tagged.Tag()
  80. }
  81. }
  82. var created *time.Time
  83. if image.Created != "" {
  84. t, err := time.Parse(time.RFC3339Nano, image.Created)
  85. if err != nil {
  86. return err
  87. }
  88. created = &t
  89. }
  90. mux.Lock()
  91. defer mux.Unlock()
  92. summary[getCanonicalContainerName(c)] = api.ImageSummary{
  93. ID: id,
  94. Repository: repository,
  95. Tag: tag,
  96. Platform: platforms.Platform{
  97. Architecture: image.Architecture,
  98. OS: image.Os,
  99. OSVersion: image.OsVersion,
  100. Variant: image.Variant,
  101. },
  102. Size: image.Size,
  103. Created: created,
  104. LastTagTime: image.Metadata.LastTagTime,
  105. }
  106. return nil
  107. })
  108. }
  109. err = eg.Wait()
  110. return summary, err
  111. }
  112. func (s *composeService) getImageSummaries(ctx context.Context, repoTags []string) (map[string]api.ImageSummary, error) {
  113. summary := map[string]api.ImageSummary{}
  114. l := sync.Mutex{}
  115. eg, ctx := errgroup.WithContext(ctx)
  116. for _, repoTag := range repoTags {
  117. eg.Go(func() error {
  118. inspect, err := s.apiClient().ImageInspect(ctx, repoTag)
  119. if err != nil {
  120. if errdefs.IsNotFound(err) {
  121. return nil
  122. }
  123. return fmt.Errorf("unable to get image '%s': %w", repoTag, err)
  124. }
  125. tag := ""
  126. repository := ""
  127. ref, err := reference.ParseDockerRef(repoTag)
  128. if err == nil {
  129. // ParseDockerRef will reject a local image ID
  130. repository = reference.FamiliarName(ref)
  131. if tagged, ok := ref.(reference.Tagged); ok {
  132. tag = tagged.Tag()
  133. }
  134. }
  135. l.Lock()
  136. summary[repoTag] = api.ImageSummary{
  137. ID: inspect.ID,
  138. Repository: repository,
  139. Tag: tag,
  140. Size: inspect.Size,
  141. LastTagTime: inspect.Metadata.LastTagTime,
  142. }
  143. l.Unlock()
  144. return nil
  145. })
  146. }
  147. return summary, eg.Wait()
  148. }