Sometimes, Docker containers fail silently or exit with a non-zero code that isn't caught by standard restart policies. But what happens when a container crashes or exits unexpectedly and --restart=always isn't enough?
In such cases, a custom Docker restart script can act as a watchdog to keep your containers alive and monitored.
Why Use a Restart Script Instead of Docker’s Built-in Policies?
While Docker’s --restart options like always, on-failure, or unless-stopped cover many scenarios, they fall short when:
-
The Docker daemon itself crashes or restarts
-
You want custom logging or notification when a container dies
-
You’re using complex stacks and need to chain actions
That’s where a script comes in handy.
The Script: Restart Docker Containers Automatically
Here's a simple Bash script that monitors and restarts containers by name:
#!/bin/bash
CONTAINER_NAME="my-container"
RESTART_DELAY=5
echo "Monitoring Docker container: $CONTAINER_NAME"
while true
do
RUNNING=$(docker ps -q -f name="^/${CONTAINER_NAME}$")
if [ -z "$RUNNING" ]; then
echo "$(date) - $CONTAINER_NAME not running. Restarting..."
docker start $CONTAINER_NAME
else
echo "$(date) - $CONTAINER_NAME is running."
fi
sleep $RESTART_DELAY
done
#!/bin/bash
CONTAINER_NAME="my-container"
RESTART_DELAY=5
echo "Monitoring Docker container: $CONTAINER_NAME"
while true
do
RUNNING=$(docker ps -q -f name="^/${CONTAINER_NAME}$")
if [ -z "$RUNNING" ]; then
echo "$(date) - $CONTAINER_NAME not running. Restarting..."
docker start $CONTAINER_NAME
else
echo "$(date) - $CONTAINER_NAME is running."
fi
sleep $RESTART_DELAY
done
Your Dynamic Snippet will be displayed here... This message is displayed because you did not provided both a filter and a template to use.
You can use nohup or a service file to start this script. Even you can add in cronjob on @reboot.
So it can start running on reboot. Like:
vim docker-restart-watcher.sh
chmod +x docker-restart-watcher.sh
nohup ./docker-restart-watcher.sh &
nohup script_name.sh
or ./script_name.sh &
You can even monitor multiple containers by wrapping the check inside a loop over a list.
A More Advanced Version
Want to track multiple containers?
#!/bin/bash
CONTAINERS=("web" "worker" "db")
RESTART_DELAY=10 # in seconds
while true
do
for CONTAINER_NAME in "${CONTAINERS[@]}"
do
RUNNING=$(docker ps -q -f name="^/${CONTAINER_NAME}$")
if [ -z "$RUNNING" ]; then
echo "$(date) - $CONTAINER_NAME stopped. Restarting..."
docker start $CONTAINER_NAME
else
echo "$(date) - $CONTAINER_NAME is running."
fi
done
sleep $RESTART_DELAY
done
#!/bin/bash
CONTAINERS=("web" "worker" "db")
RESTART_DELAY=10 # in seconds
while true
do
for CONTAINER_NAME in "${CONTAINERS[@]}"
do
RUNNING=$(docker ps -q -f name="^/${CONTAINER_NAME}$")
if [ -z "$RUNNING" ]; then
echo "$(date) - $CONTAINER_NAME stopped. Restarting..."
docker start $CONTAINER_NAME
else
echo "$(date) - $CONTAINER_NAME is running."
fi
done
sleep $RESTART_DELAY
done
This is how you can restart your docker containers, even if it fails and you are not around.
Hope you find it helpful!!