mirror of
https://github.com/docker/compose.git
synced 2025-07-23 21:54:40 +02:00
detect dependency failed to start
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
9d12eec148
commit
b8bbdcd872
@ -31,6 +31,7 @@ import (
|
|||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
"github.com/docker/docker/api/types/network"
|
"github.com/docker/docker/api/types/network"
|
||||||
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
|
|
||||||
@ -277,7 +278,7 @@ func containerEvents(containers Containers, eventFunc func(string) progress.Even
|
|||||||
return events
|
return events
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServiceConditionRunningOrHealthy is a service condition on statys running or healthy
|
// ServiceConditionRunningOrHealthy is a service condition on status running or healthy
|
||||||
const ServiceConditionRunningOrHealthy = "running_or_healthy"
|
const ServiceConditionRunningOrHealthy = "running_or_healthy"
|
||||||
|
|
||||||
func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, dependencies types.DependsOnConfig) error {
|
func (s *composeService) waitDependencies(ctx context.Context, project *types.Project, dependencies types.DependsOnConfig) error {
|
||||||
@ -315,7 +316,8 @@ func (s *composeService) waitDependencies(ctx context.Context, project *types.Pr
|
|||||||
case types.ServiceConditionHealthy:
|
case types.ServiceConditionHealthy:
|
||||||
healthy, err := s.isServiceHealthy(ctx, project, dep, false)
|
healthy, err := s.isServiceHealthy(ctx, project, dep, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
w.Events(containerEvents(containers, progress.ErrorEvent))
|
||||||
|
return errors.Wrap(err, "dependency failed to start")
|
||||||
}
|
}
|
||||||
if healthy {
|
if healthy {
|
||||||
w.Events(containerEvents(containers, progress.Healthy))
|
w.Events(containerEvents(containers, progress.Healthy))
|
||||||
@ -644,7 +646,7 @@ func (s *composeService) connectContainerToNetwork(ctx context.Context, id strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Project, service string, fallbackRunning bool) (bool, error) {
|
func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Project, service string, fallbackRunning bool) (bool, error) {
|
||||||
containers, err := s.getContainers(ctx, project.Name, oneOffExclude, false, service)
|
containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true, service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@ -662,6 +664,10 @@ func (s *composeService) isServiceHealthy(ctx context.Context, project *types.Pr
|
|||||||
return container.State != nil && container.State.Status == "running", nil
|
return container.State != nil && container.State.Status == "running", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if container.State.Status == "exited" {
|
||||||
|
return false, fmt.Errorf("container for service %q exited (%d)", service, container.State.ExitCode)
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
10
pkg/e2e/fixtures/dependencies/dependency-exit.yaml
Normal file
10
pkg/e2e/fixtures/dependencies/dependency-exit.yaml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
services:
|
||||||
|
web:
|
||||||
|
image: nginx:alpine
|
||||||
|
depends_on:
|
||||||
|
db:
|
||||||
|
condition: service_healthy
|
||||||
|
db:
|
||||||
|
image: alpine
|
||||||
|
command: sh -c "exit 1"
|
||||||
|
|
@ -123,3 +123,18 @@ func TestUpWithBuildDependencies(t *testing.T) {
|
|||||||
res.Assert(t, icmd.Success)
|
res.Assert(t, icmd.Success)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpWithDependencyExit(t *testing.T) {
|
||||||
|
c := NewParallelCLI(t)
|
||||||
|
|
||||||
|
t.Run("up with dependency to exit before being healthy", func(t *testing.T) {
|
||||||
|
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/dependencies",
|
||||||
|
"-f", "fixtures/dependencies/dependency-exit.yaml", "up", "-d")
|
||||||
|
|
||||||
|
t.Cleanup(func() {
|
||||||
|
c.RunDockerComposeCmd(t, "--project-name", "dependencies", "down")
|
||||||
|
})
|
||||||
|
|
||||||
|
res.Assert(t, icmd.Expected{ExitCode: 1, Err: "dependency failed to start: container for service \"db\" exited (1)"})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user