Request icon
OutputValues icon
Loop icon
Log icon
If icon
SlackIncomingWebhook icon
Pause icon
Commands icon
Docker icon
Sleep icon
Fail icon
Schedule icon

Restart Docker Containers That Are Up but Failing Their Healthcheck

Find running Docker containers whose healthcheck reports unhealthy, restart them behind an approval, then re-check health and fail if any stays unhealthy.

Categories
Infrastructure

A container with a HEALTHCHECK can be up and failing at the same time. After the configured number of consecutive failed checks the daemon marks it unhealthy, but the process never exited, so a restart policy never fires. Restart policies act on exit, not on health. The daemon keeps reporting the container as running. A load balancer may keep routing to it. It sits there serving errors. Nothing restarts it.

This blueprint restarts it, inside a boundary you set. It reads the health status of every container carrying a label you apply yourself, keeps a protect-list, caps how many go in one run, then runs docker restart on each unhealthy one behind a dry run and an approval. It waits one settle window for the healthcheck to run again, inspects every target once more, then fails the run if any target is still unhealthy.

This blueprint was created by zkasuran.

Safety

Restarting a container interrupts what it is serving, so the flow is built so it cannot reach anything but its own target:

  • Label scoping. The Engine API query filters on label_selector, so a container without that label is never returned, let alone restarted. The label is one you apply to the containers you want this flow to manage.
  • Has a healthcheck. A container with no HEALTHCHECK has no health to act on, so it is read and kept, never a target.
  • A protect-list. protected_name_pattern wins over every other rule. A stateful service you do not want bounced mid-recovery stays untouched even when it is unhealthy.
  • A ceiling. max_restarts bounds one run, so a selector that matches more than you expected cannot bounce a whole host in one execution.
  • One target, named by you. Every read goes to docker_host over the Engine API and the restart goes to the same endpoint through DOCKER_HOST. The flow holds no socket path and no hostname of its own, so the daemon that runs Kestra is never a target.
  • Three gates before any write. auto_remediate true, dry_run false, then an approved Pause. The shipped defaults and the disabled schedule are read-only.
  • A settle before judging. A container reads as starting right after a restart, so the re-check waits settle_seconds for the healthcheck to run again before it decides pass or fail.

How it works

  1. list_labelled (io.kestra.plugin.core.http.Request, read-only) asks the Docker Engine API for running containers matching label_selector. The Docker plugin has no container list task, so the inventory comes from the API. candidates keeps the ids and names.

  2. inspect_each (io.kestra.plugin.core.flow.Loop, read-only) inspects each candidate. State.Health.Status, State.Health.FailingStreak and the last healthcheck output are in the inspect payload only, not in the list.

  3. assess gives every candidate a verdict:

    • KEEP_NO_HEALTHCHECK: no HEALTHCHECK, so there is no health to act on;
    • KEEP_PROTECTED: its name matches protected_name_pattern;
    • KEEP_HEALTHY: the healthcheck reports healthy;
    • KEEP_STARTING: the healthcheck has not settled yet;
    • KEEP_OVER_CAP: unhealthy, but beyond max_restarts for this run;
    • RESTART: running, has a healthcheck, reports unhealthy, unprotected, inside the cap.

    summary logs the counts and every target with its failing streak.

  4. route stops with a log line when nothing is unhealthy. Otherwise findings_alert posts the plan to Slack and remediate_gate checks auto_remediate and dry_run.

  5. hold (io.kestra.plugin.core.flow.Pause) waits for approval. remediate_restart (Loop, concurrencyLimit: 1) then runs one docker restart per target through io.kestra.plugin.scripts.shell.Commands on the docker:27-cli image, pointed at docker_host by DOCKER_HOST.

  6. settle (io.kestra.plugin.core.flow.Sleep) waits settle_seconds so the healthcheck runs again. reverify_each inspects every target again by name. result sorts them into recovered, still unhealthy, still starting and gone. verify fails the run if any target did not come back to healthy.

  7. errors alerts Slack when the flow fails, including when the verification gate fails.

What you get

  • A failing-but-up container caught from its health status, which a restart policy and a plain docker ps both miss.
  • A recovery that a human approves, with the before and after health status in the execution log as the evidence.
  • A run that fails when the restart did not fix the health, so a container still serving errors cannot read as recovered.

Who it's for

  • Teams running services on plain Docker hosts or Docker Compose, where a healthcheck turns red but nothing acts on it.
  • Platform teams operating build hosts, edge nodes or self-hosted runners whose containers wedge without exiting.
  • Anyone who has found a container marked unhealthy for hours while traffic kept hitting it.

Why orchestrate this with Kestra

A cron job can run docker restart across a host, but it cannot read the health status first, cannot keep a protect-list, cannot stop at a ceiling, cannot wait for an approval and cannot check that the restart actually fixed the health. That last part is the point. The run is green only when the target is running again and reports healthy. Kestra reads the host, computes the plan, waits for a human, restarts one container at a time so a failure is attributable, waits out one healthcheck window, then reads the host again and fails the execution when a target is still unhealthy. The execution history is the record of which container was restarted on which host, who approved it and whether it recovered.

Tested end to end

On Kestra 2.0.5 OSS against an isolated Docker 27.5.1 daemon (Docker-in-Docker on its own network), with a local HTTP sink standing in for Slack. The daemon held five containers inside it: recover (labelled, healthcheck wired to a file the entrypoint recreates, made unhealthy by deleting the file so a restart fixes it), wedged (labelled, healthcheck exit 1, so it stays unhealthy after any restart), healthy (labelled, healthy, untouched), nohealth (labelled, no healthcheck) and unlabelled (unhealthy, no label). Every health status below was cross-checked with docker inspect against that daemon.

Run Inputs Result
1 only healthy and nohealth present, defaults 2 read, 0 targets, nothing_unhealthy. Nothing restarted
2 all five present, defaults 4 labelled read, 2 targets (recover, wedged). healthy kept as KEEP_HEALTHY, nohealth as KEEP_NO_HEALTHCHECK, unlabelled never returned by the query. Nothing restarted
3 run 2 plus auto_remediate: true Same plan, nothing restarted. The dry run blocks the write even with auto_remediate on
4 auto_remediate: true, dry_run: false, pause resumed approved: false 2 targets, nothing restarted. docker inspect after: both still unhealthy, start time unchanged
5 auto_remediate: true, dry_run: false, protected_name_pattern set to wedged, pause approved 1 target (recover), restarted, healthy after the settle. docker inspect: recover unhealthy to healthy, start time moved. wedged kept by the protect-list, untouched
6 auto_remediate: true, dry_run: false, target wedged, pause approved wedged restarted, still unhealthy after the settle, so verify failed the run and errors posted to Slack. docker inspect: start time moved, health still unhealthy
7 unreachable daemon errors alert sent

Prerequisites

  • A Docker daemon reachable over TCP from the Kestra worker. The container list comes from the Engine API, which the worker cannot read through a mounted unix socket.
  • A HEALTHCHECK on the containers you want managed, so the daemon has a health status to report. A container with no healthcheck is read and kept, never restarted.
  • A label on those containers. Add --label kestra.health=managed to your docker run or to the service in your Compose file, then point label_selector at it.
  • The one-off docker:27-cli container must be able to reach docker_host. Leave docker_cli_network as bridge for a daemon that resolves by DNS. Point it at the user-defined network when the daemon is itself a container.
  • Local testing: docker run -d --privileged --name dind -e DOCKER_TLS_CERTDIR= docker:27-dind --host=tcp://0.0.0.0:2375, start a labelled container inside it with a healthcheck wired to fail, then set docker_host to tcp://dind:2375.

Secrets

  • SLACK_WEBHOOK_URL: used by findings_alert and the errors block. In Kestra OSS, provide it as the base64-encoded environment variable SECRET_SLACK_WEBHOOK_URL.

Inputs

Input Default Purpose
docker_host tcp://docker-host:2375 Daemon to watch
docker_cli_network bridge Network the docker CLI container joins
label_selector kestra.health=managed The only containers read
protected_name_pattern ^(db|database|state|broker|queue|cache)([-_.].*)?$ Names never restarted
restart_stop_timeout_seconds 10 Grace before kill on restart
settle_seconds 45 Wait before judging health again
max_restarts 25 Ceiling for one run
auto_remediate false Must be true to restart anything
dry_run true Must be false to restart anything

Quick start

  1. Add the SLACK_WEBHOOK_URL secret.
  2. Label the containers you want managed, for example docker run --health-cmd ... --label kestra.health=managed ....
  3. Import the flow, set docker_host and label_selector, run it with the defaults and read the plan.
  4. Set settle_seconds above your healthcheck interval, so the re-check does not judge a container that is still starting.
  5. Run it with auto_remediate: true and dry_run: false, read the plan in the paused execution, then resume it with approved: true.
  6. Enable the nightly trigger once the plans look right. Change its inputs when you want it to act.

How to extend

  • Trigger the flow from a webhook your monitoring fires on an unhealthy event, so the restart happens in seconds rather than on a schedule.
  • Keep a restart count per container in a KV store, so a container restarted every run is escalated to a human instead of bounced forever.
  • Replace the restart with a stop plus an alert for containers that have been restarted too many times, since a repeat offender is a bug no restart fixes.
  • Loop over a list of hosts to watch a whole fleet in one execution.
  • Read the last healthcheck output into the Slack alert, so the message carries why the container is unhealthy.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.