Published on

Deploying Traefik with Docker: A Step-by-Step Tutorial

Authors
  • avatar
    Name
    Kamil Jonak
    Twitter
  • Name
    Twitter
traefik-setup-banner

Deploying Traefik with Docker: A Step-by-Step Tutorial

This tutorial will guide you through the process of setting up Traefik as a reverse proxy using Docker. It covers essential steps including HTTPS configuration, Docker integration, and a simple demonstration with a sample web application.

Prerequisites

Before starting, ensure you have Docker and Docker Compose installed on your system. Basic knowledge of Docker and understanding of domain names and DNS configurations will be helpful.

Step 1: Create the Docker Network

docker network create web

This network will allow containers to communicate internally and with Traefik.

Step 2: Set Up Traefik Configuration

Traefik Configuration File (traefik.yml)

log:
  level: INFO

api:
  dashboard: true

entryPoints:
  web:
    address: ':80'
  websecure:
    address: ':443'

providers:
  docker:
    endpoint: 'unix:///var/run/docker.sock'
    watch: true
    exposedByDefault: false
    network: web

certificatesResolvers:
  le:
    acme:
      email: your-email@example.com
      storage: acme.json
      tlsChallenge: true

Docker Compose File for Traefik (docker-compose.yml)

version: '3.7'

services:
  traefik:
    image: traefik:v2.7
    container_name: traefik
    restart: unless-stopped
    command:
      - --api.insecure=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      - --certificatesresolvers.le.acme.tlschallenge=true
      - --certificatesresolvers.le.acme.email=your-email@example.com
      - --certificatesresolvers.le.acme.storage=/acme.json
    ports:
      - '80:80'
      - '443:443'
      - '8080:8080'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.yml:/traefik.yml
      - ./acme.json:/acme.json

networks:
  default:
    name: web

Step 3: Secure the Traefik Dashboard

labels:
  - 'traefik.http.routers.traefik.rule=Host(`traefik.yourdomain.com`)'
  - 'traefik.http.routers.traefik.service=api@internal'
  - 'traefik.http.routers.traefik.entrypoints=websecure'
  - 'traefik.http.routers.traefik.tls.certresolver=le'

Step 4: Deploy a Sample Web Application

Application Docker Compose (docker-compose.yml)

version: '3.7'

services:
  whoami:
    image: containous/whoami
    labels:
      - 'traefik.enable=true'
      - 'traefik.http.routers.whoami.rule=Host(`whoami.yourdomain.com`)'
      - 'traefik.http.routers.whoami.entrypoints=websecure'
      - 'traefik.http.routers.whoami.tls.certresolver=le'

networks:
  default:
    external:
      name: web

Step 5: Start Everything Up

docker-compose up -d

Step 6: Verify the Setup

Access the following:

Conclusion

By following this tutorial, you should now have a fully functional setup of Traefik running as a reverse proxy on Docker, enhancing your application delivery with automated HTTPS configuration and load balancing capabilities.