Search code examples
dockerdocker-composewebservertraefik

Traefik v2 reverse proxy to a local server outside Docker


I have a simple server written in Python that listens on port 8000 inside a private network (HTTP communication). There is now a requirement to switch to HTTPS communications and every client that sends a request to the server should get authenticated with his own cert/key pair.

I have decided to use Traefik v2 for this job. Please see the block diagram.

Traefik runs as a Docker image on a host that has IP 192.168.56.101. First I wanted to simply forward a HTTP request from a client to Traefik and then to the Python server running outside Docker on port 8000. I would add the TLS functionality when the forwarding is running properly.

However, I can not figure out how to configure Traefik to reverse proxy from i.e. 192.168.56.101/notify?wrn=1 to the Python server 127.0.0.1:8000/notify?wrn=1.

When I try to send the above mentioned request to the server (curl "192.168.56.101/notify?wrn=1") I get "Bad Gateway" as an answer. What am I missing here? This is the first time that I am in contact with Docker and reverse proxy/Traefik. I believe it has something to do with ports but I can not figure it out.

Here is my Traefik configuration:

  • docker-compose.yml
version: "3.3"

services:
  traefik:
    image: "traefik:v2.1"
    container_name: "traefik"
    hostname: "traefik"
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./traefik.yml:/traefik.yml:ro"
  • traefik.yml
## STATIC CONFIGURATION
log:
  level: INFO

api:
  insecure: true
  dashboard: true

entryPoints:
  web:
    address: ":80"

providers:
  docker:
    watch: true
    endpoint: "unix:///var/run/docker.sock"
  file:
    filename: "traefik.yml"


## DYNAMIC CONFIGURATION
http:
  routers:
    to-local-ip:
      rule: "Host(`192.168.56.101`)"
      service: to-local-ip
      entryPoints:
        - web

  services:
    to-local-ip:
      loadBalancer:
        servers:
          - url: "http://127.0.0.1:8000"

Solution

  • First, 127.0.0.1 will resolve to the traefik container and not to the docker host. You need to provide a private IP of the node and it needs to be accessible form the traefik container.