admin管理员组

文章数量:1418637

I have a situation with Traefik here.

I wrote a docker-compose.yaml file that has a simple php application and an nginx container. My goal here is to deploy multiple versions of my composition using docker compose project feature. Problem is every two times, the request is forwarded to the wrong nginx.

Here is my compose.yaml 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 external network where traefik is deployed
    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

Problem is when i do:

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

It works well for the first project. But when i try to reach project_2, it reaches the wrong nginx one time over two.

Do you have any idea why ?

My Traefik configuration is here:

global:
  checkNewVersion: false
  sendAnonymousUsage: false

log:
  level: DEBUG

api:
  dashboard: true
  insecure: true

entryPoints:
  web:
    address: ":80"

  websecure:
    address: ":443"
    http:
      tls: {}

providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false
    watch: true
    network: "gateway"

  file:
    filename: "/etc/traefik/tls.yaml"
    watch: true

I have a situation with Traefik here.

I wrote a docker-compose.yaml file that has a simple php application and an nginx container. My goal here is to deploy multiple versions of my composition using docker compose project feature. Problem is every two times, the request is forwarded to the wrong nginx.

Here is my compose.yaml 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 external network where traefik is deployed
    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

Problem is when i do:

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

It works well for the first project. But when i try to reach project_2, it reaches the wrong nginx one time over two.

Do you have any idea why ?

My Traefik configuration is here:

global:
  checkNewVersion: false
  sendAnonymousUsage: false

log:
  level: DEBUG

api:
  dashboard: true
  insecure: true

entryPoints:
  web:
    address: ":80"

  websecure:
    address: ":443"
    http:
      tls: {}

providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false
    watch: true
    network: "gateway"

  file:
    filename: "/etc/traefik/tls.yaml"
    watch: true
Share Improve this question asked Jan 29 at 14:23 Paul BHSPaul BHS 292 bronze badges 1
  • Do both Compose files have a service named nginx? Since they're on the same networks, I wouldn't be surprised if a Docker-internal DNS lookup returned both containers, and connections alternated between them. – David Maze Commented Jan 29 at 15:37
Add a comment  | 

1 Answer 1

Reset to default 0

You don't set a Traefik service name per container, so Traefik needs to create one, and it's probably using the container name. So you have two routers with different names, assigned to the same service name with two target containers.

Probably the simple solution: assign a unique Traefik service name, like in simple Traefik example:

whoami:
  image: traefik/whoami:v1.10
  networks:
    - proxy
  labels:
    - traefik.enable=true
    - traefik.http.routers.mywhoami.rule=Host(`whoami.example`)
    - traefik.http.services.mywhoami.loadbalancer.server.port=80

Or use different names in compose file, not the same "nginx".

本文标签: nginxTraefik bad loadbalancing with docker compose projetcsStack Overflow