-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-entrypoint
executable file
·41 lines (33 loc) · 1.33 KB
/
docker-entrypoint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env sh
set -e
DOCKER_SOCK=/tmp/docker.sock
if [ "$1" = 'autoheal' ] && [ -e ${DOCKER_SOCK} ]; then
# https://docs.docker.com/engine/api/v1.25/
# Set container selector
if [ "$AUTOHEAL_CONTAINER_LABEL" == "all" ]; then
selector() {
jq -r .[].Id
}
else
selector() {
jq -r '.[] | select(.Labels["'${AUTOHEAL_CONTAINER_LABEL:=autoheal}'"] == "true") | .Id'
}
fi
echo "Monitoring containers for unhealthy status"
while true; do
sleep ${AUTOHEAL_INTERVAL:=5}
CONTAINERS=$(curl --no-buffer -s -XGET --unix-socket ${DOCKER_SOCK} http://localhost/containers/json | selector)
for CONTAINER in $CONTAINERS; do
HEALTH=$(curl --no-buffer -s -XGET --unix-socket ${DOCKER_SOCK} http://localhost/containers/${CONTAINER}/json | jq -r .State.Health.Status)
if [ "unhealthy" = "$HEALTH" ]; then
DATE=$(date +%d-%m-%Y" "%H:%M:%S)
echo "$DATE Container ${CONTAINER:0:12} found to be unhealthy. Restarting container ..."
# Note: kill and stop didn't honor the restart policy
#curl --no-buffer -s -XPOST --data '{"signal":"SIGKILL"}' --unix-socket ${DOCKER_SOCK} http://localhost/containers/${CONTAINER}/kill
curl --no-buffer -s -XPOST --unix-socket ${DOCKER_SOCK} http://localhost/containers/${CONTAINER}/restart
fi
done
done
else
exec "$@"
fi