traefik logo


Traefik is an edge router, it distribute all my HTTP HTTPS and TCP request to the good docker container. This page dscribe how i setup a docker traefik instance in my personal docker swarm Environement : 2 managers and 2 workers. Hosted on 4 DigitalOcean’s droplets.

This article explain how i setup trafik and his dashborad.

Docker file

I want traefik to listen port :80 and :443 and i’ll publish the ports in host mode to be sure to have the real IP’s of the clients (I need them for security purpose, allow only my IP by example) :

      - target: 80
        published: 80
        protocol: tcp
        mode: host
      - target: 443
        published: 443
        protocol: tcp
        mode: host

For the :443 port, i want a valid SLL certificate, so i need to define a Cloudflare environement variable filled with my cloudflare mail and API key (this could be done in the static configuration file, but, i simply choose to write my credentals here):

      - ""
      - "CF_API_KEY=cloudflare_super_secure_key"

Next step is to bind some needed volumes to my container :

      # Directory for letsencrypt files
      - "/var/data/traefik2/letsencrypt:/letsencrypt"

      # Mount the docker socket
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

      # Mount the static traefik.toml file
      - "/var/data/traefik2/conf/traefik.toml:/etc/traefik/traefik.toml"

      # Directory for the logs
      - "/var/data/traefik2/log/:/log"

      # Directory for the config files
      - "/var/data/traefik2/conf:/config"

      # Set the same timezone as the host
      - "/etc/timezone:/etc/timezone:ro"
      - "/etc/localtime:/etc/localtime:ro"

You can view the full docker-compose.yml file here

Configuration files


Now that the compose file is ready It’s time to setup traefik.toml static configuration file.

First let’s send Anonymous usage to help the Traefik team

  sendAnonymousUsage = true

I then need to tell traefik witch port to use (aka the same ports i exposed in the docker-file.yml)

    address = ":80"
    address = ":443"

The next step is to define the providers


  # Docker

    # Traefik will watch for docker container change
    watch = true

    # The endpoint to look at
    endpoint = "unix:///var/run/docker.sock"

    # My containers will  never be exposed if i don't configure them
    exposedByDefault = false

    # Docker is in swarm mode
    swarmMode = true

    # The default docker network to look at
    network = "lan_nosecure"

  # File (aka traefik.toml and/or others dynamic configuration files)

    # My configuratioin files will be located in thiis directory
    directory = "/config"

    # Traefik will watch for file change
    watch = true

I want the API to be rechable (i’ll define later in the dynamic.toml file how to access it)

  insecure = false
  dashboard = true
  debug = true

Send metrics to my influxDB

    address = "http://IP:8086"
    protocol = "http"
    pushInterval = "10s"
    database = "traefik"
    retentionPolicy = ""
    addEntryPointsLabels = true
    addServicesLabels = true

Define the log and the access files

  level = "error"
  filePath = "/log/traefik.log"

  filePath = "/log/access.log"

I’ll next set the SSL certificate configuration


      # Where to save my certificates
      storage = "/letsencrypt/acme.json"

      # When you're good to go you can remove this line (it's the letsencrypt testing server)
      caServer = ""

      # I choose to use the DNS challenge and wait 10 seconds before check
        provider = "cloudflare"
        delayBeforeCheck = 10

You can view the full traefik.toml file here


The hard part !

I consider this part as the “tricky” part of the setup as it’s more complex to write. It’s require patience and no typo !

I first want all my HTTP traffic to be SSL encrypted so i define a global redirect. All the HTTP requests will be routed to the HTTPS router :


# Define a router and name it redirecttohttps

    # This router will use this entrypoint
    entryPoints = ["http"]

    # Definition of what to catch (this regex means all)
    rule = "HostRegexp(`{host:.+}`)"

    # The traffic that enter trought this entrypoint is send to this middleware
    middlewares = ["httpsredirect"]

    # Each middleware must have a service
    service = "noop"

I also have to define this service (to avoid an error, even if this one will never be called).

          url = ""

Time to define the httpsredirect middleware.

    scheme = "https"
    permanent = true

This will tell traefik to listen all the HTTP requests (:80 as defined in traefik.toml) and redirect them to the HTTPS entrypoint (:443 as defined in traefik.toml)

I also want the traefik dashboard to be rechable from home, so i defined the router named traefik and apply the home-ipwhitelist middleware to the requests.


    # on witch entrypoint this router will listen
    entryPoints = ["https"]

    # the rule definition
    rule = "Host(``)"

    # the requests have to go trought theses middlewares before the service
    middlewares = ["home-ipwhitelist@file", "security-headers@file"]

    # Witch service this router will serve
    service = "api@internal"

    # this define the resolver this router will use to generate the SSL certiificate
      certResolver = "cloudflare"

Lets define the middlewares used by the traefik router.

This one is for the headers, you can see on that this configuration is graded “A”. It include, CORS headers, security headers and some policy. A good ressource for undestanding headers :

      AccessControlAllowMethods = ["GET", "OPTIONS", "PUT"]
      AccessControlAllowOrigin = "origin-list-or-null"
      AccessControlMaxAge = 100
      #AddVaryHeader = true
      BrowserXssFilter = true
      ContentTypeNosniff = true
      ForceSTSHeader = true
      FrameDeny = true
      SSLRedirect = true
      #STSIncludeSubdomains = true
      STSPreload = true
      CustomFrameOptionsValue = "SAMEORIGIN"
      ReferrerPolicy = "same-origin"
      FeaturePolicy = "geolocation 'self'"
      STSSeconds = 315360000

Now the home IP middleware.

    sourceRange = ["homeIP/32"]

You can view the full traefik.toml file here


I have separated this configuration in a dedicated file, you can see on that this configuration is secured.


    sniStrict = true
    cipherSuites = [

Find the tls.toml file here

Here we are !, the configuration is done, and, as you can see (you navigate to this site trought traefik right now) it’s workinig like a charm !

I really thanks the traefik team for this fast, secure and marvelous product ❤️.

Thanks for reading.