Skip to content

Commit c5cdce0

Browse files
beneschndeloof
authored andcommitted
Don't wait forever for unhealthy dependencies
The previous code would wait for dependencies to become healthy forever, even if they'd become unhealthy in the meantime. I can't find an issue report for this bug, but it was described in a comment on the PR that introduced the `--wait` flag [0]. [0]: docker#8777 (comment) Signed-off-by: Nikhil Benesch <[email protected]>
1 parent 6dc6bed commit c5cdce0

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

pkg/compose/convergence.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,15 @@ func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Pr
596596
if container.State == nil || container.State.Health == nil {
597597
return false, fmt.Errorf("container for service %q has no healthcheck configured", service)
598598
}
599-
if container.State.Health.Status != moby.Healthy {
599+
switch container.State.Health.Status {
600+
case moby.Healthy:
601+
// Continue by checking the next container.
602+
case moby.Unhealthy:
603+
return false, fmt.Errorf("container for service %q is unhealthy", service)
604+
case moby.Starting:
600605
return false, nil
606+
default:
607+
return false, fmt.Errorf("container for service %q had unexpected health status %q", service, container.State.Health.Status)
601608
}
602609
}
603610
return true, nil
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
services:
2+
fail:
3+
image: alpine
4+
command: sleep infinity
5+
healthcheck:
6+
test: "false"
7+
interval: 1s
8+
retries: 3
9+
depends:
10+
image: alpine
11+
command: sleep infinity
12+
depends_on:
13+
fail:
14+
condition: service_healthy

pkg/e2e/start_fail_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package e2e
18+
19+
import (
20+
"testing"
21+
22+
"gotest.tools/v3/icmd"
23+
)
24+
25+
func TestStartFail(t *testing.T) {
26+
c := NewParallelE2eCLI(t, binDir)
27+
const projectName = "e2e-start-fail"
28+
29+
res := c.RunDockerOrExitError("compose", "-f", "fixtures/start-fail/compose.yaml", "--project-name", projectName, "up", "-d")
30+
res.Assert(t, icmd.Expected{ExitCode: 1, Err: `container for service "fail" is unhealthy`})
31+
32+
c.RunDockerComposeCmd("--project-name", projectName, "down")
33+
}

0 commit comments

Comments
 (0)