Merge pull request #28 from JohnnyMarnell/stop-timeouts

Add global and per container stop timeouts ; minor doc fixes
This commit is contained in:
will Farrell
2019-02-26 00:17:40 -07:00
committed by GitHub
2 changed files with 17 additions and 7 deletions
+10 -3
View File
@@ -1,7 +1,7 @@
# docker-autoheal
Monitor and restart unhealthy docker containers.
This functionality was propose to be included with the addition of `HEALTHCHECK`, however didn't make the cut.
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.
## Supported tags and Dockerfile links
@@ -30,8 +30,15 @@ Note: You must apply `HEALTHCHECK` to your docker images first. See https://docs
```
AUTOHEAL_CONTAINER_LABEL=autoheal
AUTOHEAL_INTERVAL=5 # check every 5 seconds
AUTOHEAL_START_PERIOD=0 # wait 0 second before first health check
DOCKER_SOCK=/var/run/docker.sock
AUTOHEAL_START_PERIOD=0 # wait 0 seconds before first health check
AUTOHEAL_DEFAULT_STOP_TIMEOUT=10 # Docker waits max 10 seconds (the Docker default) for a container to stop before killing during restarts (container overridable via label, see below)
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
```
### Optional Container Labels
```
autoheal.stop.timeout=20 # Per containers override for stop timeout seconds during restart
```
## Testing
+7 -4
View File
@@ -36,15 +36,18 @@ if [ "$1" = 'autoheal' ] && [ -e ${DOCKER_SOCK} ]; then
sleep ${AUTOHEAL_INTERVAL:=5}
apiUrl="http://localhost/containers/json?filters=\{\"health\":\[\"unhealthy\"\]${labelFilter}\}"
docker_curl "$apiUrl" | jq -r 'foreach .[] as $CONTAINER([];[]; $CONTAINER | .Id, .Names[0])' | \
while read -r CONTAINER_ID && read -r CONTAINER_NAME; do
stopTimeout=".Labels[\"autoheal.stop.timeout\"] // ${AUTOHEAL_DEFAULT_STOP_TIMEOUT:-10}"
docker_curl "$apiUrl" | \
jq -r "foreach .[] as \$CONTAINER([];[]; \$CONTAINER | .Id, .Names[0], $stopTimeout )" | \
while read -r CONTAINER_ID && read -r CONTAINER_NAME && read -r TIMEOUT; do
CONTAINER_SHORT_ID=${CONTAINER_ID:0:12}
DATE=$(date +%d-%m-%Y" "%H:%M:%S)
if [ "null" = "$CONTAINER_NAME" ]; then
echo "$DATE Container name of ($CONTAINER_SHORT_ID) is null, which implies container does not exist - don't restart"
else
echo "$DATE Container ${CONTAINER_NAME} ($CONTAINER_SHORT_ID) found to be unhealthy - Restarting container now"
docker_curl -f -XPOST http://localhost/containers/${CONTAINER_ID}/restart || echo "$DATE Restarting container $CONTAINER_SHORT_ID failed"
echo "$DATE Container ${CONTAINER_NAME} ($CONTAINER_SHORT_ID) found to be unhealthy - Restarting container now with ${TIMEOUT}s timeout"
docker_curl -f -XPOST "http://localhost/containers/${CONTAINER_ID}/restart?t=${TIMEOUT}" \
|| echo "$DATE Restarting container $CONTAINER_SHORT_ID failed"
fi
done
done