Docker Swarm and Nginx + Varnish

  docker, docker-compose, nginx, swarm, varnish

I have a docker-compose file that contains Nginx, PhpFpm and Varnish.

My Nginx works this way:
User connect to the website => Nginx (443) => Varnish (80) => Nginx (8080) => Phpfpm:9000 or others..

My project work well with "depends_on" config inside docker-compose.
But, with docker swarm, the depends_on is ignored.

There my problems starts..

My Varnish container, needs Nginx to be running, or its crash, due to the hostname defined on top of the configuration file :

# varnish config file
backend default {
  .host = "nginx";
  .port = "8080";
  .connect_timeout = 10s;
  .first_byte_timeout = 10s;
  .between_bytes_timeout = 10s;

And, my nginx, needs varnish to be running or it’s crash too…

# pass to varnish
location / {
    proxy_pass http://varnish;

upstream varnish {
    server varnish:80;

Soooo, varnish crashs because nginx is not up and nginx crashs because varnish is not up.

Is there any solution for this problem ?

Source: Docker Questions