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

后端 未结 14 994
猫巷女王i
猫巷女王i 2020-11-30 22:33

I am deploying a few different docker containers, mysql being the first one. I want to run scripts as soon as database is up and proceed to building other containers. The sc

14条回答
  •  失恋的感觉
    2020-11-30 23:07

    I had the same issue when my Django container tried to connect the mysql container just after it started. I solved it using the vishnubob's wait-for.it.sh script. Its a shell script which waits for an IP and a host to be ready before continuing. Here is the example I use for my applicaction.

    ./wait-for-it.sh \
        -h $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $MYSQL_CONTAINER_NAME) \
        -p 3306 \
        -t 90
    

    In that script I'm asking to the mysql container to wait maximum 90 seconds (it will run normally when ready) in the port 3306 (default mysql port) and the host asigned by docker for my MYSQL_CONTAINER_NAME. The script have more variables but for mw worked with these three.

提交回复
热议问题