ps.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "sort"
  17. "strings"
  18. "github.com/docker/docker/api/types/container"
  19. "golang.org/x/sync/errgroup"
  20. "github.com/docker/compose/v2/pkg/api"
  21. )
  22. func (s *composeService) Ps(ctx context.Context, projectName string, options api.PsOptions) ([]api.ContainerSummary, error) {
  23. projectName = strings.ToLower(projectName)
  24. oneOff := oneOffExclude
  25. if options.All {
  26. oneOff = oneOffInclude
  27. }
  28. containers, err := s.getContainers(ctx, projectName, oneOff, options.All, options.Services...)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if len(options.Services) != 0 {
  33. containers = containers.filter(isService(options.Services...))
  34. }
  35. summary := make([]api.ContainerSummary, len(containers))
  36. eg, ctx := errgroup.WithContext(ctx)
  37. for i, ctr := range containers {
  38. eg.Go(func() error {
  39. publishers := make([]api.PortPublisher, len(ctr.Ports))
  40. sort.Slice(ctr.Ports, func(i, j int) bool {
  41. return ctr.Ports[i].PrivatePort < ctr.Ports[j].PrivatePort
  42. })
  43. for i, p := range ctr.Ports {
  44. publishers[i] = api.PortPublisher{
  45. URL: p.IP,
  46. TargetPort: int(p.PrivatePort),
  47. PublishedPort: int(p.PublicPort),
  48. Protocol: p.Type,
  49. }
  50. }
  51. inspect, err := s.apiClient().ContainerInspect(ctx, ctr.ID)
  52. if err != nil {
  53. return err
  54. }
  55. var (
  56. health container.HealthStatus
  57. exitCode int
  58. )
  59. if inspect.State != nil {
  60. switch inspect.State.Status {
  61. case container.StateRunning:
  62. if inspect.State.Health != nil {
  63. health = inspect.State.Health.Status
  64. }
  65. case container.StateExited, container.StateDead:
  66. exitCode = inspect.State.ExitCode
  67. }
  68. }
  69. var (
  70. local int
  71. mounts []string
  72. )
  73. for _, m := range ctr.Mounts {
  74. name := m.Name
  75. if name == "" {
  76. name = m.Source
  77. }
  78. if m.Driver == "local" {
  79. local++
  80. }
  81. mounts = append(mounts, name)
  82. }
  83. var networks []string
  84. if ctr.NetworkSettings != nil {
  85. for k := range ctr.NetworkSettings.Networks {
  86. networks = append(networks, k)
  87. }
  88. }
  89. summary[i] = api.ContainerSummary{
  90. ID: ctr.ID,
  91. Name: getCanonicalContainerName(ctr),
  92. Names: ctr.Names,
  93. Image: ctr.Image,
  94. Project: ctr.Labels[api.ProjectLabel],
  95. Service: ctr.Labels[api.ServiceLabel],
  96. Command: ctr.Command,
  97. State: ctr.State,
  98. Status: ctr.Status,
  99. Created: ctr.Created,
  100. Labels: ctr.Labels,
  101. SizeRw: ctr.SizeRw,
  102. SizeRootFs: ctr.SizeRootFs,
  103. Mounts: mounts,
  104. LocalVolumes: local,
  105. Networks: networks,
  106. Health: health,
  107. ExitCode: exitCode,
  108. Publishers: publishers,
  109. }
  110. return nil
  111. })
  112. }
  113. return summary, eg.Wait()
  114. }