将 depend_on 与堆栈一起使用

Using depend_on with stack

The documentation for depends_on states:

There are several things to be aware of when using depends_on:

  • depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.

  • The depends_on option is ignored when deploying a stack in swarm mode with a version 3 Compose file.

那么,这是否意味着当我使用 swarm 模式和版本 3 组合文件进行部署时,它将忽略 depends_on 下的所有条件?例如:

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

非常正确,swarm 作为编排将忽略 depends_on

但是对于您的情况,depends_on 确实没有必要,因为您已经拥有 wait-for-it.sh,它比 depends_on 具有更好的控制以确保启动顺序。

You can control the order of service startup and shutdown with the depends_on option. Compose always starts and stops containers in dependency order, where dependencies are determined by depends_on, links, volumes_from, and network_mode: "service:...".

However, for startup Compose does not wait until a container is “ready” (whatever that means for your particular application) - only until it’s running. There’s a good reason for this.

There are limitations to this first solution. For example, it doesn’t verify when a specific service is really ready.

Alternatively, write your own wrapper script to perform a more application-specific health check. For example, you might want to wait until Postgres is ready to accept commands

详情请参阅 Control startup and shutdown order in Compose,因此您的解决方案中不需要 depends_on