Skip to content

Docker audit fix · Runtime & Resources

How to Configure Docker Container Restart Policies

A restart policy of always brings a container back forever, so a crashing or exploited one keeps cycling, burning CPU and flooding the logs. CIS asks instead for on-failure with a maximum retry count, typically 5, so a real fault gets noticed.

Hiren KalariyaHiren KalariyaLast reviewed restart-policy
CtrlOpsContainer Restart PolicyLOW

What did your audit say?

WARN · when the audit printed

Some use always

The named containers will be restarted indefinitely, so a crash loop can run unnoticed until someone reads the logs.

Severity
Low
Needs root or sudo
Yes
Results it can return
WARN · SKIP · PASS
Check in this audit
6 of 6

Every threshold on this page is transcribed from the docker-security-runtime-resources audit script that CtrlOps runs, and a build check fails if the two ever disagree. See all 6 Runtime & Resources checks, or how the audit runs.

The check inspects each running container with the template {{if eq .HostConfig.RestartPolicy.Name "always"}}{{.Name}} {{end}}, so it reads one field, .HostConfig.RestartPolicy.Name, and compares it against a single exact string: always. Every other value is left alone, including unless-stopped, on-failure and no, and .HostConfig.RestartPolicy.MaximumRetryCount is never read, so an on-failure policy passes whether or not it has a retry ceiling. An empty match list is the PASS; otherwise the WARN counts the matching containers and names up to five.

Needs a reachable daemon and at least one running container; otherwise SKIP. It matches one exact value: .HostConfig.RestartPolicy.Name == "always". Nothing else is flagged - notably unless-stopped, which behaves the same as always except after a manual stop, passes this check while providing the same unbounded restart behaviour CIS objects to. no and on-failure (with or without a retry count) also pass. This is an advisory CIS item that many teams deliberately reject; the severity is LOW for that reason.

What each result means
ResultWhenWhat it means
SKIPNo running containersNothing was verified. No restart policy was read, so this says nothing about how any container behaves when it crashes.
PASSNo container uses alwaysNo running container is set to restart forever, though unless-stopped also passes here and behaves almost identically.
WARNSome use alwaysThe named containers will be restarted indefinitely, so a crash loop can run unnoticed until someone reads the logs.

This check follows CIS Docker 5.14 "Ensure that the 'on-failure' container restart policy is set to '5'": an unbounded restart loop (always) keeps a crashing or exploited container cycling, consuming CPU and flooding logs, and can hide an attack. on-failure:5 gives up after five attempts so the operator notices.

  • Docker's own documentation recommends unless-stopped or always for long-running services so they survive reboots; CIS's on-failure:5 means a service that crashes five times at boot (for example because a database dependency is slow) stays down until someone intervenes. Many teams deliberately reject this CIS item and rely on health checks plus alerting instead. Treat as advisory and document the decision.
  • unless-stopped is functionally the same as always except after a manual stop; it is not flagged by this check, so switching to it silences the WARN without changing the restart behaviour that CIS is concerned about.
  • Docker doubles the wait between restarts, starting at 100 ms and capped at a minute, and resets it once a container stays up for 10 seconds. That slows a tight crash loop but never ends it.
  • In Compose, the service-level restart: key sets this policy; deploy.restart_policy is a Swarm setting.
  1. Find the containers using always

    The WARN message names up to five. For the full list:

    sh
    docker inspect -f '{{.Name}} {{.HostConfig.RestartPolicy.Name}}' $(docker ps -q) | grep ' always$'

  2. Switch to on-failure with a retry cap

    docker update changes the policy on a running container without recreating it. Set the same value in Compose, or the next docker compose up puts always back.

    sh
    docker update --restart on-failure:5 <name>

    yaml
    services:  app:    restart: on-failure:5

  3. Alternative

    Or keep a persistent policy with health checks and alerts

    If a service must come back after a reboot on its own, keeping always or unless-stopped is a defensible choice. Make crash loops visible instead: add a HEALTHCHECK, alert when RestartCount rises, and record the decision as an accepted exception. Do not switch to unless-stopped only to clear this warning.

  4. Confirm the policy and restart counts

    sh
    docker inspect -f '{{.Name}} {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}} restarts={{.RestartCount}}' $(docker ps -q)

sh
docker inspect -f '{{.Name}} {{.HostConfig.RestartPolicy.Name}}:{{.HostConfig.RestartPolicy.MaximumRetryCount}}' $(docker ps -q)# expected under CIS: on-failure:5# Whether restarts are actually happening - usually the more useful signal:docker inspect -f '{{.Name}} restarts={{.RestartCount}}' $(docker ps -q)docker ps --format 'table {{.Names}}\t{{.Status}}'      # "Restarting (1) 5 seconds ago" is the red flagdocker inspect -f '{{.Name}} health={{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' $(docker ps -q)

Debugging

  • This one message covers two different cases: the audit could not reach the Docker daemon, or it reached it and nothing was running. Run docker ps as the audit user, then sudo docker ps. If both fail, the daemon is unreachable: check systemctl status docker, then run the audit as root or give the account sudo. If they work but list nothing, there is genuinely nothing to evaluate, and a stopped container is never checked. The Docker Inventory check reports the two cases separately, so its result tells you which one you hit.
  • The most common outcome. Docker's own guidance recommends always/unless-stopped for long-running services so they survive a reboot, while CIS wants on-failure:5 so a repeatedly crashing container stays down and gets noticed. Both positions are defensible; pick one, and if you keep always, record this as an accepted exception with health checks and restart-count alerting in place.
  • Worth knowing that this changed almost nothing: unless-stopped restarts just as persistently, it merely remembers a manual stop across daemon restarts. If you were aiming at the CIS intent, use on-failure:5.
  • A slow dependency, such as a database not yet listening, can use up all five attempts. Add a HEALTHCHECK and depends_on: condition: service_healthy, or make the entrypoint wait for its dependency, before adopting the CIS setting.
  • The restart policy is hiding the real fault. docker logs --tail 50 <name> and docker inspect -f '{{.State.ExitCode}} {{.RestartCount}}' show why. Docker's growing delay between restarts keeps the CPU cost of a tight loop low, but the loop itself goes on until the cause is fixed.
  • docker update changes the running container only. When Compose next recreates it, the restart: value in the Compose file wins, so set restart: on-failure:5 there as well.

Sources

Next

Re-run the Runtime & Resources audit after applying the fix and confirm this check moves to PASS. CtrlOps runs all 6 checks over your existing SSH connection and scores the result, so the change is visible without reading another config file.

Download for MacDownload for WindowsAll 6 Runtime & Resources fixes

14-day free trial, no credit card required.

Explore article with AI tools

Written by

Hiren Kalariya

Co-Founder & CEO, TST Technology

Hiren Kalariya is the Co-Founder and CEO of TST Technology and the product lead behind CtrlOps, a local-first desktop app that makes Linux server management fast and easy for developers and DevOps teams. Over the past five years he has shipped 60+ production systems, and he still tests every tool he writes about on real servers.

Stop juggling tools. Start managing fleets.

PuTTY
Termius
MobaXterm
WinSCP
SecureCRT
Royal TS
Warp
CtrlOps14 tools left
Webmin
aaPanel
RunCloud
Dokploy
IP spreadsheet
bash (2)
sticky notes

One app. All your servers. No context switching. Free to start, free to leave.

Start instantly · No credit card · Cancel anytime

Your privacy preferences. We use product analytics to improve CtrlOps. Google Analytics cookies and live chat stay off until you allow them. Cookie policy