Erik_DA
Erik_DA

Reputation: 111

Traefik v2 not routing with the path but working with ip

I have 3 VM machines running in a docker swarm, my docker-compose file works perfectly when I build it on one VM machine, but when I deploy the stack and want to work in swarm mode Traefik detects the containers and services but the router does not work with the paths I mentioned inside the docker-compose file. this is my docker-compose file :

    version: "3"

    volumes:
      prometheus_data: {}

    services:
        joomla:
            image: erfanemoon/joomla_img
            hostname: joomla
            ports:
               - 8081:80
               #- 81:81
            deploy:
                mode: replicated
                replicas: 1
                placement:
                   constraints:
                    - node.role == worker
                labels:
                  - "traefik.enable=true"
                  - "traefik.http.routers.joomla.rule=Host(`joomla.localhost`)"
                  - "traefik.http.routers.joomla.priority=49"
                  - "traefik.http.routers.joomla.entrypoints=web"
                  #- "traefik.http.services.joomla.loadbalancer.server.port=80"
            entrypoint: ["wait-for-it.sh", "-t", "120", "db:3306", "--", "docker-php-entrypoint", "-D FOREGROUND"]
            depends_on:
                - db
            volumes:
                - /gfs/joomla/:/var/www/html/
            networks:
                - intnet
                #- extnet  
 pmy:
        image: phpmyadmin/phpmyadmin
        hostname: pmy
        depends_on:
           - db
        ports:
            - 8082:80
        deploy:
            mode: replicated
            replicas: 1
            placement:
               constraints:
                - node.role == worker
            labels:
              - "traefik.enable=true"
              - "traefik.http.routers.pmy.rule=Host(`pmy.localhost`)"
              - "traefik.http.routers.pmy.priority=50"
              - "traefik.http.routers.pmy.entrypoints=web"
              - "traefik.http.services.pmy.loadbalancer.server.port=80"
        environment:
            DB_HOST: db
            MYSQL_ROOT_PASSWORD: qweasd
        volumes:
            - /gfs/phpmyadmin:/config
        networks:
            - intnet
            #- extnet  

 db:
        image: mariadb
        command: --default-authentication-plugin=mysql_native_password
        hostname: db
        ports:
           - 3306:3306
        environment:
            MYSQL_ROOT_PASSWORD: qweasd
        volumes:
            - /gfs/mysql:/var/lib/mysql
        networks:
            - intnet
            #- extnet  


    traefik:
            image: "traefik:v2.0.0-rc3"
            hostname: traefik
            command:
                - "--api=true"
                - "--api.debug=true"
                - "--api.insecure=true"
                #- --providers.docker=true  
                #- --traefik.http.routers.traefik.rule=Host(`traefik.localhsot`)  
                - "--providers.docker.endpoint=unix:///var/run/docker.sock"
                - "--providers.docker.swarmMode=true"
                - "--providers.docker.exposedbydefault=false"
                - "--providers.docker.network=app_intnet"
               #- --providers.docker.network=app_extnet  
                - "--entrypoints.web.address=:80"
            ports:
                - "80:80"
                - "8080:8080"
            volumes:
                - /var/run/docker.sock:/var/run/docker.sock:ro
               #- /home/erfan/joomla/data/traefik.yml:/traefik.yml:ro
               #- /home/erfan/joomla/data/acme.json:/acme.json  
            deploy:
                placement:
                    constraints:
                        - node.role == manager
            networks:
               - intnet
               - extnet
    networks:
         intnet:
            internal: true
            driver: overlay
            ipam:
              driver: default
              config:
                  - subnet: 192.168.200.0/24
         extnet:
           driver: overlay
           #external: true
           ipam:
             driver: default
             config:
                 - subnet: 192.168.100.0/24

and these are docker networks :

     NETWORK ID          NAME                DRIVER              SCOPE
        ukzzfno60hbc        app_extnet          overlay             swarm
        qtj33re0udg1        app_intnet          overlay             swarm
        7a29fd57a6e7        bridge              bridge              local
        01595efaa286        docker_gwbridge     bridge              local
        96be2c7598cf        host                host                local
        26migzlan45q        ingress             overlay             swarm
        b54bbc08d908        none                null                local

when I enter : joomla.localhost or pmy.localhost, it does not open the page but when I enter the IP address it is working and I don't understand why it is happening, would be glad if anyone can help me

Upvotes: 2

Views: 3510

Answers (1)

Al-waleed Shihadeh
Al-waleed Shihadeh

Reputation: 2845

you are missing the following label from the services

- traefik.docker.network=intnet

here is a full and simple example for using Traefik with Docker Swarm, also more info can be found in this post

version: '3.7'

networks:
  traefik:
    external: true

volumes:
  db_data:

services:

  proxy:
    image: traefik:v2.1
    command:
      - '--providers.docker=true'
      - '--entryPoints.web.address=:80'
      - '--providers.providersThrottleDuration=2s'
      - '--providers.docker.watch=true'
      - '--providers.docker.swarmMode=true'
      - '--providers.docker.swarmModeRefreshSeconds=15s'
      - '--providers.docker.exposedbydefault=false'
      - '--providers.docker.defaultRule=Host("local.me")'
      - '--accessLog.bufferingSize=0'
      - '--api=true'
      - '--api.dashboard=true'
      - '--api.insecure=true'
      - '--ping.entryPoint=web'
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock:ro'
    ports:
      - '80:80'
      - '8080:8080'
    deploy:
      restart_policy:
        condition: any
        delay: 5s
        max_attempts: 3
        window: 120s
      update_config:
        delay: 10s
        order: start-first
        parallelism: 1
      rollback_config:
        parallelism: 0
        order: stop-first
    logging:
      driver: json-file
      options:
        'max-size': '10m'
        'max-file': '5'
    networks:
      - traefik

  mysql:
    image: mysql:5.7
    command: mysqld --general-log=1 --general-log-file=/var/log/mysql/general-log.log
    deploy:
      restart_policy:
        condition: any
        delay: 5s
        max_attempts: 3
        window: 120s
      update_config:
        delay: 10s
        order: start-first
        parallelism: 1
      rollback_config:
        parallelism: 0
        order: stop-first
    logging:
      driver: json-file
      options:
        'max-size': '10m'
        'max-file': '5'
    networks:
      - traefik
    volumes:
      - db_data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: dummy
      MYSQL_DATABASE: rails_blog_production

  rails_blog_web:
    image: wshihadeh/rails_blog:demo-v1
    command: 'web'
    deploy:
      labels:
        - traefik.enable=true
        - traefik.http.services.blog.loadbalancer.server.port=8080
        - traefik.http.routers.blog.rule=Host(`blog.local.me`)
        - traefik.http.routers.blog.service=blog
        - traefik.http.routers.blog.entrypoints=web
        - traefik.docker.network=traefik
      restart_policy:
        condition: any
        delay: 5s
        max_attempts: 3
        window: 120s
      update_config:
        delay: 10s
        order: start-first
        parallelism: 1
      rollback_config:
        parallelism: 0
        order: stop-first
    logging:
      driver: json-file
      options:
        'max-size': '10m'
        'max-file': '5'
    networks:
      - traefik
    depends_on:
      - mysql
    environment:
      DATABASE_URL: mysql2://root:dummy@mysql/rails_blog_production
      RAILS_SERVE_STATIC_FILES: 'true'

Upvotes: 2

Related Questions