Skip to content

Instantly share code, notes, and snippets.

@Snuffy2
Last active June 20, 2023 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Snuffy2/1d49250df3a5c8fdb3a24d486df92015 to your computer and use it in GitHub Desktop.
Save Snuffy2/1d49250df3a5c8fdb3a24d486df92015 to your computer and use it in GitHub Desktop.
Combine Autoheal with Webhookd to restart Docker Compose containers respecting depends_on
#!/bin/bash
NOW=$(date +'%m/%d %I:%M:%S %P')
LOGFILE="/opt/scripts/autoheal.log"
echo "${NOW}:" | tee -a ${LOGFILE}
echo "-------------------------------------------------------------" | tee -a ${LOGFILE}
echo "Hook information: name=${hook_name}, id=${hook_id}, method=${hook_method}" | tee -a ${LOGFILE}
echo "Header parameter: user-agent=${user_agent}" | tee -a ${LOGFILE}
#echo "Script parameters: ${@}" | tee -a ${LOGFILE}
AUTOHEAL_TEXT=$(echo ${@} | jq '.text' | tr -d '"')
echo "Text: ${AUTOHEAL_TEXT}" | tee -a ${LOGFILE}
if [[ "${AUTOHEAL_TEXT}" == "Container vpn ("* ]] && [[ "${AUTOHEAL_TEXT}" == *"Successfully restarted the container!" ]]
then
echo "VPN Restarted, running docker restart excluding vpn script" | tee -a ${LOGFILE}
/home/pi/scripts/pushover.sh "VPN Restarted, running docker restart excluding vpn script. (ID: ${hook_id})" -2
/opt/scripts/triggered/vpn_restart_docker.sh &
elif [[ "${AUTOHEAL_TEXT}" == "Container vpn ("* ]] && [[ "${AUTOHEAL_TEXT}" == *"Failed to restart the container!" ]]
then
echo "VPN Restart Failed, running full docker restart script" | tee -a ${LOGFILE}
/home/pi/scripts/pushover.sh "VPN Restart Failed, running full docker restart script. (ID: ${hook_id})" -1
/opt/scripts/triggered/full_restart_docker.sh &
elif [[ "${AUTOHEAL_TEXT}" == *"Successfully restarted the container!" ]]
then
/home/pi/scripts/pushover.sh "${AUTOHEAL_TEXT} (ID: ${hook_id})" -2
elif [[ "${AUTOHEAL_TEXT}" == *"Failed to restart the container!" ]]
then
/home/pi/scripts/pushover.sh "${AUTOHEAL_TEXT} (ID: ${hook_id})" 0
fi
echo | tee -a ${LOGFILE}
version: '3.8'
networks:
default:
driver: bridge
ipam:
config:
- subnet: 172.16.100.0/24
services:
autoheal:
image: willfarrell/autoheal
container_name: autoheal
restart: unless-stopped
logging:
driver: json-file
environment:
- TZ=America/New_York
- AUTOHEAL_START_PERIOD=300
- AUTOHEAL_INTERVAL=300
- "WEBHOOK_URL=http://172.16.100.1:12345/autoheal"
- AUTOHEAL_CONTAINER_LABEL=all
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
- /etc/localtime:/etc/localtime:ro
#!/bin/bash
LOGFILE="/opt/scripts/autoheal.log"
echo "Fully Restarting docker containers" | tee -a ${LOGFILE}
echo "Stopping Containers" | tee -a ${LOGFILE}
docker compose -f /docker/docker-compose/docker-compose.yml --env-file /docker/docker-compose/.env --ansi never down
echo "Waiting 5 seconds"
sleep 5
echo "Restarting Containers" | tee -a ${LOGFILE}
docker compose -f /docker/docker-compose/docker-compose.yml --env-file /docker/docker-compose/.env --ansi never up -d &
echo "Done" | tee -a ${LOGFILE}
echo | tee -a ${LOGFILE}
#!/bin/bash
LOGFILE="/opt/scripts/autoheal.log"
echo "Restarting docker containers dependent on VPN" | tee -a ${LOGFILE}
echo "Stopping Containers" | tee -a ${LOGFILE}
docker stop $(docker ps -a | grep -v "vpn" | awk 'NR>1 {print $1}') >/dev/null 2>&1
echo "Waiting 10 seconds"
sleep 10
echo "Restarting Containers" | tee -a ${LOGFILE}
docker compose -f /docker/docker-compose/docker-compose.yml --env-file /docker/docker-compose/.env --ansi never up -d &
echo "Done" | tee -a ${LOGFILE}
echo | tee -a ${LOGFILE}
# Output hook execution logs to server logs, default is false
WHD_HOOK_LOG_OUTPUT=true
# Maximum hook execution time in second, default is 10
WHD_HOOK_TIMEOUT=300
# HTTP listen address, default is ":8080"
# Example: `localhost:8080` or `:8080` for all interfaces
WHD_LISTEN_ADDR=":12345"
# Scripts location, default is "scripts"
WHD_SCRIPTS="/opt/scripts"
@Snuffy2
Copy link
Author

Snuffy2 commented Jun 20, 2023

Instructions

Prerequisites:

https://github.com/willfarrell/docker-autoheal
https://github.com/ncarlier/webhookd

Why?

Like others, I was trying to find a way to restart dockers when a main docker that the others rely on is restarted. The primary example for this is a VPN Docker. This will detect when a VPN docker restarts by Autoheal and then restarts all of the other dockers. It can be expanded upon or adapted for many other purposes. I installed webhookd locally, but it could also be installed via Docker or on another computer as needed.

My Setup

Raspberry Pi 4 running Debian Bullseye

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment