How do I know when my docker mysql container is up and mysql is ready for taking queries? How do I know when my docker mysql container is up and mysql is ready for taking queries? docker docker

How do I know when my docker mysql container is up and mysql is ready for taking queries?


You can install mysql-client package and use mysqladmin to ping target server. Useful when working with multiple docker container. Combine with sleep and create a simple wait-loop:

while ! mysqladmin ping -h"$DB_HOST" --silent; do    sleep 1done


This little bash loop waits for mysql to be open, shouldn't require any extra packages to be installed:

until nc -z -v -w30 $CFG_MYSQL_HOST 3306do  echo "Waiting for database connection..."  # wait for 5 seconds before check again  sleep 5done


This was more or less mentioned in comments to other answers, but I think it deserves its own entry.

First of all you can run your container in the following manner:

docker run --name mysql --health-cmd='mysqladmin ping --silent' -d mysql

There is also an equivalent in the Dockerfile.

With that command your docker ps and docker inspect will show you health status of your container. For mysql in particular this method has the advantage of mysqladmin being available inside the container, so you do not need to install it on the docker host.

Then you can simply loop in a bash script to wait on the status to become healthy. The following bash script is created by Dennis.

function getContainerHealth {  docker inspect --format "{{.State.Health.Status}}" $1}function waitContainer {  while STATUS=$(getContainerHealth $1); [ $STATUS != "healthy" ]; do     if [ $STATUS == "unhealthy" ]; then      echo "Failed!"      exit -1    fi    printf .    lf=$'\n'    sleep 1  done  printf "$lf"}

Now you can do this in your script:

waitContainer mysql

and your script will wait until the container is up and running. The script will exit if the container becomes unhealthy, which is possible, if for example docker host is out of memory, so that the mysql cannot allocate enough of it for itself.