docker stack deploy depends_on

Viewed 4781

Given compose file

version: '3.8'
services:
  whoami1:
    image: containous/whoami
    depends_on:
      - whoami2
  whoami2:
    image: containous/whoami

when deployed to docker swarm docker stack deploy -c docker-compose.yaml test services whoami1 and whoami2 seem to start in random order and ignore depends_on condition.

 docker stack deploy -c docker-compose.yaml test
Creating network test_default
Creating service test_whoami1
Creating service test_whoami2

Does docker swarm support service startup sequencing via dependencies?

1 Answers

No, at least not built in.

Even with depends_on the whoami2 may not yet be ready to interact with whoami1 because it may need time to boot itself:

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.

https://docs.docker.com/compose/startup-order/

They hint at two possibilites to check if whoami2 is ready.

Use a tool such as wait-for-it, dockerize, or sh-compatible wait-for. These are small wrapper scripts which you can include in your application’s image to poll a given host and port until it’s accepting TCP connections.

And depends_on is indeed ignored for docker swarm:

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

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

https://docs.docker.com/compose/compose-file/#depends_on

Related