Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

osama_abdullah's avatar

Do not start service A unless service B is running docker swarm.

I have a nginx_proxy service that needs both nginx_1 and nginx_2 to be running if one of them is not running then the nginx_proxy will crash as it can resolve the IP of the service(s)

those are the nginx_proxy configurations:

server {

    listen 80;

    server_name www.app1.com;

    location / {
        proxy_pass http://nginx_1;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host:$server_port;
        proxy_http_version 1.1;
    }
}
server {

    listen 80;

    server_name www.app2.com;

    location / {
        proxy_pass http://nginx_2;

        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Host $host:$server_port;
        proxy_http_version 1.1;
    }
}

and this is the stack:

version: '3.9'

services:
  nginx_proxy:
    {...}
  nginx_1:
    {...}
  nginx_2:
    {...}

Since depends_on doesn't work in the swarm mode, and also this solution gives services.nginx.depends_on must be a list error is there any other choices?

0 likes
3 replies
LaryAI's avatar
Level 58

In Docker Swarm, the depends_on option is not supported. Instead, you can use the healthcheck option to ensure that a service is running before starting another service that depends on it.

Here's an example of how you can modify the docker-compose.yml file to use healthcheck:

version: '3.9'

services:
  nginx_proxy:
    {...}
    depends_on:
      - nginx_1
      - nginx_2
    healthcheck:
      test: ["CMD", "curl", "-f", "http://nginx_1", "||", "exit", "1"]
      interval: 30s
      timeout: 10s
      retries: 3
  nginx_1:
    {...}
  nginx_2:
    {...}

In this example, the nginx_proxy service depends on both nginx_1 and nginx_2. The healthcheck option is used to test whether nginx_1 is running before starting nginx_proxy. If the health check fails, the nginx_proxy service will not start.

You can modify the test command to check for the health of nginx_2 as well.

Note that the healthcheck option requires Docker 1.12 or later.

osama_abdullah's avatar
osama_abdullah
OP
Best Answer
Level 14

I wrote a little script that will wait until all dependencies are up and running and then run the nginx_proxy

this is the script

also, add this to your nginx Dockerfile:

COPY --chmod=500 ./conf/wait_for_dependencies /wait_for_dependencies

CMD /wait_for_dependencies /docker-entrypoint.sh nginx -g 'daemon off;'

and pass the dependencies as an environment variable separated by |:

  nginx_proxy:
    {...}
    environment:
      - DEPENDENCIES=nginx_1|nginx_2

Please or to participate in this conversation.