skip to Main Content

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?

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. I did some research and test then I realized something.

    According to this link:

    The docker stack deploy command supports any Compose file of version "3.0" or above

    And according to this link:

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

    So you don’t have any other choices at this time.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search