diff --git a/Dockerfile b/Dockerfile index 9ded7f4..1d73a7a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,8 @@ ENV AUTOHEAL_CONTAINER_LABEL=autoheal \ CURL_TIMEOUT=30 \ WEBHOOK_URL="" \ WEBHOOK_JSON_KEY="content" \ - APPRISE_URL="" + APPRISE_URL="" \ + POST_RESTART_SCRIPT="" HEALTHCHECK --interval=5s CMD pgrep -f autoheal || exit 1 diff --git a/README.md b/README.md index 9638d92..956122e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # Docker Autoheal +Monitor and restart unhealthy docker containers. Monitor and restart unhealthy docker containers. This functionality was proposed to be included with the addition of `HEALTHCHECK`, however didn't make the cut. This container is a stand-in till there is native support for `--exit-on-unhealthy` https://github.com/docker/docker/pull/22719. @@ -76,6 +77,7 @@ CURL_TIMEOUT=30 # --max-time seconds for curl requests to Docker API WEBHOOK_URL="" # post message to the webhook if a container was restarted (or restart failed) WEBHOOK_JSON_KEY="content" # the key to use for the message in the json body of the request to the webhook url APPRISE_URL="" # post message to Apprise if a container was restarted (or restart failed) +POST_RESTART_SCRIPT="" # Run the specified script if a container was restarted (or restart failed). Script is run from inside the container. Make sure to mount a host directory with the script you want to run. e.g. `-v ./scripts:/scripts` ``` ### Optional Container Labels @@ -83,12 +85,30 @@ APPRISE_URL="" # post message to Apprise if a container was restarted (or res autoheal.stop.timeout=20 # Per containers override for stop timeout seconds during restart ``` +### Post-Restart Script + +Here's an example of how you can execute a script after a restart. + +The following values are passed as arguments: `CONTAINER_NAME`, `CONTAINER_SHORT_ID`, `CONTAINER_STATE`, `RESTART_TIMEOUT`. + +```bash +docker build -t autoheal . + +docker run -d \ + -e AUTOHEAL_CONTAINER_LABEL=all \ + -e POST_RESTART_SCRIPT=/scripts/post_restart.sh \ + -v ./scripts:/scripts \ + -v /var/run/docker.sock:/var/run/docker.sock \ + autoheal +``` + ## Testing ```bash docker build -t autoheal . docker run -d \ -e AUTOHEAL_CONTAINER_LABEL=all \ + -e POST_RESTART_SCRIPT=/scripts/post_restart.sh -v /var/run/docker.sock:/var/run/docker.sock \ autoheal ``` diff --git a/docker-entrypoint b/docker-entrypoint index 80a7825..e171e52 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -77,6 +77,14 @@ notify_webhook() { fi } +notify_post_restart_script() { + if [ -n "$POST_RESTART_SCRIPT" ] + then + # execute post restart script as background process to prevent healer from blocking + $POST_RESTART_SCRIPT "$@" & + fi +} + # https://towardsdatascience.com/proper-ways-to-pass-environment-variables-in-json-for-curl-post-f797d2698bf3 generate_webhook_payload() { local text="$@" @@ -146,6 +154,7 @@ then else notify_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Successfully restarted the container!" & fi + notify_post_restart_script "$CONTAINER_NAME" "$CONTAINER_SHORT_ID" "$CONTAINER_STATE" "$TIMEOUT" & fi done sleep "$AUTOHEAL_INTERVAL" &