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