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

Commit 597598c

Browse files
committed
only start container once we have a wait channel set, to prevent race condition with fast terminating container
see https://github.com/docker/cli/blob/master/cli/command/container/run.go#L157-L168 Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 1dc0c4c commit 597598c

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

local/compose/containers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ func (s *composeService) getContainers(ctx context.Context, project string, oneO
4343
f := filters.NewArgs(
4444
projectFilter(project),
4545
)
46+
if len(selectedServices) == 1 {
47+
f.Add("label", fmt.Sprintf("%s=%s", serviceLabel, selectedServices[0]))
48+
}
4649
switch oneOff {
4750
case oneOffOnly:
4851
f.Add("label", fmt.Sprintf("%s=%s", oneoffLabel, "True"))
@@ -57,7 +60,7 @@ func (s *composeService) getContainers(ctx context.Context, project string, oneO
5760
if err != nil {
5861
return nil, err
5962
}
60-
if len(selectedServices) > 0 {
63+
if len(selectedServices) > 1 {
6164
containers = containers.filter(isService(selectedServices...))
6265
}
6366
return containers, nil

local/compose/convergence.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -337,16 +337,14 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
337337
}
338338

339339
func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Project, service string) (bool, error) {
340-
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
341-
Filters: filters.NewArgs(
342-
projectFilter(project.Name),
343-
serviceFilter(service),
344-
),
345-
})
340+
containers, err := s.getContainers(ctx, project.Name, oneOffExclude, false, service)
346341
if err != nil {
347342
return false, err
348343
}
349344

345+
if len(containers) == 0 {
346+
return false, nil
347+
}
350348
for _, c := range containers {
351349
container, err := s.apiClient.ContainerInspect(ctx, c.ID)
352350
if err != nil {
@@ -355,10 +353,7 @@ func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Pr
355353
if container.State == nil || container.State.Health == nil {
356354
return false, fmt.Errorf("container for service %q has no healthcheck configured", service)
357355
}
358-
switch container.State.Health.Status {
359-
case "starting":
360-
return false, nil
361-
case "unhealthy":
356+
if container.State.Health.Status != moby.Healthy {
362357
return false, nil
363358
}
364359
}

local/compose/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
8989
}
9090
defer restore()
9191

92+
statusC, errC := s.apiClient.ContainerWait(context.Background(), oneoffContainer.ID, container.WaitConditionNextExit)
93+
9294
err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
9395
if err != nil {
9496
return 0, err
@@ -99,7 +101,6 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
99101
return 0, err
100102
}
101103

102-
statusC, errC := s.apiClient.ContainerWait(context.Background(), oneoffContainer.ID, container.WaitConditionNotRunning)
103104
select {
104105
case status := <-statusC:
105106
return int(status.StatusCode), nil

0 commit comments

Comments
 (0)