images.go 2.7 KB

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