Docker-compose check if mysql connection is ready

后端 未结 9 2113
一个人的身影
一个人的身影 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 13:57

    If you are using docker-compose v3+, condition as an option of depends_on has been removed.

    The recommended path is to use rather wait-for-it, dockerize, or wait-for. In your docker-compose.yml file, change your command to be:

    command: sh -c 'bin/wait-for db:3306 -- bundle exec rails s'
    

    I personally prefer wait-for since it can run in an Alpine container (sh compatible, no dependance on bash). Drawback is that it depends on netcat, so if you decide to use it, make sure you have netcat installed in the container, or install it in your Dockerfile, for example with:

    RUN apt-get -q update && apt-get -qy install netcat
    

    I also forked the wait-for project so it can check for healthy HTTP status (it uses wget). Then you can do something like that:

    command: sh -c 'bin/wait-for http://api/ping -- jest test'
    

    PS: A PR is also ready to be merged to add that capacity to wait-for project.

提交回复
热议问题