Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 = "[email protected]"
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
like image 756
jbssm Avatar asked Sep 15 '17 18:09

jbssm


People also ask

What port does Traefik use?

We're publishing the default HTTP ports 80 and 443 on the host, and making sure the container is placed within the web network we've created earlier on. Finally, we're giving this container a static name called traefik .

Is Traefik a reverse proxy?

Traefik is a leading modern reverse proxy and load balancer that makes deploying microservices easy. Traefik integrates with your existing infrastructure components and configures itself automatically and dynamically.

Why does Traefik need Docker sock?

Traefik requires access to the docker socket to get its dynamic configuration. You can specify which Docker API Endpoint to use with the directive endpoint . Accessing the Docker API without any restriction is a security concern: If Traefik is attacked, then the attacker might get access to the underlying host.

How do I access my Traefik container?

The Traefik project has an official Docker image, so you will use that to run Traefik in a Docker container. But before you get your Traefik container up and running, you need to create a configuration file and set up an encrypted password so you can access the monitoring dashboard.


1 Answers

With the new Traefik (v.2) you need to use a combination of labels and an external file, you can find below my working example.

In your docker compose you need to add the comands to define the external file and enable the provider

  - "--providers.file=true"
  - "--providers.file.filename=/etc/traefik/rules.toml"

Into your file (rules.toml) the routing to foward to your external service (be aware of the syntax, use the char to define the host ( ` ) )

example :

Docker-compose:

  traefik:
    image: "traefik:v2.0.0"
    container_name: "traefik"
    restart: always
    command:
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--certificatesresolvers.myhttpchallenge.acme.httpchallenge=true"
      - "--certificatesresolvers.myhttpchallenge.acme.httpchallenge.entrypoint=web"
      - "[email protected]"
      - "--providers.file=true"
      - "--providers.file.filename=/etc/traefik/rules.toml"
      - "--providers.docker=true"
      - "--providers.file.watch=true"
    ports:
      - "80:80"
      - "8080:8080"
      - "443:443"
    networks:
      - proxy
    environment:
      - CF_API_EMAIL="xx"
      - CF_API_KEY="xx"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./traefik/rules.toml:/etc/traefik/rules.toml"

Rules.toml

  [http.routers]
   # Define a connection between requests and services
     [http.routers.nasweb]
        rule = "Host(`nas.xxxx.com`)"
        entrypoints = ["websecure"]
        service = "nas"
     [http.routers.nasweb.tls]
        certResolver = "myhttpchallenge"


 [http.services]
        # Define how to reach an existing service on our infrastructure
        [http.services.nas.loadBalancer]
           [[http.services.nas.loadBalancer.servers]]
             url = "http://192.168.0.165:80"
like image 92
Philippe Guarino Avatar answered Sep 17 '22 06:09

Philippe Guarino