Search code examples
http-redirectnetwork-programmingtraefik

How to get traefik to redirect to specific non-docker port from inside docker


First of all I'm sorry if I'm not using the right terms to ask this question, but I'm not up to the terminology in place.

I have traefik running in a docker container and serving some services with the PathPrefix option, for instance, www.myserver.com/wordpress redirects to a docker container running wordpress.

But how do I get it to redirect to outside a docker container? Specifically, how do I get www.myserver.com to redirect to port 8080 in my machine to serve a service I have running there in the host OS (not in a docker container)?

This is my traefik.toml:

logLevel = "DEBUG"
defaultEntryPoints = ["http", "https"]

[entryPoints]
    [entryPoints.http]
        address = ":80"
        compress = false
        [entryPoints.http.redirect]
            entryPoint = "https"
    [entryPoints.https]
        address = ":443"
        [entryPoints.https.tls]

[acme]
email = "mymail@mail.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
#onDemand = true
[[acme.domains]]
    main = "www.myserver.com"

[web]
address = ":8888"

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "www.myserver.com"
watch = true
exposedbydefault = false

And my docker-compose.yml for the traefik container:

version: "2"

services:
  traefik:
    image: traefik
    network_mode: "host"
    ports:
      - "80:80"
      - "443:443"
      - "8888:8888"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ${SERVER_DIR}/AppData/traefik:/etc/traefik/
      - ${PWD}/acme.json:/acme.json
      - ${PWD}/traefik.toml:/etc/traefik/traefik.toml
      - ${PWD}/servers.toml:/etc/traefik/servers.toml
    restart: never

Solution

  • I've fiddled around and found the answer.

    In traefik.toml add:

    ################################################################
    # File configuration backend
    ################################################################
    # Enable file configuration backend
    # Optional
    [file]
            filename = "servers.toml"
    
    # Enable watch file changes
            watch = true
    

    In docker-compose.yml change the volumes: to:

    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ${SERVER_DIR}/AppData/traefik:/etc/traefik/
      - ${PWD}/acme.json:/acme.json
      - ${PWD}/traefik.toml:/etc/traefik/traefik.toml
      - ${PWD}/servers.toml:/servers.toml
    

    Add file servers.toml:

    loglevel = "ERROR"
    
    [backends]
    [backends.nasweb]
            [backends.nasweb.servers.nasweb]
                url = "http://192.168.1.11:8080"
    
    [frontends]
            [frontends.domain]
                    backend = "nasweb"
            [frontends.domain.routes.domain]
                    rule = "Host:www.myserver.com"