Installing Traefik on Linux

3 minutes read

Docker Setup

Firewall limitations:

Warning

Intallation (Debian)

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Update apt database
sudo apt update

# Install docker and other goodies
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Verify that the installation is successful by running the hello-world image:

sudo docker run hello-world

Traefik

mkdir traefik
cd traefik

sudo docker network create docbridge

vim docker-compose.yml

Put the following config:

---
services:

  traefik:
    image: "traefik:v3.2.1"
    container_name: "traefik"
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./config/traefik.yml:/etc/traefik/traefik.yml:ro"
      - "./config/conf:/etc/traefik/conf:ro"
    networks:
      - docbridge
    restart: unless-stopped
  
networks:
  docbridge:
    external: true

mkdir config

vim config/traefik.yml

Put the following config:

global:
  checkNewVersion: false
  sendAnonymousUsage: false
log:
  level: DEBUG
api:
  dashboard: true
  insecure: true
entryPoints:
  web:
    address: :80
  websecure:
    address: :443
providers:
  file:
    directory: "/etc/traefik/conf"
    watch: true

mkdir -p config/conf

vim config/conf/routes.yml

Put the following config

---
http:
  routers:
    nginx-http:
      rule: HostRegexp(`nginx`)
      service: service-1
      entryPoints: 
        - web
  services:
    service-1:
      loadBalancer:
        servers:
          - url: "http://nginx"

Note: For “url” we have provided the nginx container name which is “nginx”. That is because nginx in this set-up is running as a docker container. And the Docker bridge networks have internal DNS resolution built-in by Docker. That’s why it is possible to point to nginx by simply using the container name, “nginx” but we have to provide the protocol here which is “http://”. For the “url” We can also use “domain”, “IP” and “IP:Port”. For example, http://127.0.0.1:8080 or http://localhost:8000 or simply https://example.com and so on.

Setup demo docker containers (Nginx)

mkdir nginx

vim nginx/docker-compose.yml

Put the following config inside the docker-compose.yml.

---
services:
  nginx:
    image: "nginx:latest"
    container_name: "nginx_demo"
    restart: unless-stopped
    networks:
      - docbridge

networks:
  docbridge:
    external: true

Deploy docker containers

cd nginx

sudo docker compose up -d

cd traefik

sudo docker compose up -d

sudo docker compose down <container_name|container_ID|etc>

sudo docker compose down nginx

Done!