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.


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)

echo "\nāœ… Done! You can open $localhost now. šŸ˜ƒ"

 More on Stackoverflow   Read the doc  Random snippet

  Work with me!