4  Traefik

To install Traefik directly using Docker commands without Docker Compose, follow the steps below:

4.0.1 Step 1: Create a Traefik Network

First, create a Docker network that Traefik and other containers will use:

docker network create traefik_network

4.0.2 Step 2: Run Traefik Using Docker

  • On your hard-drive, create a directory called /traefik. On Windows use C:/traefik. Use any other location if you wish.

  • In that directory create the file traefik.yml with the following contents:

## traefik.yml

global:
  checkNewVersion: true
  sendAnonymousUsage: false

log:
  level: DEBUG

accessLog: {}

#tracing:
#  elastic: {}

metrics:
  prometheus: {}

ping: {}

# API and dashboard configuration
api:
  dashboard: true
  insecure: true

# Docker entrypoints backend
entryPoints:
  web:
    address: :80

  web-secure:
    address: :443
    
# Docker configuration backend
providers:
  docker:
    defaultRule: 'Host(`{{ if index .Labels "com.docker.compose.service" }}{{ index .Labels "com.docker.compose.service" }}.aritlab.com{{ else }}{{ trimPrefix `/` .Name }}.aritlab.com{{ end }}`)'
    endpoint: unix:///var/run/docker.sock
    # For Windows
    # endpoint: "npipe:////./pipe/docker_engine"
    watch: true
    exposedByDefault: true
  • Use the following Docker command to install and run Traefik:
docker run -d --restart always -p 80:80 -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -v /traefik:/etc/traefik --network traefik_network --name traefik traefik:latest

Explanation:

  • -d: Runs the container in detached mode (in the background).

  • --restart always: Ensures Traefik restarts automatically if it stops.

  • --network traefik_network: Connects Traefik to the created Docker network.

  • -p: Maps host ports to container ports for HTTP (80), HTTPS (443), and the Dashboard (8080).

  • -v /var/run/docker.sock:/var/run/docker.sock:ro: Allows Traefik to interact with Docker and detect running containers.

  • -v /traefik:/etc/traefik: Allows Traefik to read the configuration file traefik.yml from local directory /traefik.

4.0.3 Step 3: Access the Traefik Dashboard

Open a web browser and navigate to http://your-server-ip:8080. You should see the Traefik dashboard showing active services and configurations.

4.0.4 Notes:

  • Make sure your DNS records are correctly set up to point to your server IP for the domain you specify.
  • For production, consider configuring HTTPS with Let’s Encrypt and securing the Traefik dashboard.
  • You can also customize Traefik further by creating a traefik.toml file and mapping it with the -v option.

This approach provides a quick way to set up Traefik using Docker without relying on Docker Compose.