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

Set a Restart Policy on Running Docker Containers Without Restarting Them

Find running Docker containers with no restart policy, set it in place behind an approval, then re-read it and prove the service was not interrupted.

Categories
Infrastructure

A container started with --restart=no, which is the default, never comes back. The host reboots or dockerd restarts, then that container stays down. Nothing alerts, because the daemon did exactly what it was told. Someone notices hours later when a queue stops draining or a health check page is missing a tile.

This blueprint finds those containers while they are still up and fixes them in place. It reads the restart policy of every container carrying a label you apply yourself, keeps anything on a protect-list, caps how many change in one run, then writes the policy you chose with docker update behind a dry run and an approval. Every labelled container is inspected again afterwards. The run fails if a target is still unprotected or if the workload was disturbed.

This blueprint was created by zkasuran.

Safety

The write is a configuration rewrite, which is why this flow can act on a running service at all:

  • docker update does not restart anything. It rewrites HostConfig.RestartPolicy on the existing container. The process keeps running and the container keeps its id. The policy takes effect at the next stop. This is the whole reason the flow is safe to run against production containers, where a reaper or a recreate is not.
  • It is proven, not assumed. The container id and State.StartedAt are captured before the change and read again after it. A target counts as hardened only when the policy is the one asked for and the id, the start time and the running state are all unchanged. A recreated container comes back with a new id, so it cannot read as untouched.
  • Label scoping. The Engine API query filters on label_selector, so a container without that label is never returned, let alone changed.
  • A protect-list. protected_name_pattern wins over every other rule. One-shot jobs belong there, since a job meant to run once should stay with no restart policy.
  • A ceiling. max_updates bounds one run, so a selector that matches more than you expected cannot rewrite a whole host in one execution.
  • 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.
  • One target, named by you. Every read goes to docker_host over the Engine API and every write 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.

How it works

  1. list_running (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. HostConfig.RestartPolicy, State.StartedAt and RestartCount are in the inspect payload only, not in the list.

  3. assess gives every candidate a verdict. An empty policy name and the literal no both mean the daemon will leave the container down:

    • KEEP_POLICY_SET: it already has a policy;
    • KEEP_PROTECTED: its name matches protected_name_pattern;
    • KEEP_NOT_RUNNING: the daemon no longer reports it running;
    • KEEP_OVER_CAP: eligible, but beyond max_updates for this run;
    • HARDEN: running, unprotected, no policy, inside the cap.

    summary logs the counts, every target and its start time.

  4. route stops with a log line when there is nothing to harden. 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_update (Loop, concurrencyLimit: 1) then runs one docker update --restart=<policy> <id> per target through io.kestra.plugin.scripts.shell.Commands on the docker:27-cli image, pointed at docker_host by DOCKER_HOST.

  6. reverify_each inspects every labelled container again by name. result joins the plan with the host. verify fails the run in three cases: a target still unprotected, a hardened container that lost its id or its start time, a container the plan kept that changed.

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

What you get

  • The reboot problem found while the service is still up, instead of after the next restart.
  • A fix that does not interrupt anything, with the before and after container id and start time in the execution log as the evidence.
  • A run that fails when the policy did not land, so a host that is still exposed cannot read as a clean one.

Who it's for

  • Teams running services on plain Docker hosts or Docker Compose, without an orchestrator that restarts workloads for them.
  • Platform teams who inherited hosts where containers were started by hand and the flags were never audited.
  • Anyone who has had a service stay down after a host reboot because one container was started without --restart.

Why orchestrate this with Kestra

A shell loop can run docker update across a host, but it cannot hold the change behind an approval, cannot keep a protect-list, cannot stop at a ceiling and cannot prove it left the running workload alone. That last part is what makes this safe to automate: the run is only green when the policy changed and the container id, the start time and the running state did not. Kestra reads the host, computes the plan, waits for a human, writes one container at a time so a failure is attributable, then reads the host again and fails the execution when the result does not match the plan. The execution history becomes the record of which host was hardened, when it happened and who approved it.

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 four running containers: app-ledger (labelled, --restart=no), app-unlabelled (no label, --restart=no), app-already (labelled, already unless-stopped), migrate-once (labelled, --restart=no, matching the protect-list).

Run Inputs Result
1 defaults 3 labelled containers read, 1 target (app-ledger). app-already kept as KEEP_POLICY_SET, migrate-once kept by the protect-list, app-unlabelled never returned by the query
2 auto_remediate: true Same plan, nothing changed. The dry run blocks the write even with auto_remediate on
3 run 2 plus dry_run: false, pause approved app-ledger went from no to unless-stopped. Same container id, same State.StartedAt, still running, RestartCount still 0. The other three untouched, confirmed with docker inspect on the daemon itself
4 same, pause resumed with approved: false Nothing changed
5 rerun of run 3 on the now clean host 0 targets, the nothing_to_harden branch
6 same as run 3, but the target was recreated during the pause The policy read back correct while the container id had changed, so verify failed the run and errors posted to Slack
7 max_updates: 1 with two eligible containers 1 target, the other reported as KEEP_OVER_CAP
8 unreachable daemon errors alert sent

Proof the whole thing is worth doing: the daemon was restarted after run 3. app-ledger came back by itself, app-unlabelled stayed down.

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 label on the containers you want managed. Add --label kestra.restart-policy=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 few labelled containers inside it, 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 harden
docker_cli_network bridge Network the docker CLI container joins
label_selector kestra.restart-policy=managed The only containers read
desired_policy unless-stopped Policy to write
protected_name_pattern ^(batch|job|migrate|oneshot|init)([-_.].*)?$ Names left alone
max_updates 25 Ceiling for one run
auto_remediate false Must be true to write anything
dry_run true Must be false to write anything

Quick start

  1. Add the SLACK_WEBHOOK_URL secret.
  2. Label the containers you want managed, for example docker run --label kestra.restart-policy=managed ....
  3. Import the flow, set docker_host and label_selector, run it with the defaults and read the plan.
  4. Run it with auto_remediate: true and dry_run: false, read the plan in the paused execution, then resume it with approved: true.
  5. Enable the nightly trigger once the plans look right. Change its inputs when you want it to act.

How to extend

  • Normalise a policy you disagree with, not only a missing one, by comparing the current policy with desired_policy instead of treating any policy as acceptable.
  • Add on-failure:N to desired_policy for workloads that should retry a crash a fixed number of times, then split the retry count off the value before it reaches the CLI.
  • Loop over a list of hosts to harden a whole fleet in one execution.
  • Run it from a host boot event rather than a schedule, so the audit happens right after the reboot that would have exposed the gap.
  • Record the hardened ids in a KV store to show how often containers are started without a policy, which is a signal about the deploy path rather than the host.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.