
Self-Healing Docker: Bash Script That Auto-Restarts Containers
Manual restarts during incidents are reactive. Self-healing means your containers recover themselves between alerts. This post shows how to build a lightweight bash watchdog that: Monitors container health via Docker health checks Restarts unhealthy containers Integrates with systemd for daemon-like behavior Logs everything for incident review The Self-Healing Architecture Docker has built-in restart policies ( --restart unless-stopped ), but they don’t respect health checks. A container can be "running" but unhealthy (app crashed, dependencies down, etc.). Our script loops every 30 seconds: Query Docker API for container health Restart unhealthy containers Log actions to systemd journal Repeat Step 1: Docker Health Checks (the foundation) First, ensure your containers have proper health checks in docker-compose.yml or docker run : services : web : image : nginx healthcheck : test : [ " CMD" , " curl" , " -f" , " http://localhost/health" ] interval : 30s timeout : 10s retries : 3 start
Continue reading on Dev.to DevOps
Opens in a new tab


