Traefik with two sites, the second one up does not work half the time

Hi !

I'm behaving very strangely with Traefik... I feel like he's getting mixed up. I have a Traefik which allows me to manage my two websites locally, to launch them in parallel. Here are my compositions:

On Project/infrastructure/docker-compose.yml

version: '3'

services:
  reverse-proxy:
    image: traefik:v3.0
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entryPoints.web.address=:80"
      - "--log.level=DEBUG"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=traefik"
      - "traefik.http.routers.traefik-secure.entrypoints=web"
      - "traefik.http.routers.traefik-secure.rule=Host(`monitoring.localhost`)"
      - "traefik.http.routers.traefik-secure.service=api@internal"
    networks:
      - traefik

networks:
  traefik:
    external: true

And my two sites look like this:

Project/site1/docker-compose.yml - COMPOSE_PROJECT_NAME=site1

Project/site2/docker-compose.yml - COMPOSE_PROJECT_NAME=site2

version: '3'

services:
  php:
    container_name: "${COMPOSE_PROJECT_NAME}-php"
    build:
      context: ./config/php/${PHP_VERSION}-fpm
      args:
        COMPOSER_VERSION: ${COMPOSER_VERSION}
        ENABLE_XDEBUG: ${ENABLE_XDEBUG}
        ENABLE_WKHTMLTOPDF: ${ENABLE_WKHTMLTOPDF}
        ENABLE_BLACKFIRE: ${ENABLE_BLACKFIRE}
    volumes:
      - ${APP_FOLDER}:/var/www
    networks:
      - traefik
  nginx:
    container_name: "${COMPOSE_PROJECT_NAME}-nginx"
    build:
      context: ./config/nginx
    volumes:
      - ${APP_FOLDER}:/var/www
      - ./config/nginx/nginx-${PROJECT_TYPE}.conf:/etc/templates/nginx.conf
      - ./logs:/var/log/nginx
      - ./nginx/certs:/etc/nginx/certs
    environment:
      ADMIN_FOLDER_PRESTASHOP: ${ADMIN_FOLDER_PRESTASHOP}
    depends_on:
      - php
    labels:
      # Traefik 2.0
      - "traefik.enable=true"
      - "traefik.http.routers.${COMPOSE_PROJECT_NAME}.entrypoints=web"
      - "traefik.http.routers.${COMPOSE_PROJECT_NAME}.rule=Host(`${COMPOSE_PROJECT_NAME}.localhost`)"
      - "traefik.docker.network=traefik"
    networks:
      - traefik
  nodejs:
    container_name: "${COMPOSE_PROJECT_NAME}-nodejs"
    image: "nikolaik/python-nodejs:latest"
    working_dir: /var/www/
    environment:
      - NODE_ENV=development
    volumes:
      - ${APP_FOLDER}:/var/www
    expose:
      - "8081"
    #command: bash -c "yarn install && yarn encore dev --watch"
    networks:
      - traefik

networks:
  traefik:
    external: true

When I up the first one, everything is ok, I can access it via site1.localhost for example When I up the second one, and I access site2.localhost, one time out of two I am on the right site, the other time I am on the "site1" container...

If I reverse the order of launching the "up", it is always my second site which alternates...

Do you have an idea ? I don't understand...

Use docker inspect and docker service inspect to check if the templating was done correctly.

Note that some browsers will resolve any *.localhost to 127.0.0.1, even if it is not or differently set in hosts file.

1 Like

Hi Bluepuma77,

Thanks for reply.

Docker inspect (site1-nginx site1-php site2-nginx site2-php seems good)... All directory are good...
I change to *.local just for testing, it's similar... I don't understand...

Hello
I have the exact same problem. Have you found a solution ?

Hello,

I need to create a network for each projet :

services:
  php:
    build:
      context: ...
    networks:
      - project_network
      - databases
      - mailhog
  nginx:
    build:
      context: ...
    depends_on:
      - php
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.${COMPOSE_PROJECT_NAME}.rule=Host(`fr.${COMPOSE_PROJECT_NAME}.docker.localhost`) || Host(`en.${COMPOSE_PROJECT_NAME}.docker.localhost`)"
      - "traefik.http.routers.${COMPOSE_PROJECT_NAME}.tls=true"
      - "traefik.docker.network=traefik"
    networks:
      - project_network
      - traefik

networks:
  project_network:
  databases:
    external: true
  mailhog:
    external: true
  traefik:
    external: true

project_network (same for all subsite) help me to separate each project :slight_smile:

I see. My problem here is when i have differents stack of the same docker-compose.yaml file.
Here is my file:

networks:
  gateway:
    external: true  
  application:
    
  nginx:
    image: nginx:latest
    container_name: ${COMPOSE_PROJECT_NAME}_nginx
    networks:
      - application # <== This is the internal network
      - gateway # <== This is the traefik external network
    expose:
      - 80
    labels:
      - traefik.enable=true
      - traefik.http.routers.${COMPOSE_PROJECT_NAME}_home.rule=Host(`${COMPOSE_PROJECT_NAME}.${DOMAIN}`)
  
  service:
    #...
    # here is my php application

When I do:

docker compose -p project_1 up -d 
docker compose -p project_2 up -d 

The first stack works perfectly well. But the second fails.
Every two request, it reaches the wrong nginx...

Add network into nginx and your php service.

network are prefixed by name of folder

In my case :slight_smile:
~/Project/infrastructure/docker_compose.yml <= traefik, mysql, mailhog, phpmyadmin services
~/Project/project_1/docker_compose.yml <= nginx and php services (example above)
~/Project/project_2/docker_compose.yml <= nginx and php services

I have a lot of network :
databases
mailhog
traefik
project_1_project_network <= this is important
project_2_project_network <= this is important