mirror of
https://github.com/docker/compose.git
synced 2025-07-28 08:04:09 +02:00
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 <nicolas.deloof@gmail.com>
This commit is contained in:
parent
1dc0c4cef9
commit
597598cb7a
@ -43,6 +43,9 @@ func (s *composeService) getContainers(ctx context.Context, project string, oneO
|
|||||||
f := filters.NewArgs(
|
f := filters.NewArgs(
|
||||||
projectFilter(project),
|
projectFilter(project),
|
||||||
)
|
)
|
||||||
|
if len(selectedServices) == 1 {
|
||||||
|
f.Add("label", fmt.Sprintf("%s=%s", serviceLabel, selectedServices[0]))
|
||||||
|
}
|
||||||
switch oneOff {
|
switch oneOff {
|
||||||
case oneOffOnly:
|
case oneOffOnly:
|
||||||
f.Add("label", fmt.Sprintf("%s=%s", oneoffLabel, "True"))
|
f.Add("label", fmt.Sprintf("%s=%s", oneoffLabel, "True"))
|
||||||
@ -57,7 +60,7 @@ func (s *composeService) getContainers(ctx context.Context, project string, oneO
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(selectedServices) > 0 {
|
if len(selectedServices) > 1 {
|
||||||
containers = containers.filter(isService(selectedServices...))
|
containers = containers.filter(isService(selectedServices...))
|
||||||
}
|
}
|
||||||
return containers, nil
|
return containers, nil
|
||||||
|
@ -337,16 +337,14 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Project, service string) (bool, error) {
|
func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Project, service string) (bool, error) {
|
||||||
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
containers, err := s.getContainers(ctx, project.Name, oneOffExclude, false, service)
|
||||||
Filters: filters.NewArgs(
|
|
||||||
projectFilter(project.Name),
|
|
||||||
serviceFilter(service),
|
|
||||||
),
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(containers) == 0 {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
container, err := s.apiClient.ContainerInspect(ctx, c.ID)
|
container, err := s.apiClient.ContainerInspect(ctx, c.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -355,10 +353,7 @@ func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Pr
|
|||||||
if container.State == nil || container.State.Health == nil {
|
if container.State == nil || container.State.Health == nil {
|
||||||
return false, fmt.Errorf("container for service %q has no healthcheck configured", service)
|
return false, fmt.Errorf("container for service %q has no healthcheck configured", service)
|
||||||
}
|
}
|
||||||
switch container.State.Health.Status {
|
if container.State.Health.Status != moby.Healthy {
|
||||||
case "starting":
|
|
||||||
return false, nil
|
|
||||||
case "unhealthy":
|
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,6 +89,8 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
|
|||||||
}
|
}
|
||||||
defer restore()
|
defer restore()
|
||||||
|
|
||||||
|
statusC, errC := s.apiClient.ContainerWait(context.Background(), oneoffContainer.ID, container.WaitConditionNextExit)
|
||||||
|
|
||||||
err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
|
err = s.apiClient.ContainerStart(ctx, containerID, apitypes.ContainerStartOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -99,7 +101,6 @@ func (s *composeService) RunOneOffContainer(ctx context.Context, project *types.
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
statusC, errC := s.apiClient.ContainerWait(context.Background(), oneoffContainer.ID, container.WaitConditionNotRunning)
|
|
||||||
select {
|
select {
|
||||||
case status := <-statusC:
|
case status := <-statusC:
|
||||||
return int(status.StatusCode), nil
|
return int(status.StatusCode), nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user