From 317fc0ce94e5326f68e276227712f4c72e337882 Mon Sep 17 00:00:00 2001 From: Carl Mercier Date: Tue, 18 Apr 2023 19:27:33 -0400 Subject: [PATCH 1/2] Upgrade Alpine --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 1485535..38ed707 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine:3.13.5 +FROM alpine:3.17 RUN apk add --no-cache curl jq From 758d785dfffd434aef1519c5735e22c0e579adfb Mon Sep 17 00:00:00 2001 From: Carl Mercier Date: Tue, 18 Apr 2023 20:27:11 -0400 Subject: [PATCH 2/2] Allow specifying a script to be executed after restarting a container --- Dockerfile | 3 ++- README.md | 25 ++++++++++++++++++++++--- docker-entrypoint | 13 +++++++++++-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 38ed707..13c7dec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,7 +11,8 @@ ENV AUTOHEAL_CONTAINER_LABEL=autoheal \ AUTOHEAL_DEFAULT_STOP_TIMEOUT=10 \ DOCKER_SOCK=/var/run/docker.sock \ CURL_TIMEOUT=30 \ - WEBHOOK_URL="" + WEBHOOK_URL="" \ + POST_RESTART_SCRIPT="" HEALTHCHECK --interval=5s CMD pgrep -f autoheal || exit 1 diff --git a/README.md b/README.md index f9bcdcb..46f035e 100644 --- a/README.md +++ b/README.md @@ -1,6 +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. @@ -33,7 +33,7 @@ docker run -d \ ``` a) Apply the label `autoheal=true` to your container to have it watched. -b) Set ENV `AUTOHEAL_CONTAINER_LABEL=all` to watch all running containers. +b) Set ENV `AUTOHEAL_CONTAINER_LABEL=all` to watch all running containers. c) Set ENV `AUTOHEAL_CONTAINER_LABEL` to existing label name that has the value `true`. @@ -61,6 +61,7 @@ AUTOHEAL_DEFAULT_STOP_TIMEOUT=10 # Docker waits max 10 seconds (the Docker def DOCKER_SOCK=/var/run/docker.sock # Unix socket for curl requests to Docker API 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) +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 @@ -68,12 +69,30 @@ WEBHOOK_URL="" # post message to the webhook if a container was restarted (or 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 + autoheal ``` diff --git a/docker-entrypoint b/docker-entrypoint index eb482bf..ea462fe 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -67,13 +67,21 @@ 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="$@" cat <&2 notify_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Failed to restart the container!" & - else + 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"