ps.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "golang.org/x/sync/errgroup"
  19. "github.com/docker/compose/v2/pkg/api"
  20. )
  21. func (s *composeService) Ps(ctx context.Context, projectName string, options api.PsOptions) ([]api.ContainerSummary, error) {
  22. projectName = strings.ToLower(projectName)
  23. oneOff := oneOffExclude
  24. if options.All {
  25. oneOff = oneOffInclude
  26. }
  27. containers, err := s.getContainers(ctx, projectName, oneOff, options.All, options.Services...)
  28. if err != nil {
  29. return nil, err
  30. }
  31. project := options.Project
  32. if project == nil {
  33. project, err = s.getProjectWithResources(ctx, containers, projectName)
  34. if err != nil {
  35. return nil, err
  36. }
  37. }
  38. if len(options.Services) == 0 {
  39. options.Services = project.ServiceNames()
  40. }
  41. containers = containers.filter(isService(options.Services...))
  42. summary := make([]api.ContainerSummary, len(containers))
  43. eg, ctx := errgroup.WithContext(ctx)
  44. for i, container := range containers {
  45. i, container := i, container
  46. eg.Go(func() error {
  47. var publishers []api.PortPublisher
  48. sort.Slice(container.Ports, func(i, j int) bool {
  49. return container.Ports[i].PrivatePort < container.Ports[j].PrivatePort
  50. })
  51. for _, p := range container.Ports {
  52. publishers = append(publishers, api.PortPublisher{
  53. URL: p.IP,
  54. TargetPort: int(p.PrivatePort),
  55. PublishedPort: int(p.PublicPort),
  56. Protocol: p.Type,
  57. })
  58. }
  59. inspect, err := s.apiClient().ContainerInspect(ctx, container.ID)
  60. if err != nil {
  61. return err
  62. }
  63. var (
  64. health string
  65. exitCode int
  66. )
  67. if inspect.State != nil {
  68. switch inspect.State.Status {
  69. case "running":
  70. if inspect.State.Health != nil {
  71. health = inspect.State.Health.Status
  72. }
  73. case "exited", "dead":
  74. exitCode = inspect.State.ExitCode
  75. }
  76. }
  77. summary[i] = api.ContainerSummary{
  78. ID: container.ID,
  79. Name: getCanonicalContainerName(container),
  80. Image: container.Image,
  81. Project: container.Labels[api.ProjectLabel],
  82. Service: container.Labels[api.ServiceLabel],
  83. Command: container.Command,
  84. State: container.State,
  85. Status: container.Status,
  86. Created: container.Created,
  87. Health: health,
  88. ExitCode: exitCode,
  89. Publishers: publishers,
  90. }
  91. return nil
  92. })
  93. }
  94. return summary, eg.Wait()
  95. }