Docker-compose depends on not waiting until depended on service isn't fully started

前端 未结 1 811
死守一世寂寞
死守一世寂寞 2021-01-28 17:34
version: \'3\'
services: 
server:
  container_name: hotel-server
  build: 
    dockerfile: Dockerfiles/server/Dockerfile
    context: .
  environment:
    dbhost: db
  l         


        
相关标签:
1条回答
  • 2021-01-28 18:27

    Is there any workaround to that?

    Yes.

    First, realize that depends-on is almost entirely useless. Docker doesn't know anything about your application; it has no way to tell that your database server isn't actually ready to service requests.

    The correct solution is to code your application so that (a) it will retry the initial database connection until it is ready, and (b) it will reconnect to the database if the connection should fail. (a) solves the problem you're asking about, and (b) allows you to restart the database container independent of the application container.

    If you don't control the code in your application container, you can wrap your main command with a shell script that does something like:

    while ! psql -c 'select 1'; do
      sleep 1
    done
    

    (Setting appropriate authentication options or setting up a .pgpass file)

    0 讨论(0)
提交回复
热议问题