8

I am using traefik version 2(or 2.x) and I want to forward all the request from port 80 to different port like 8081 with traefik router. So request like http://localhost/xx will be forwarded to http://localhost:8081/xx URL.

I am newbie with traefik and I am using docker for this configuration. Below is my docker-compose.yml file configuration. After configuring this traefik dashboard is loaded on http://localhost:8080/dashboard/#/ URL but request forwarding is not worked.

version: "3"

services:
  traefik:
    image: "traefik:v2.1.0"
    container_name: "traefik"
    command:
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  whoami:
    image: "containous/whoami"
    container_name: "simple-service"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.entrypoints=web"
      - "traefik.http.services.whoami.loadbalancer.server.port=8081"
      - "traefik.docker.network=proxy"
      - "traefik.http.routers.whoami.rule=Host(`localhost`)"

Any help on this would be appreciated.

Dhaval Goti
  • 447
  • 2
  • 10
  • 25

1 Answers1

2

you need to map your service port to 8081

this is a fully working/tested example where you can access whoami
by going to http://whoami.docker.local:8081 or http://whoami.docker.local

version: "3"

services:
    traefik:
        image: traefik
        command:
            - --api.insecure=true
            - --providers.docker=true
        ports:
            - "80:80"
            - "8080:8080"
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
        labels:
            - traefik.http.routers.api.rule=Host(`traefik.docker.local`)
            - traefik.http.routers.api.service=api@internal

    whoami:
        image: containous/whoami
        ports:
            - "8081:80"
        labels:
            - traefik.http.routers.whoami.rule=Host(`whoami.docker.local`)
            - traefik.http.routers.whoami.service=whoami@docker
            - traefik.http.services.whoami.loadbalancer.server.port=80

it works on port 80 and also 8081, as per your request.

root@d:~# lsof -i :80,8081

COMMAND     PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
docker-pr 28208 root    4u  IPv6 51666675      0t0  TCP *:tproxy (LISTEN)
docker-pr 28265 root    4u  IPv6 51671715      0t0  TCP *:http (LISTEN)

but it could be easier to help if you explain why you want to access :8081,
because traefik is used so we dont have to do those kind of redirections.

RASG
  • 5,988
  • 4
  • 26
  • 47
  • Thanks for this. But still expected behavior not able to get with this configuration. – Dhaval Goti Jan 22 '20 at 08:56
  • @DhavalGoti edited my answer. full working example added. – RASG Jan 22 '20 at 14:02
  • I am using Springboot Application for url forwarding in place of containous/whoami web server which runs on 8081 port. So when I hit URL like http://localhost/x, spring app redirect based on x value provided. With this configuration I am still getting "Bad Gateway" when I hit URL http://localhost/x. – Dhaval Goti Jan 23 '20 at 06:17
  • 9
    In this example `http://whoami.docker.local:8081` will not be routed via traefik. – Sathesh Dec 23 '20 at 04:30