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