From 5b5d43e913f4be64ab3826429268fc4cb7fffda5 Mon Sep 17 00:00:00 2001 From: Dominik Deutsch Date: Fri, 11 Jun 2021 14:11:06 +0200 Subject: [PATCH] Added NOTIFY_ONLY option to disable automated container restarts --- README.md | 1 + docker-entrypoint | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f9bcdcb..048ba84 100644 --- a/README.md +++ b/README.md @@ -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) +NOTIFY_ONLY="" # set this to true if you want to disable the automated restart ``` ### Optional Container Labels diff --git a/docker-entrypoint b/docker-entrypoint index eb482bf..dcf8267 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:-""} +NOTIFY_ONLY=${NOTIFY_ONLY:-""} # only use unix domain socket if no TCP endpoint is defined case "${DOCKER_SOCK}" in @@ -111,13 +112,19 @@ if [ "$1" = "autoheal" ] && [ -e "$DOCKER_SOCK" ];then then echo "$DATE Container $CONTAINER_NAME (${CONTAINER_SHORT_ID}) found to be restarting - don't restart" else - echo "$DATE Container $CONTAINER_NAME (${CONTAINER_SHORT_ID}) found to be unhealthy - Restarting container now with ${TIMEOUT}s timeout" - if ! restart_container "$CONTAINER_ID" "$TIMEOUT" + + if [ -n "$NOTIFY_ONLY" ] then - echo "$DATE Restarting container $CONTAINER_SHORT_ID failed" >&2 - notify_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Failed to restart the container!" & - else - notify_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Successfully restarted the container!" & + notify_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Automated container restard is disabled by NOTIFY_ONLY env variable." & + else + echo "$DATE Container $CONTAINER_NAME (${CONTAINER_SHORT_ID}) found to be unhealthy - Restarting container now with ${TIMEOUT}s timeout" + if ! restart_container "$CONTAINER_ID" "$TIMEOUT" + then + echo "$DATE Restarting container $CONTAINER_SHORT_ID failed" >&2 + notify_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Failed to restart the container!" & + else + notify_webhook "Container ${CONTAINER_NAME:1} (${CONTAINER_SHORT_ID}) found to be unhealthy. Successfully restarted the container!" & + fi fi fi done