Docker-compose check if mysql connection is ready

后端 未结 9 2152
一个人的身影
一个人的身影 2020-11-27 13:40

I am trying to make sure that my app container does not run migrations / start until the db container is started and READY TO accept connections.

So I decided to use

9条回答
  •  没有蜡笔的小新
    2020-11-27 14:00

    Hi for a simple healthcheck using docker-compose v2.1, I used:

    /usr/bin/mysql --user=root --password=rootpasswd --execute \"SHOW DATABASES;\"
    

    Basically it runs a simple mysql command SHOW DATABASES; using as an example the user root with the password rootpasswd in the database.

    If the command succeed the db is up and ready so the healthcheck path. You can use interval so it tests at interval.

    Removing the other field for visibility, here is what it would look like in your docker-compose.yaml.

    version: '2.1'
    
      services:
        db:
          ... # Other db configuration (image, port, volumes, ...)
          healthcheck:
            test: "/usr/bin/mysql --user=root --password=rootpasswd --execute \"SHOW DATABASES;\""
            interval: 2s
            timeout: 20s
            retries: 10
    
         app:
           ... # Other app configuration
           depends_on:
             db:
             condition: service_healthy
    

提交回复
热议问题