1
0

images.go 3.2 KB

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