Attendre qu'un conteneur Docker soit prêt grâce à la commande inspect

Publié le 01/11/2021 • Actualisé le 01/11/2021


English language detected! 🇬🇧

  We noticed that your browser is using English. Do you want to read this post in this language?

Read the english version 🇬🇧 Close

Dans ce bout de code, nous voyons comment attendre qu'un conteneur Docker soit prêt grâce à la commande inspect. C'est un script que l'on peut appeler juste après un docker-composer up pour ne pas avoir à lancer docker-compose ps manuellement. Le conteneur qu'on teste ici est le principal et doit avoir un test de santé valide (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. 😃"

 Plus sur Stackoverflow   Lire la doc  Snippet aléatoire

  Travaillez avec moi !