Table of Contents

Using "Traefik" for Reverse Proxy and "Let's Encrypt" for Automatic HTTPS certificates

Important Note :

This is an alternative method to Using Nginx as a Reverse Proxy.

Purpose:

The purpose of using Traefik are:

Create the Folder and Config Files

#!/bin/bash
STORAGE_PATH="/home/docker-user/traefik"
mkdir -p "$STORAGE_PATH"/storage/traefik/data
touch "$STORAGE_PATH"/storage/traefik/data/acme.json
chmod 600 "$STORAGE_PATH"/storage/traefik/data/acme.json
touch "$STORAGE_PATH"/storage/traefik/data/traefik.yml

Deploy the Traefik configuration

nano traefik.yml
api:
  dashboard: true
 
entryPoints:
  http:
    address: ":80"
  https:
    address: ":443"
 
providers:
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false
 
certificatesResolvers:
  http:
    acme:
      email: email@example.com
      storage: acme.json
      httpChallenge:
        entryPoint: http

Create the traefik docker-compose.yml

echo $(htpasswd -nb <USER> <PASSWORD>) | sed -e s/\\$/\\$\\$/g
version: '3'
 
services:
  traefik:
    image: traefik:v2.0
    container_name: traefik
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    networks:
      - proxy
    ports:
      - 80:80
      - 443:443
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./storage/traefik/data/traefik.yml:/traefik.yml:ro
      - ./storage/traefik/data/acme.json:/acme.json
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.entrypoints=http"
      - "traefik.http.routers.traefik.rule=Host(`traefik.example.com`)"
      - "traefik.http.middlewares.traefik-auth.basicauth.users=USER:PASSWORD"
      - "traefik.http.middlewares.traefik-https-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.traefik.middlewares=traefik-https-redirect"
      - "traefik.http.routers.traefik-secure.entrypoints=https"
      - "traefik.http.routers.traefik-secure.rule=Host(`traefik.example.com`)"
      - "traefik.http.routers.traefik-secure.middlewares=traefik-auth"
      - "traefik.http.routers.traefik-secure.tls=true"
      - "traefik.http.routers.traefik-secure.tls.certresolver=http"
      - "traefik.http.routers.traefik-secure.service=api@internal"
 
networks:
  proxy:
    external: true
"traefik.http.routers.traefik-secure.tls=true"
      - "traefik.http.routers.traefik-secure.tls.certresolver=http"
      - "traefik.http.routers.traefik-secure.service=api@internal"
 
networks:
  proxy:
    external: true

Adding Services to Traefik

Start the traefik container - docker-compose up -d

Change your docker-compose.yml from other services to be available via Traefik.

As example, here is my docker-compose.yml for DokuWiki before and after Traefik.

Before

version: '3'

volumes:
    dokuwiki_data:
        external: true
    dokuwiki_conf:
        external: true
    dokuwiki_lib-plugins:
        external: true
    dokuwiki_lib-tpl:
        external: true
    dokuwiki_logs:
        external: true

services:
  dokuwiki:
    image: 'mprasil/dokuwiki'
    container_name: 'dokuwiki_zebra'
    ports:
      - '80:80'
    volumes:
        - dokuwiki_data:/dokuwiki/data
        - dokuwiki_conf:/dokuwiki/conf
        - dokuwiki_lib-plugins:/dokuwiki/lib/plugins
        - dokuwiki_lib-tpl:/dokuwiki/lib/tpl
        - dokuwiki_logs:/var/log

After

version: '3'

volumes:
    dokuwiki_data:
        external: true
    dokuwiki_conf:
        external: true
    dokuwiki_lib-plugins:
        external: true
    dokuwiki_lib-tpl:
        external: true
    dokuwiki_logs:
        external: true

services:
  dokuwiki:
    image: 'mprasil/dokuwiki'
    container_name: 'dokuwiki'
    restart: unless-stopped
    networks:
        - proxy
    #ports:
    #  - '80:80'
    volumes:
        - dokuwiki_data:/dokuwiki/data
        - dokuwiki_conf:/dokuwiki/conf
        - dokuwiki_lib-plugins:/dokuwiki/lib/plugins
        - dokuwiki_lib-tpl:/dokuwiki/lib/tpl
        - dokuwiki_logs:/var/log
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.dokuwiki.entrypoints=http"
      - "traefik.http.routers.dokuwiki.rule=Host(`wiki.carlossousa.tech`)"
      - "traefik.http.middlewares.dokuwiki-https-redirect.redirectscheme.scheme=https"
      - "traefik.http.routers.dokuwiki.middlewares=dokuwiki-https-redirect"
      - "traefik.http.routers.dokuwiki-secure.entrypoints=https"
      - "traefik.http.routers.dokuwiki-secure.rule=Host(`wiki.carlossousa.tech`)"
      - "traefik.http.routers.dokuwiki-secure.tls=true"
      - "traefik.http.routers.dokuwiki-secure.tls.certresolver=http"
      - "traefik.http.routers.dokuwiki-secure.service=dokuwiki"
      - "traefik.http.services.dokuwiki.loadbalancer.server.port=80"
      - "traefik.docker.network=proxy"

networks:
   proxy:
     external: true

After starting the service, it should now be available.