Waiting for a Docker container to be ready thanks to the inspect command
Published on 2021-11-01 • Modified on 2021-11-01
This snippet shows how to wait for a Docker container to be ready, thanks to the inspect command. This is a script you can call just after a docker-composer up
, so you don't have to run docker-compose ps
to check the status of all your containers. The container you test here is your main one and must have a valid health check.
#!/bin/sh
main_container=strangebuzz-php-1
seconds=1
localhost=https://localhost
until docker inspect --format "{{json .State.Health.Status }}" $main_container | grep '"healthy"' > /dev/null 2>&1 ; do
>&2 echo "Docker is not ready - wait before opening $localhost š“ ($seconds)"
sleep 1
seconds=$(expr $seconds + 1)
done
echo "\nā
Done! You can open $localhost now. š"
More on Stackoverflow Read the doc Random snippet