From ab84fc5c35c491baf45c1e068235b03da4bb3cfc Mon Sep 17 00:00:00 2001 From: nukedupe Date: Fri, 16 Apr 2021 01:26:02 +0200 Subject: [PATCH 01/20] fix issue #48 --- docker-entrypoint | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docker-entrypoint b/docker-entrypoint index 96b494f..280bbbd 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -64,7 +64,13 @@ term_handler() { # shellcheck disable=2039 trap 'kill $$; term_handler' SIGTERM -if [ "$1" = "autoheal" ] && [ -e "$DOCKER_SOCK" ];then +if [ "$1" = "autoheal" ] ++then + if [ -n "$UNIX_SOCK" ] && ! [ -S "$DOCKER_SOCK" ] + then + echo "Connection problem" + exit + fi # Delayed startup if [ "$AUTOHEAL_START_PERIOD" -gt 0 ] then From 35e7733b0cb6a69394441a1eb1a21d3e82b43a7b Mon Sep 17 00:00:00 2001 From: nukedupe Date: Fri, 16 Apr 2021 01:27:23 +0200 Subject: [PATCH 02/20] add tcp support --- docker-entrypoint | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docker-entrypoint b/docker-entrypoint index 280bbbd..6fbcee8 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -10,7 +10,9 @@ CURL_TIMEOUT=${CURL_TIMEOUT:-30} # only use unix domain socket if no TCP endpoint is defined case "${DOCKER_SOCK}" in - "tcp://"*) HTTP_ENDPOINT="$(echo ${DOCKER_SOCK} | sed 's#tcp://#https://#')" + "tcp://"*) HTTP_ENDPOINT="$(echo ${DOCKER_SOCK} | sed 's#tcp://#http://#')" + ;; + "tcps://"*) HTTP_ENDPOINT="$(echo ${DOCKER_SOCK} | sed 's#tcps://#https://#')" CA="--cacert /certs/ca.pem" CLIENT_KEY="--key /certs/client-key.pem" CLIENT_CERT="--cert /certs/client-cert.pem" From b9c3b3555077516375e4f2459285d4c05ce9545f Mon Sep 17 00:00:00 2001 From: nukedupe Date: Fri, 16 Apr 2021 01:27:53 +0200 Subject: [PATCH 03/20] update alpine to 3.13 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 18d1230..42d1e6f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine:3.11 +FROM alpine:3.13 RUN apk add --no-cache curl jq From 58712cde2610eaceb53e0d944ba55269ba8df21c Mon Sep 17 00:00:00 2001 From: nukedupe <82565629+nukedupe@users.noreply.github.com> Date: Fri, 16 Apr 2021 12:51:21 +0200 Subject: [PATCH 04/20] Update docker-entrypoint Don't exit 0 if there's an error. Co-authored-by: Philipp Schmitt --- docker-entrypoint | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-entrypoint b/docker-entrypoint index 6fbcee8..dec6a45 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -71,7 +71,7 @@ if [ "$1" = "autoheal" ] if [ -n "$UNIX_SOCK" ] && ! [ -S "$DOCKER_SOCK" ] then echo "Connection problem" - exit + exit 1 fi # Delayed startup if [ "$AUTOHEAL_START_PERIOD" -gt 0 ] From 827924d0b7c948a1e893874badf045c7e088950b Mon Sep 17 00:00:00 2001 From: nukedupe <82565629+nukedupe@users.noreply.github.com> Date: Fri, 16 Apr 2021 12:52:00 +0200 Subject: [PATCH 05/20] Update docker-entrypoint redirect output to stderr Co-authored-by: Philipp Schmitt --- docker-entrypoint | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-entrypoint b/docker-entrypoint index dec6a45..aa3ba4d 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -70,7 +70,7 @@ if [ "$1" = "autoheal" ] +then if [ -n "$UNIX_SOCK" ] && ! [ -S "$DOCKER_SOCK" ] then - echo "Connection problem" + echo "Connection problem" >&2 exit 1 fi # Delayed startup From 724611862f42ef56679e96b189ca4a3b3a65be3e Mon Sep 17 00:00:00 2001 From: nukedupe Date: Fri, 16 Apr 2021 13:12:51 +0200 Subject: [PATCH 06/20] fix issue #48 --- docker-entrypoint | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-entrypoint b/docker-entrypoint index aa3ba4d..54e4d05 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -70,7 +70,7 @@ if [ "$1" = "autoheal" ] +then if [ -n "$UNIX_SOCK" ] && ! [ -S "$DOCKER_SOCK" ] then - echo "Connection problem" >&2 + echo "unix socket is currently not available" >&2 exit 1 fi # Delayed startup From 2ae63255b4ccf6b9dc7b5b8ac799e26f19eef5a9 Mon Sep 17 00:00:00 2001 From: nukedupe Date: Fri, 16 Apr 2021 16:23:39 +0200 Subject: [PATCH 07/20] fix documentation --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3a749db..e53a4da 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ docker run -d \ --name autoheal \ --restart=always \ -e AUTOHEAL_CONTAINER_LABEL=all \ - -e DOCKER_SOCK=tcp://HOST:PORT \ + -e DOCKER_SOCK=tcps://HOST:PORT \ -v /path/to/certs/:/certs/:ro \ willfarrell/autoheal ``` @@ -38,7 +38,8 @@ 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`. Note: You must apply `HEALTHCHECK` to your docker images first. See https://docs.docker.com/engine/reference/builder/#healthcheck for details. -See https://docs.docker.com/engine/security/https/ for how to configure TCP with mTLS +Use `tcp://` for unencrypted tcp and `tcps://` for TLS enabled connection. +See https://docs.docker.com/engine/security/https/ for how to configure TCP with mTLS. The certificates, and keys need these names: * ca.pem From 9e8c865c7451ab2c4b88726f4df705053a623bb6 Mon Sep 17 00:00:00 2001 From: nukedupe Date: Fri, 16 Apr 2021 20:27:25 +0200 Subject: [PATCH 08/20] fix typo --- docker-entrypoint | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-entrypoint b/docker-entrypoint index 54e4d05..7ef114f 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -67,7 +67,7 @@ term_handler() { trap 'kill $$; term_handler' SIGTERM if [ "$1" = "autoheal" ] -+then +then if [ -n "$UNIX_SOCK" ] && ! [ -S "$DOCKER_SOCK" ] then echo "unix socket is currently not available" >&2 From b8c89a85b088be87a9329a5e530f7c66721135ea Mon Sep 17 00:00:00 2001 From: Arno Hautala Date: Tue, 5 Oct 2021 19:57:40 -0400 Subject: [PATCH 09/20] added support for Apprise notifications --- Dockerfile | 3 ++- docker-entrypoint | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 1485535..752335b 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="" \ + APPRISE_URL="" HEALTHCHECK --interval=5s CMD pgrep -f autoheal || exit 1 diff --git a/docker-entrypoint b/docker-entrypoint index eb482bf..02b0e20 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -8,6 +8,7 @@ DOCKER_SOCK=${DOCKER_SOCK:-/var/run/docker.sock} UNIX_SOCK="" CURL_TIMEOUT=${CURL_TIMEOUT:-30} WEBHOOK_URL=${WEBHOOK_URL:-""} +APPRISE_URL=${APPRISE_URL:-""} # only use unix domain socket if no TCP endpoint is defined case "${DOCKER_SOCK}" in @@ -65,6 +66,12 @@ notify_webhook() { # execute webhook requests as background process to prevent healer from blocking curl -X POST -H "Content-type: application/json" -d "$(generate_webhook_payload $text)" $WEBHOOK_URL fi + + if [ -n "$APPRISE_URL" ] + then + # execute webhook requests as background process to prevent healer from blocking + curl -X POST -H "Content-type: application/json" -d "$(generate_apprise_payload $text)" $APPRISE_URL + fi } # https://towardsdatascience.com/proper-ways-to-pass-environment-variables-in-json-for-curl-post-f797d2698bf3 @@ -77,6 +84,16 @@ generate_webhook_payload() { EOF } +generate_apprise_payload() { + local text="$@" + cat < Date: Tue, 5 Oct 2021 22:49:55 -0400 Subject: [PATCH 10/20] documentation --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index f9bcdcb..d1074b4 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,16 @@ If you need the timezone to match the local machine, you can map the `/etc/local docker run ... -v /etc/localtime:/etc/localtime:ro ``` +### Apprise configuration +a) Start the Apprise docker container. See: https://hub.docker.com/r/caronc/apprise + +b) Create an Apprise configuration for Autoheal to use: +http://localhost:8000/cfg/autoheal + +See the Apprise wiki for configuration details: https://github.com/caronc/apprise/wiki + +c) Set the `APPRISE_URL` environment variable to use the Apprise configuration: +`APPRISE_URL="http://localhost:8000/notify/autoheal"` ## ENV Defaults ``` @@ -61,6 +71,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) +APPRISE_URL="" # post message to Apprise if a container was restarted (or restart failed) ``` ### Optional Container Labels From 44aaecc3101f4aaea58dc38eeaf08d02f7bd5f99 Mon Sep 17 00:00:00 2001 From: hthighwaymonk <49250826+hthighwaymonk@users.noreply.github.com> Date: Thu, 7 Oct 2021 15:46:58 -0700 Subject: [PATCH 11/20] Update docker-entrypoint line 75 using `"text":"$text" fails, but if you change it to `"content":"$text"` it works --- docker-entrypoint | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-entrypoint b/docker-entrypoint index eb482bf..7d7208e 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -72,7 +72,7 @@ generate_webhook_payload() { local text="$@" cat < Date: Fri, 15 Oct 2021 08:40:53 -0400 Subject: [PATCH 12/20] silently curl in notifications --- docker-entrypoint | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-entrypoint b/docker-entrypoint index 02b0e20..b457135 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -64,13 +64,13 @@ notify_webhook() { if [ -n "$WEBHOOK_URL" ] then # execute webhook requests as background process to prevent healer from blocking - curl -X POST -H "Content-type: application/json" -d "$(generate_webhook_payload $text)" $WEBHOOK_URL + curl -s -X POST -H "Content-type: application/json" -d "$(generate_webhook_payload $text)" $WEBHOOK_URL fi if [ -n "$APPRISE_URL" ] then # execute webhook requests as background process to prevent healer from blocking - curl -X POST -H "Content-type: application/json" -d "$(generate_apprise_payload $text)" $APPRISE_URL + curl -s -X POST -H "Content-type: application/json" -d "$(generate_apprise_payload $text)" $APPRISE_URL fi } From 0cbd4e25370dce9dd0358b7d75082725558760a5 Mon Sep 17 00:00:00 2001 From: Arno Hautala Date: Fri, 1 Apr 2022 16:42:57 -0400 Subject: [PATCH 13/20] blacklist support --- docker-entrypoint | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-entrypoint b/docker-entrypoint index eb482bf..fd376ce 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -97,7 +97,7 @@ if [ "$1" = "autoheal" ] && [ -e "$DOCKER_SOCK" ];then do STOP_TIMEOUT=".Labels[\"autoheal.stop.timeout\"] // $AUTOHEAL_DEFAULT_STOP_TIMEOUT" get_container_info | \ - jq -r "foreach .[] as \$CONTAINER([];[]; \$CONTAINER | .Id, .Names[0], .State, ${STOP_TIMEOUT})" | \ + jq -r ".[] | select(.Labels[\"autoheal\"] != \"False\") | foreach . as \$CONTAINER([];[]; \$CONTAINER | .Id, .Names[0], .State, ${STOP_TIMEOUT})" | \ while read -r CONTAINER_ID && read -r CONTAINER_NAME && read -r CONTAINER_STATE && read -r TIMEOUT do # shellcheck disable=2039 From a47a09ac29c218276d4448a9bed56ab34dc0a200 Mon Sep 17 00:00:00 2001 From: Arno Hautala Date: Fri, 1 Apr 2022 16:48:55 -0400 Subject: [PATCH 14/20] readme note --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index f9bcdcb..e71e3b9 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ 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`. +Note: You can also exclude a container (eg. if using `AUTOHEAL_CONTAINER_LABEL=all`) by setting the label `autoheal=false`. + Note: You must apply `HEALTHCHECK` to your docker images first. See https://docs.docker.com/engine/reference/builder/#healthcheck for details. See https://docs.docker.com/engine/security/https/ for how to configure TCP with mTLS From 44da6aa643ea14f85327433f2fb56fac855bfa2e Mon Sep 17 00:00:00 2001 From: 70m6 <105207964+70m6@users.noreply.github.com> Date: Wed, 15 Jun 2022 17:04:00 +0200 Subject: [PATCH 15/20] properly handle signals while sleeping https://i.imgur.com/OpOvSut.png --- docker-entrypoint | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docker-entrypoint b/docker-entrypoint index eb482bf..73b065f 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -90,7 +90,8 @@ if [ "$1" = "autoheal" ] && [ -e "$DOCKER_SOCK" ];then if [ "$AUTOHEAL_START_PERIOD" -gt 0 ] then echo "Monitoring containers for unhealthy status in $AUTOHEAL_START_PERIOD second(s)" - sleep "$AUTOHEAL_START_PERIOD" + sleep "$AUTOHEAL_START_PERIOD" & + wait $! fi while true @@ -121,7 +122,8 @@ if [ "$1" = "autoheal" ] && [ -e "$DOCKER_SOCK" ];then fi fi done - sleep "$AUTOHEAL_INTERVAL" + sleep "$AUTOHEAL_INTERVAL" & + wait $! done else From e53897503f4cb880b2191447e6c777d05a23377a Mon Sep 17 00:00:00 2001 From: xythian Date: Fri, 14 Apr 2023 09:38:19 -0700 Subject: [PATCH 16/20] Update base image to alpine 3.17.3. --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 42d1e6f..9911470 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine:3.13 +FROM alpine:3.17.3 RUN apk add --no-cache curl jq From 317fc0ce94e5326f68e276227712f4c72e337882 Mon Sep 17 00:00:00 2001 From: Carl Mercier Date: Tue, 18 Apr 2023 19:27:33 -0400 Subject: [PATCH 17/20] 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 18/20] 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" From 3ef82dc99297533341bb6831bdb47de83c5b282c Mon Sep 17 00:00:00 2001 From: Tyler Pace Date: Tue, 18 Apr 2023 18:21:18 -0700 Subject: [PATCH 19/20] Manually merge PR#79. --- Dockerfile | 1 + README.md | 9 +++++---- docker-entrypoint | 9 +++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index 6202e9d..9ded7f4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,7 @@ ENV AUTOHEAL_CONTAINER_LABEL=autoheal \ DOCKER_SOCK=/var/run/docker.sock \ CURL_TIMEOUT=30 \ WEBHOOK_URL="" \ + WEBHOOK_JSON_KEY="content" \ APPRISE_URL="" HEALTHCHECK --interval=5s CMD pgrep -f autoheal || exit 1 diff --git a/README.md b/README.md index 71a18b1..9d40d7d 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`. @@ -60,7 +60,7 @@ http://localhost:8000/cfg/autoheal See the Apprise wiki for configuration details: https://github.com/caronc/apprise/wiki -c) Set the `APPRISE_URL` environment variable to use the Apprise configuration: +c) Set the `APPRISE_URL` environment variable to use the Apprise configuration: `APPRISE_URL="http://localhost:8000/notify/autoheal"` ## ENV Defaults @@ -72,6 +72,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) +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) ``` @@ -87,5 +88,5 @@ docker build -t autoheal . docker run -d \ -e AUTOHEAL_CONTAINER_LABEL=all \ -v /var/run/docker.sock:/var/run/docker.sock \ - autoheal + autoheal ``` diff --git a/docker-entrypoint b/docker-entrypoint index e0fa433..88c125d 100755 --- a/docker-entrypoint +++ b/docker-entrypoint @@ -8,6 +8,7 @@ DOCKER_SOCK=${DOCKER_SOCK:-/var/run/docker.sock} UNIX_SOCK="" CURL_TIMEOUT=${CURL_TIMEOUT:-30} WEBHOOK_URL=${WEBHOOK_URL:-""} +WEBHOOK_JSON_KEY=${WEBHOOK_JSON_KEY:-"text"} APPRISE_URL=${APPRISE_URL:-""} # only use unix domain socket if no TCP endpoint is defined @@ -81,8 +82,8 @@ 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 fi From 5b99d0280f3a0e62e08c2ef23a308a84379a7b78 Mon Sep 17 00:00:00 2001 From: modem7 Date: Wed, 31 May 2023 12:04:57 +0100 Subject: [PATCH 20/20] Updating image Updated Dockerfile and scripts with some of the latest PR's, and work from tylerpace. Adding a few files back from original repo such as tests and github workflow. Co-Authored-By: Tyler Pace <4195596+tylerpace@users.noreply.github.com> --- .github/workflows/github-build.yml | 0 Dockerfile | 11 +++++--- README.md | 45 ++++-------------------------- tests/.env | 0 tests/README.md | 0 tests/docker-compose.autoheal.yml | 0 tests/docker-compose.yml | 0 tests/watch-autoheal/Dockerfile | 0 8 files changed, 12 insertions(+), 44 deletions(-) mode change 100644 => 100755 .github/workflows/github-build.yml mode change 100644 => 100755 tests/.env mode change 100644 => 100755 tests/README.md mode change 100644 => 100755 tests/docker-compose.autoheal.yml mode change 100644 => 100755 tests/docker-compose.yml mode change 100644 => 100755 tests/watch-autoheal/Dockerfile diff --git a/.github/workflows/github-build.yml b/.github/workflows/github-build.yml old mode 100644 new mode 100755 diff --git a/Dockerfile b/Dockerfile index 1d73a7a..ad668f5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,9 @@ -FROM alpine:3.17.3 +# syntax = docker/dockerfile:latest + +FROM alpine:3.18.3 RUN apk add --no-cache curl jq -COPY docker-entrypoint / -ENTRYPOINT ["/docker-entrypoint"] - ENV AUTOHEAL_CONTAINER_LABEL=autoheal \ AUTOHEAL_START_PERIOD=0 \ AUTOHEAL_INTERVAL=5 \ @@ -16,6 +15,10 @@ ENV AUTOHEAL_CONTAINER_LABEL=autoheal \ APPRISE_URL="" \ POST_RESTART_SCRIPT="" +COPY docker-entrypoint / + HEALTHCHECK --interval=5s CMD pgrep -f autoheal || exit 1 +ENTRYPOINT ["/docker-entrypoint"] + CMD ["autoheal"] diff --git a/README.md b/README.md index 956122e..f9bcdcb 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,6 @@ # Docker Autoheal -Monitor and restart unhealthy docker containers. -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. @@ -28,21 +27,18 @@ docker run -d \ --name autoheal \ --restart=always \ -e AUTOHEAL_CONTAINER_LABEL=all \ - -e DOCKER_SOCK=tcps://HOST:PORT \ + -e DOCKER_SOCK=tcp://HOST:PORT \ -v /path/to/certs/:/certs/:ro \ willfarrell/autoheal ``` 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`. -Note: You can also exclude a container (eg. if using `AUTOHEAL_CONTAINER_LABEL=all`) by setting the label `autoheal=false`. - Note: You must apply `HEALTHCHECK` to your docker images first. See https://docs.docker.com/engine/reference/builder/#healthcheck for details. -Use `tcp://` for unencrypted tcp and `tcps://` for TLS enabled connection. -See https://docs.docker.com/engine/security/https/ for how to configure TCP with mTLS. +See https://docs.docker.com/engine/security/https/ for how to configure TCP with mTLS The certificates, and keys need these names: * ca.pem @@ -55,16 +51,6 @@ If you need the timezone to match the local machine, you can map the `/etc/local docker run ... -v /etc/localtime:/etc/localtime:ro ``` -### Apprise configuration -a) Start the Apprise docker container. See: https://hub.docker.com/r/caronc/apprise - -b) Create an Apprise configuration for Autoheal to use: -http://localhost:8000/cfg/autoheal - -See the Apprise wiki for configuration details: https://github.com/caronc/apprise/wiki - -c) Set the `APPRISE_URL` environment variable to use the Apprise configuration: -`APPRISE_URL="http://localhost:8000/notify/autoheal"` ## ENV Defaults ``` @@ -75,9 +61,6 @@ 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) -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 @@ -85,30 +68,12 @@ POST_RESTART_SCRIPT="" # Run the specified script if a container was restarte 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/tests/.env b/tests/.env old mode 100644 new mode 100755 diff --git a/tests/README.md b/tests/README.md old mode 100644 new mode 100755 diff --git a/tests/docker-compose.autoheal.yml b/tests/docker-compose.autoheal.yml old mode 100644 new mode 100755 diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml old mode 100644 new mode 100755 diff --git a/tests/watch-autoheal/Dockerfile b/tests/watch-autoheal/Dockerfile old mode 100644 new mode 100755