Skip to content

Traefik Example: local host, http only

Containers

  1. Dashboard - http://traefik.loc ( admin:password )
  2. Nginx http://nginx.loc
  3. Whoami http://nginx.loc/whoami
# docker ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED          STATUS          PORTS                                 NAMES
2155e0e8676e   nginx:alpine            "/docker-entrypoint.…"   20 minutes ago   Up 20 minutes   80/tcp                                nginx
96dea35a139d   traefik:v3.3            "/entrypoint.sh --ap…"   20 minutes ago   Up 20 minutes   0.0.0.0:80->80/tcp, [::]:80->80/tcp   traefik
6b234f7eaf8d   traefik/whoami:latest   "/whoami"                20 minutes ago   Up 20 minutes   80/tcp                                whoami

Files

/etc/hosts

echo "127.0.0.1 nginx.loc traefik.loc" >> /etc/hosts

docker-compose.yml

services:
  traefik:
    image: traefik:v3.3
    container_name: traefik
    restart: always
    command:
      #- "--log.level=DEBUG"
      #- "--log.filePath=/var/log/traefik/traefik.log"
      #- "--accessLog.filePath=/var/log/traefik/access.log"
      - "--api.insecure=false"
      - "--api.dashboard=true"
      - "--api.debug=true"
      - "--providers.docker=true"
      - "--providers.docker.exposedbydefault=false"
      - "--entrypoints.web.address=:80"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      # - ./log/:/var/log/traefik/
    ports:
      - "80:80"
    networks:
      - traefik
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.dashboard.entrypoints=web"
      - "traefik.http.routers.dashboard.rule=Host(`traefik.loc`)"
      - "traefik.http.routers.dashboard.service=api@internal"
      - "traefik.http.routers.dashboard.middlewares=auth-dashboard"
      - "traefik.http.middlewares.auth-dashboard.basicauth.users=admin:$$apr1$$OB/RXgaZ$$e5Wqqg8lvdhkdXMildSUu1"

  nginx:
    image: nginx:alpine
    container_name: nginx
    restart: always
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    networks:
      - traefik
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.nginx.rule=Host(`nginx.loc`)"
      - "traefik.http.routers.nginx.entrypoints=web"
      - "traefik.http.services.nginx.loadbalancer.server.port=80"

  whoami:
    image: traefik/whoami:latest
    container_name: whoami
    restart: unless-stopped
    networks:
      - traefik
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`nginx.loc`) && PathPrefix(`/whoami`)"
      - "traefik.http.services.whoami.loadbalancer.server.port=80"

networks:
  traefik:
    driver: bridge
    name: traefik

./nginx.conf

events {
  worker_connections 1000;
  multi_accept on;
}

http {

server {
  listen 80 default_server;
  server_name _;

  access_log /dev/stdout;
  error_log /dev/stderr;

  location / {
    default_type text/plain;
    return 200 '200 nginx in traefik';
  }
}
}