diff --git a/check_docker_container.sh b/check_docker_container.sh index 4153cf2..50a2ed1 100644 --- a/check_docker_container.sh +++ b/check_docker_container.sh @@ -1,7 +1,39 @@ #!/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=$(sudo docker inspect --format='{{.State.Health.Status}}' "$1") + + # Check the health status and return 0 if healthy, 1 if unhealthy + if [[ "$health_status" = "healthy" > /dev/null 2>&1 ]]; then + return 0 + + elif [[ "$health_status" = "starting" > /dev/null 2>&1 ]]; then + + echo "Container in starting state" + sleep 15 + + check_container_health $1 + + else + return 1 + fi +} + # Debugging output echo "Checking container: $container_name" @@ -12,7 +44,7 @@ state_running=$(sudo docker inspect --format="{{.State.Running}}" "$container_na echo "Restarting: $state_restarting" echo "Running: $state_running" -if [ "$state_restarting" = "false" ] && [ "$state_running" = "true" ]; then +if [ "$state_restarting" = "false" ] && [ "$state_running" = "true" > /dev/null 2>&1 ]; then echo "Container is in the expected state. Exiting with status 0." exit 0 else