Traefik reverse proxy – simple example for Docker to play around with

Traefik is an open-source Edge Router that makes publishing your services a fun and easy experience. It receives requests on behalf of your system and finds out which components are responsible for handling them.

Traefik Architecture

The following code snipped is a docker-compose file to spin up 2 docker containers.

  • Traefik
  • Whoami (Simple Webserivce)
version: "3.3"

services:

  traefik:
    image: "traefik:v2.4"
    container_name: "traefik"
    command:
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    ports:
      - "8002:80"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  whoami:
    image: "containous/whoami"
    container_name: "simple-service"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Path(`/test`)"
      - "traefik.http.routers.whoami.entrypoints=web"
      - "traefik.http.services.whoami.loadbalancer.server.port=80"

After successfully spinning up those containers you can access the simple webservice through Traefik in you webbrowser at http://localhost:8002/test

Here is a little bit different example which uses a different entrypoint port in Traefik, but gives you the absolute same result and the webservice is also accessible at http://localhost:8002/test

version: "3.3"

services:

  traefik:
    image: "traefik:v2.4"
    container_name: "traefik"
    command:
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:2001"
    ports:
      - "8002:2001"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  whoami:
    image: "containous/whoami"
    container_name: "simple-service"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoamirouter.rule=Path(`/blub`)"
      - "traefik.http.routers.whoamirouter.entrypoints=web"
      - "traefik.http.routers.whoamirouter.service=whoamisvc"
      - "traefik.http.services.whoamisvc.loadbalancer.server.port=80"