Traefik V2 don't resolve host and container ip then return 504 Gateway timeout

Good morning,

Since few weeks i have switched to Treafik from jwilder/nginx-proxy. I have started by creating an independent stack for Traefik that can run in stand alone everywhere on my server. In other part i adpated every web projects's docker-compose.yml with Treafik's labels.

Here is my server structure in its simplified version :

  • /home/me/projects/
    • proxy/
      • log/
      • traefik.toml
      • docker-compose.yml
    • project-alpha/
      • .env
      • docker-compose.yml
      • index.php
      • nginx.conf
    • project-beta/
      • .env
      • docker-compose.yml
      • index.php
      • nginx.conf
    • ...

At first every time, I deployed a new project, I configured it manually and everything worked fine.

Now, to deploy my projects dynamically I made a bash script which copies the structure of a docker web project then adapts the credentials and the names of the services then start docker services with docker-compose command.
The script work correctly but Treafik is this context can't seem to resolve host and container ip, so i get a 504 gateway timeout error.

All my old projects configured manually conitnue to work everytime, the first project that I deploy dynamically works but the following ones come into conflict and start working alternately and randomly.

e.g. out of three dynamically deployed projects, only one works. out of five dynamically deployed projects, two or three are working.

I tried several things including exposing port 80 of each project to see if the nginx and php services responded, yes it's work!

ports:
      - "8181:80"

I also replaced the Traefik proxy with jwilder / nginx-proxy and replaced the labels with the jwilder environment variable, yes it's work!

#replace that
#labels:
    #  traefik.enable: true
    #  traefik.http.routers.webserver_[replace]_route.entrypoints: web
    #  traefik.http.routers.webserver_[replace]_route.rule: Host(`${DOMAIN}`)
    #  traefik.http.routers.webserver_[replace]_route.middlewares: gzip
    #  traefik.http.middlewares.gzip.compress: true
#by that
environment:
      VIRTUAL_HOST: ${DOMAIN}

I'm definitely sure it comes from Traefik, probably in the interpretation of the files created by the bash script.

1/ To reproduce the stack first create a network :

docker network create proxy

2/ Create a folder for the Traefik stack
2.a/ traefik.toml

[api]
    dashboard=true
    debug=true
[log]
    level="INFO"
    filePath = "/var/log/traefik/traefik.log"
[accessLog]
    filePath = "/var/log/traefik/access.log"
[entryPoints]
    [entryPoints.web]
        address=":80"
        [entryPoints.name.transport]
              [entryPoints.name.transport.lifeCycle]
                graceTimeOut = 300
    [entryPoints.webssl]
            address=":443"
[providers.docker]
    endpoint = "unix:///var/run/docker.sock"

2.b/ docker-compose.yml

version: '3.7'

services:
  traefik:
    container_name: traefik
    restart: always
    image: traefik:latest
    volumes:
      - ./traefik.toml:/etc/traefik/traefik.toml
      - /var/run/docker.sock:/var/run/docker.sock
      - ./log/:/var/log/traefik/
    ports:
      - 80:80
      - 443:443
    labels:
      traefik.enable: true
      traefik.http.routers.dashboard.entrypoints: web
      traefik.http.routers.dashboard.rule: Host(`traefik.local`)
      traefik.http.routers.dashboard.service: api@internal
      #traefik.http.routers.dashboard.middlewares: auth
      #traefik.http.middlewares.auth.basicauth.users: ${CREDENTIAL}
    networks:
      - proxy

networks:
  proxy:
    external:
      name: proxy

3/Create a folder for the script bash and dependices
3.a/ deploy.stack.sh

#!/bin/sh
#####################################
### Install website by sh script ####
#####################################

# vars
domain=$1
accountKey=$2
user="me"
userPath="/home/${user}/projects/${accountKey}"

# target folder creation
mkdir -p "${userPath}/www"

# copy docker stack website
cp  -r ./docker-stack-website/* "${userPath}/www"
cp  -r ./docker-stack-website/.env "${userPath}/www"

#replace vars inside .env
perl -p -i -e "s/\[replace\]/${domain}/g" "${userPath}/www/.env"
perl -p -i -e "s/\[account_key\]/${accountKey}/g" "${userPath}/www/.env"

#replace vars inside docker-compose.yml
perl -p -i -e "s/\[replace\]/${accountKey}/g" "${userPath}/www/docker-compose.yml"

#replace vars inside nginx.conf
perl -p -i -e "s/\[replace\]/${accountKey}/g" "${userPath}/www/nginx.conf"
perl -p -i -e "s/\[replace_domain\]/${domain}/g" "${userPath}/www/nginx.conf"

#starting services
cd "${userPath}/www/"
docker-compose up -d

# end
cd -

3.b/ docker-stack-website/.env

DOMAIN=[replace]
COMPOSE_PROJECT_NAME=[account_key]

3.c/ docker-stack-website/docker-compose.yml

version: '3.7'

services:

  app_[replace]:
    image: php:7.4-fpm
    container_name: app_[replace]
    volumes:
      - .:/var/www/html
    networks:
      - [replace]

  webserver_[replace]:
    container_name: webserver_[replace]
    image: nginx:latest
    volumes:
      - .:/usr/share/nginx/html
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - app_[replace]
    restart: always
    labels:
      traefik.enable: true
      traefik.http.routers.webserver_[replace]_route.entrypoints: web
      traefik.http.routers.webserver_[replace]_route.rule: Host(`${DOMAIN}`)
      traefik.http.routers.webserver_[replace]_route.middlewares: gzip
      traefik.http.middlewares.gzip.compress: true
    networks:
      - proxy
      - [replace]

networks:
  sg_proxy:
    external:
      name: proxy
  [replace]:
    name: [replace]
    driver: bridge

3.d/ docker-stack-website/index.php

<?php
echo "Helloooooo php!";

3.e/ docker-stack-website/nginx.conf

server {
    listen       80;
    server_name  [replace_domain];
    index   index.php;
    root    /usr/share/nginx/html;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    error_log  /var/log/nginx/host.error.log debug;

    location / {
        try_files   $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   app_[replace]:9000;
        fastcgi_split_path_info  ^(.+\.php)(/.*)$;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        include        fastcgi_params;
    }
}

4/ Start Traefik with docker-compose command
5/ Run bash script

sh deploy.stack.sh [domain] [accountKey]

To conclude, everything is green in the Traefik dashboard. Everything is green in the traefik.log logs too. The only thing I get is this return in the access.log

172.31.0.1 - - [11/Mar/2021:11:26:31 +0000] "GET / HTTP/1.1" 504 15 "-" "-" 290 "webserver_stack_route@docker" "http://172.25.0.4:80" 30000ms

Thank you in advance for your help