Checking if the MySQL service of a Docker container is ready
Published on 2020-09-03 • Modified on 2020-09-03
In the previous snippet, we saw how to do it for a Postgres service; now it's for MySQL. The process is similar, but there is a slight difference as there is no equivalent of the pg_isready
command for MySQL. Therefore we have to grep the output of the mysqladmin ping
command to check if the service is "alive". Change the sb-db
parameter for the name of your MySQL docker container. Check out my full Makefile to see how I am calling this script in the run
target.
#!/bin/sh
# https://stackoverflow.com/q/42567475/633864
seconds=1
until docker container exec -it strangebuzz-db-1 mysqladmin -h 127.0.0.1 ping -P 3306 -proot | grep "mysqld is alive" ; do
>&2 echo "MySQL is unavailable - waiting for it... 😴 ($seconds)"
sleep 1
seconds=$(expr $seconds + 1)
done
sleep 1
More on Stackoverflow Read the doc Random snippet