diff --git a/provider/docker.go b/provider/docker.go index 411f18696..50259d6bf 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -58,6 +58,7 @@ type dockerData struct { Name string Labels map[string]string // List of labels set to container or service NetworkSettings networkSettings + Health string } // NetworkSettings holds the networks data to the Docker provider @@ -214,6 +215,9 @@ func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, po } eventHandler.Handle("start", startStopHandle) eventHandler.Handle("die", startStopHandle) + eventHandler.Handle("health_status: healthy", startStopHandle) + eventHandler.Handle("health_status: unhealthy", startStopHandle) + eventHandler.Handle("health_status: starting", startStopHandle) errChan := events.MonitorWithHandler(ctx, dockerClient, options, eventHandler) if err := <-errChan; err != nil { @@ -378,6 +382,11 @@ func (provider *Docker) containerFilter(container dockerData) bool { return false } + if container.Health != "" && container.Health != "healthy" { + log.Debugf("Filtering unhealthy or starting container %s", container.Name) + return false + } + return true } @@ -578,6 +587,10 @@ func parseContainer(container dockertypes.ContainerJSON) dockerData { } + if container.State != nil && container.State.Health != nil { + dockerData.Health = container.State.Health.Status + } + return dockerData } diff --git a/provider/docker_test.go b/provider/docker_test.go index 45f5ccbd8..3d106bd71 100644 --- a/provider/docker_test.go +++ b/provider/docker_test.go @@ -569,12 +569,18 @@ func TestDockerGetLabel(t *testing.T) { }{ { container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "foo", + }, Config: &container.Config{}, }, expected: "Label not found:", }, { container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "foo", + }, Config: &container.Config{ Labels: map[string]string{ "foo": "bar", @@ -608,6 +614,9 @@ func TestDockerGetLabels(t *testing.T) { }{ { container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "foo", + }, Config: &container.Config{}, }, expectedLabels: map[string]string{}, @@ -615,6 +624,9 @@ func TestDockerGetLabels(t *testing.T) { }, { container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "foo", + }, Config: &container.Config{ Labels: map[string]string{ "foo": "fooz", @@ -628,6 +640,9 @@ func TestDockerGetLabels(t *testing.T) { }, { container: docker.ContainerJSON{ + ContainerJSONBase: &docker.ContainerJSONBase{ + Name: "foo", + }, Config: &container.Config{ Labels: map[string]string{ "foo": "fooz",