Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 4d3d7c1

Browse files
authored
Merge pull request #1211 from docker/health
Add HEALTH column to compose ps
2 parents 9ef7850 + c4f6b1b commit 4d3d7c1

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

api/compose/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ type ContainerSummary struct {
111111
Project string
112112
Service string
113113
State string
114+
Health string
114115
Publishers []PortPublisher
115116
}
116117

cli/cmd/compose/ps.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ func runPs(ctx context.Context, opts psOptions) error {
9393
ports = append(ports, fmt.Sprintf("%s->%d/%s", p.URL, p.TargetPort, p.Protocol))
9494
}
9595
}
96-
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", container.Name, container.Service, container.State, strings.Join(ports, ", "))
96+
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", container.Name, container.Service, container.State, container.Health, strings.Join(ports, ", "))
9797
}
9898
},
99-
"NAME", "SERVICE", "STATE", "PORTS")
99+
"NAME", "SERVICE", "STATE", "HEALTH", "PORTS")
100100
}

local/compose/ps.go

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
moby "github.com/docker/docker/api/types"
2525
"github.com/docker/docker/api/types/filters"
26+
"golang.org/x/sync/errgroup"
2627

2728
"github.com/docker/compose-cli/api/compose"
2829
)
@@ -38,32 +39,49 @@ func (s *composeService) Ps(ctx context.Context, projectName string, options com
3839
return nil, err
3940
}
4041

41-
var summary []compose.ContainerSummary
42-
for _, c := range containers {
43-
var publishers []compose.PortPublisher
44-
for _, p := range c.Ports {
45-
var url string
46-
if p.PublicPort != 0 {
47-
url = fmt.Sprintf("%s:%d", p.IP, p.PublicPort)
42+
summary := make([]compose.ContainerSummary, len(containers))
43+
eg, ctx := errgroup.WithContext(ctx)
44+
for i, c := range containers {
45+
container := c
46+
i := i
47+
eg.Go(func() error {
48+
var publishers []compose.PortPublisher
49+
for _, p := range container.Ports {
50+
var url string
51+
if p.PublicPort != 0 {
52+
url = fmt.Sprintf("%s:%d", p.IP, p.PublicPort)
53+
}
54+
publishers = append(publishers, compose.PortPublisher{
55+
URL: url,
56+
TargetPort: int(p.PrivatePort),
57+
PublishedPort: int(p.PublicPort),
58+
Protocol: p.Type,
59+
})
60+
}
61+
62+
inspect, err := s.apiClient.ContainerInspect(ctx, container.ID)
63+
if err != nil {
64+
return err
4865
}
49-
publishers = append(publishers, compose.PortPublisher{
50-
URL: url,
51-
TargetPort: int(p.PrivatePort),
52-
PublishedPort: int(p.PublicPort),
53-
Protocol: p.Type,
54-
})
55-
}
5666

57-
summary = append(summary, compose.ContainerSummary{
58-
ID: c.ID,
59-
Name: getCanonicalContainerName(c),
60-
Project: c.Labels[projectLabel],
61-
Service: c.Labels[serviceLabel],
62-
State: c.State,
63-
Publishers: publishers,
67+
var health string
68+
if inspect.State != nil && inspect.State.Health != nil {
69+
health = inspect.State.Health.Status
70+
}
71+
72+
summary[i] = compose.ContainerSummary{
73+
ID: container.ID,
74+
Name: getCanonicalContainerName(container),
75+
Project: container.Labels[projectLabel],
76+
Service: container.Labels[serviceLabel],
77+
State: container.State,
78+
Health: health,
79+
Publishers: publishers,
80+
}
81+
return nil
6482
})
6583
}
66-
return summary, nil
84+
return summary, eg.Wait()
6785
}
6886

6987
func groupContainerByLabel(containers []moby.Container, labelName string) (map[string][]moby.Container, []string, error) {

0 commit comments

Comments
 (0)