Search code examples
dockerdocker-composetraefik

How to properly configure traefik in docker-compose.yml?


I'm playing with Traefik and trying to set it up, but I get the feeling that it doesn't see the traefki.yml configuration file, because the redirect from http to https doesn't work, and when I try to go to mydomain.com, I get "This site can't be reached " or "404 Not Found". I can not understand what the problem is, it seems that I did everything according to the documentation.

My docker-compose.yml:

version: "3.8"
services:

  traefik:
    image: "traefik:v2.6"
    container_name: "reverse-proxy"
    command:
      # Enable Docker in Traefik, so that it reads labels from Docker services
      - "--providers.docker=true"
      # Do not expose all Docker services, only the ones explicitly exposed
      - "--providers.docker.exposedbydefault=false"
      - "--providers.file.directory=/etc/traefik/dynamic"
    ports:
      # Listen on port 80, default for HTTP, necessary to redirect to HTTPS
      - 80:80
      # Listen on port 443, default for HTTPS
      - 443:443
    volumes:
      # Add Docker as a mounted volume, so that Traefik can read the labels of other services
      - /var/run/docker.sock:/var/run/docker.sock:ro
      # Mount the dynamic configuration
      - ./data/traefik.yml:/etc/traefik/dynamic/traefik.yml
      # Mount the directory containing the certs
      - ./certs:/etc/certs/
    networks:
      # Use the public network created to be shared between Traefik and
      # any other service that needs to be publicly available with HTTPS
      - reverse-proxy-public

  landing:
    container_name: "landing-page"
    restart: always
    build:
      context: "landing/nginx"
      dockerfile: "Dockerfile"
    volumes:
      - ./landing/nginx/conf.d:/etc/nginx/conf.d
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.landing.entrypoints=websecure"
      - "traefik.http.routers.landing.rule=Host(`mydomain.com`)"
      - "traefik.http.services.landing.loadbalancer.server.port=8000"
      - "traefik.http.routers.landing.tls=true"
    networks:
      - reverse-proxy-public

networks:
  # Use the previously created public network "reverse-proxy-public", shared with other
  # services that need to be publicly available via this Traefik
  reverse-proxy-public:
    external: true

My traefik.yml:

global:
  checkNewVersion: true
  sendAnonymousUsage: false  # true by default

# Log information
# ---
log:
  level: ERROR  # DEBUG, INFO, WARNING, ERROR, CRITICAL
  format: common  # common, json, logfmt
  filePath: /var/log/traefik/traefik.log

# Accesslog
# ---
accesslog:
  format: common  # common, json, logfmt
  filePath: /var/log/traefik/access.log

# Entry Points configuration
# ---
entryPoints:
  web:
    address: :80
    # Redirect to HTTPS
    # ---
    http:
      redirections:
        entryPoint:
          to: websecure
          scheme: https

  websecure:
    address: :443

# Overwrite Default Certificates
# ---
tls:
 stores:
   default:
     defaultCertificate:
       certFile: /etc/certs/mydomain.com.crt
       keyFile: /etc/certs/mydomain.com.key

Solution

  • You copy your traefik.yml to /etc/traefik/dynamic.

    At startup, Traefik searches for a file named traefik.yml (or traefik.yaml or traefik.toml) in:

    • /etc/traefik/
    • $XDG_CONFIG_HOME/
    • $HOME/.config/
    • . (the working directory).

    You can override this using the configFile argument, so just add:

    version: "3.8"
    services:
    
      traefik:
        image: "traefik:v2.6"
        container_name: "reverse-proxy"
        command:
          - "--configFile=etc/traefik/dynamic/traefik.yml"
          ...