images.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "strings"
  18. "sync"
  19. "github.com/distribution/reference"
  20. "github.com/docker/docker/api/types/container"
  21. "github.com/docker/docker/api/types/filters"
  22. "github.com/docker/docker/errdefs"
  23. "golang.org/x/sync/errgroup"
  24. "github.com/docker/compose/v2/pkg/api"
  25. "github.com/docker/compose/v2/pkg/utils"
  26. )
  27. func (s *composeService) Images(ctx context.Context, projectName string, options api.ImagesOptions) ([]api.ImageSummary, error) {
  28. projectName = strings.ToLower(projectName)
  29. allContainers, err := s.apiClient().ContainerList(ctx, container.ListOptions{
  30. All: true,
  31. Filters: filters.NewArgs(projectFilter(projectName)),
  32. })
  33. if err != nil {
  34. return nil, err
  35. }
  36. var containers []container.Summary
  37. if len(options.Services) > 0 {
  38. // filter service containers
  39. for _, c := range allContainers {
  40. if utils.StringContains(options.Services, c.Labels[api.ServiceLabel]) {
  41. containers = append(containers, c)
  42. }
  43. }
  44. } else {
  45. containers = allContainers
  46. }
  47. images := []string{}
  48. for _, c := range containers {
  49. if !utils.StringContains(images, c.Image) {
  50. images = append(images, c.Image)
  51. }
  52. }
  53. imageSummaries, err := s.getImageSummaries(ctx, images)
  54. if err != nil {
  55. return nil, err
  56. }
  57. summary := make([]api.ImageSummary, len(containers))
  58. for i, c := range containers {
  59. img, ok := imageSummaries[c.Image]
  60. if !ok {
  61. return nil, fmt.Errorf("failed to retrieve image for container %s", getCanonicalContainerName(c))
  62. }
  63. summary[i] = img
  64. summary[i].ContainerName = getCanonicalContainerName(c)
  65. }
  66. return summary, nil
  67. }
  68. func (s *composeService) getImageSummaries(ctx context.Context, repoTags []string) (map[string]api.ImageSummary, error) {
  69. summary := map[string]api.ImageSummary{}
  70. l := sync.Mutex{}
  71. eg, ctx := errgroup.WithContext(ctx)
  72. for _, repoTag := range repoTags {
  73. eg.Go(func() error {
  74. inspect, err := s.apiClient().ImageInspect(ctx, repoTag)
  75. if err != nil {
  76. if errdefs.IsNotFound(err) {
  77. return nil
  78. }
  79. return fmt.Errorf("unable to get image '%s': %w", repoTag, err)
  80. }
  81. tag := ""
  82. repository := ""
  83. ref, err := reference.ParseDockerRef(repoTag)
  84. if err == nil {
  85. // ParseDockerRef will reject a local image ID
  86. repository = reference.FamiliarName(ref)
  87. if tagged, ok := ref.(reference.Tagged); ok {
  88. tag = tagged.Tag()
  89. }
  90. }
  91. l.Lock()
  92. summary[repoTag] = api.ImageSummary{
  93. ID: inspect.ID,
  94. Repository: repository,
  95. Tag: tag,
  96. Size: inspect.Size,
  97. }
  98. l.Unlock()
  99. return nil
  100. })
  101. }
  102. return summary, eg.Wait()
  103. }