Docker Compose Wait til dependency container is fully up before launching

后端 未结 2 620
南笙
南笙 2021-02-14 09:55

I\'m working with a docker service using docker-compose, and I have a service that depends on anther.

I\'ve used the depends_on key, but the service with t

相关标签:
2条回答
  • 2021-02-14 10:23

    I've often found using a wait-for-it bash script much more effective than the built in health check to docker-compose.

    This runs a TCP health check against a given port and waits until this is complete before starting to run a process.

    Sample code:

    version: "2"
    services:
      web:
        build: .
        ports:
          - "80:8000"
        depends_on:
          - "db"
        command: ["./wait-for-it.sh", "db:5432", "--", "python", "app.py"]
      db:
        image: postgres
    

    Here's some docs:

    • https://docs.docker.com/compose/startup-order/
    • https://github.com/vishnubob/wait-for-it
    0 讨论(0)
  • 2021-02-14 10:24

    You are probably looking for docker compose healthcheck

    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
    

    There is a good reference here as well:
    https://github.com/peter-evans/docker-compose-healthcheck

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