jlandercy
jlandercy

Reputation: 11042

Port forwarding with traefik and docker-compose

I would like to serve a docker-compose service through traefik with port-forwarding. I had many tries and the best I could achieve from now is described below:

First I create two networks:

docker network create frontend  # To expose traefik
docker network create backend   # To bind backend services

The traefik configuration is about (development, dashboard enabled at :8080):

version: "3.6"

services:
  proxy:
    image: traefik:latest
    restart: unless-stopped
    command:
      - "--log.level=DEBUG"
      - "--api"
      - "--api.dashboard"
      - "--api.insecure"
      - "--providers.docker"
      - "--providers.docker.exposedbydefault=false"
      # Service entrypoint:
      - "--entrypoints.lora-server.address=:8090"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=frontend"
      - "traefik.http.routers.traefik.entrypoints=traefik"
      - "traefik.http.routers.traefik.service=api@internal"
    networks:
      - backend
      - frontend
    ports:
      - "8080:8080"
      - "8090:8090"

networks:
  frontend:
    external: true
  backend:
    external: true

The backend service is described here (a fork from ChripStack but it could be anything else):

version: "3"

services:
  # [...]
  chirpstack-application-server:
    image: chirpstack/chirpstack-application-server:3
    networks:
      - backend
    volumes:
      - ./configuration/chirpstack-application-server:/etc/chirpstack-application-server
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=backend"
      - "traefik.http.routers.chirpstack.entrypoints=lora-server"
      - "traefik.http.routers.chirpstack.rule=Host(`{host:}`)"
      - "traefik.http.routers.chirpstack.service=chirpstack-application-server@docker"
      - "traefik.http.services.chirpstack-application-server.loadbalancer.server.port=8080"
# [...]
networks:
  backend:
    external: true

The service also natively run on :8080 and I would like to access it on :8090 through traefik.

When I run both applications, traefik registers the new service and does not complain (no errors, no warning, the flow seems complete at least from the dashboard interface).

time="2020-07-30T11:47:47Z" level=debug msg="Creating middleware" middlewareType=Pipelining serviceName=chirpstack-application-server@docker entryPointName=lora-server routerName=chirpstack@docker middlewareName=pipelining
time="2020-07-30T11:47:47Z" level=debug msg="Creating load-balancer" entryPointName=lora-server routerName=chirpstack@docker serviceName=chirpstack-application-server@docker
time="2020-07-30T11:47:47Z" level=debug msg="Creating server 0 http://192.168.112.9:8080" entryPointName=lora-server routerName=chirpstack@docker serviceName=chirpstack-application-server@docker serverName=0
time="2020-07-30T11:47:47Z" level=debug msg="Added outgoing tracing middleware chirpstack-application-server@docker" middlewareName=tracing entryPointName=lora-server routerName=chirpstack@docker middlewareType=TracingForwarder

enter image description here enter image description here

But I could not access the service, I am geting 404 errors when I try to connect http://host:8090.

To my understanding, it seems traefik does not know how it should complete the flow between the two networks: http://frontend:8090 -> http://backend:8080 (because I haven't referenced it anywhere).

What should I change in my configuration to make it work? How can I specify to traefik that it must route the HTTP traffic from frontend:8090 to backend:8080? Your help is much appreciated.

Upvotes: 0

Views: 8743

Answers (1)

dmaixner
dmaixner

Reputation: 874

Traefik listens on some port, that's the only thing defined with entrypoints. You don't specify network for this incoming requests, because it's unrelated. traefik.docker.network is only used for routing after Traefik handles the incoming requests.

So the correct flow is not http://frontend:8090 -> http://backend:8080, but http://host:8090 -> http://backend:8080.

I think your configuration is correct. The only thing which seems to be odd is usage of Host rule. Maybe try to change it to something like this:

"traefik.http.routers.chirpstack.rule=hostregexp(`{host:.+}`)"

Upvotes: 1

Related Questions