-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_docker_container.sh
54 lines (37 loc) · 1.24 KB
/
check_docker_container.sh
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
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
# CHECK IF ALL ARGUMENTS HAVE BEEN PASSED
if [ -z $1 ]; then
echo "You need to pass three arguments."
echo "frst argument == container name"
exit 1
fi
container_name=$1
# HEALTHCHECK FUNCTION
check_container_health() {
# Get the health status of the container
health_status=$(docker inspect --format='{{.State.Health.Status}}' "$1")
# Check the health status and return 0 if healthy, 1 if unhealthy
if [[ "$health_status" = "healthy" ]]; then
return 0
elif [[ "$health_status" = "starting" ]]; then
echo "Container in starting state"
sleep 15
check_container_health $1
else
return 1
fi
}
# Debugging output
#echo "Checking container: $container_name"
state_restarting=$(docker inspect --format="{{.State.Restarting}}" "$container_name")
state_running=$(docker inspect --format="{{.State.Running}}" "$container_name")
# Debugging output
#echo "Restarting: $state_restarting"
#echo "Running: $state_running"
if [ "$state_restarting" = "false" ] && [ "$state_running" = "true" ]; then
#echo "Container is in the expected state. Exiting with status 0."
exit 0
else
#echo "Container is not in the expected state. Exiting with status 1."
exit 1
fi