docker - 无法访问Traefik后面的Docker容器

标签 docker docker-compose traefik

我试图访问在我的远程服务器上运行的whoami容器,但只能出现“找不到404页”错误。尝试访问traefik仪表板时得到相同的结果。
我的docker-compose.yml:

version: "3.7"
services:
  traefik:
    image: traefik:v2.3.0
    container_name: traefik
    restart: unless-stopped
    command: # CLI arguments
      ## Globals
      - "--global.checkNewVersion=false"
      - "--global.sendAnonymousUsage=false"
      ## Entrypoint Settings - https://docs.traefik.io/routing/entrypoints/#configuration ##
      - "--entrypoints.http.address=:80"
      - "--entrypoints.http.http.redirections.entryPoint.to=https"
      - "--entrypoints.http.http.redirections.entryPoint.scheme=https"
      - "--entrypoints.https.address=:443"
      ## API Settings
      - "--api=true"
      - "--api.dashboard=true"
      - "--log=true"
      - "--log.level=DEBUG" # (Default: error) DEBUG, INFO, WARN, ERROR, FATAL, PANIC
      - "--providers.docker=true"
      - "--providers.docker.watch=true"
      - "--providers.docker.exposedByDefault=false"
      ## Certificate Settings (Let's Encrypt) -  https://docs.traefik.io/https/acme/#configuration-examples ##
      - "--certificatesresolvers.mytlschallenge.acme.caServer=https://acme-staging-v02.api.letsencrypt.org/directory" # TBD - TESTING
    networks:
      - frontend
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    security_opt:
      - "no-new-privileges:true" # https://docs.docker.com/engine/reference/run/#security-configuration
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "$USERDIR/ctmp/acme/acme.json:/acme.json:rw" # cert location - you must touch this file and change permissions to 600
    labels:
      - "traefik.enable=true"
      ## HTTP Routers
      - "traefik.http.routers.traefik-rtr.rule=HostHeader(`traefik.${DOMAIN}`)"
      - "traefik.http.routers.traefik-rtr.entrypoints=https"
      - "traefik.http.routers.traefik-rtr.service=api@internal"

  whoami:
    image: "traefik/whoami"
    container_name: "simple-service"
    networks:
      - frontend
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=HostHeader(`whoami.${DOMAIN}`)"
      - "traefik.http.routers.whoami.entrypoints=http"

networks:
  frontend:
    external: true
$ USERDIR和$ DOMAIN在我的.env文件中定义。
所有traefik日志均为infodebug级别,没有出现错误。

最佳答案

我现在没有时间,但是这里有一个快速的代码重写,但未经测试。
这只是一个略有不同的方法。但是我认为这可以达到相同的目标。

  • 您必须包括您的ENV文件
  • traefik.http.routers.api.rule = HostHeader = = Host(`...)。因此很奇怪,也应该与HostHeader一起使用。 Link
  • 现在,有了此基础,您可以对其进行自定义。我使用HTTP chlange,但是使用TLS chlange应该可以。
  • version: "3.7"
    services:
      traefik:
        image: traefik:v2.3.0
        container_name: traefik
        restart: unless-stopped
        env_file:
          - .env
        command: # CLI arguments
          ## Globals
          - "--global.checkNewVersion=false"
          - "--global.sendAnonymousUsage=false"
          ## Entrypoint Settings - https://docs.traefik.io/routing/entrypoints/#configuration ##
          - "--entrypoints.http.address=:80"
          - "--entrypoints.https.address=:443"
          ## API Settings
          - "--api=true"
          - "--api.insecure=false"
          - "--api.dashboard=true"
          - "--log.level=DEBUG" # (Default: error) DEBUG, INFO, WARN, ERROR, FATAL, PANIC
          - "--providers.docker=true"
          - "--providers.docker.exposedByDefault=false"
          ## Certificate Settings
          - "--certificatesresolvers.myresolver.acme.httpchallenge=true"
          - "--certificatesresolvers.myresolver.acme.httpchallenge.entrypoint=http"
          - "--certificatesresolvers.myresolver.acme.email=YOUR-EMAIL@your-domain.com"
          - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
        networks:
          - frontend
        ports:
          - "80:80"
          - "443:443"
          - "8080:8080"
        security_opt:
          - "no-new-privileges:true" # https://docs.docker.com/engine/reference/run/#security-configuration
        volumes:
          - "/var/run/docker.sock:/var/run/docker.sock:ro"
          - "./letsencrypt:/letsencrypt"
        labels:
          - "traefik.enable=true"
          ## HTTP Routers
          - "traefik.http.routers.api.rule=Host(`traefik.${DOMAIN}`)"
          - "traefik.http.routers.api.entrypoints=https"
          - "traefik.http.routers.api.service=api@internal"
          - "traefik.http.routers.api.tls.certresolver=myresolver"
          - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
          - "traefik.http.routers.redirect.rule=hostregexp(`{host:.+}`)"
          - "traefik.http.routers.redirect.middlewares=redirect-to-https"
    
      whoami:
        image: traefik/whoami
        container_name: simple-service
        networks:
          - frontend
        labels:
          - "traefik.enable=true"
          - "traefik.http.routers.whoami.rule=Host(`whoami.${DOMAIN}`)"
          - "traefik.http.routers.whoami.entrypoints=https"
          - "traefik.http.routers.whoami.tls.certresolver=myresolver"
    
    networks:
      frontend:
        external: true
    

    关于docker - 无法访问Traefik后面的Docker容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64187476/

    相关文章:

    docker - 具有 `hashicorp/terraform:full`的gitt_prehh图像错误的Gitlab CICD管道

    docker - LetsEncrypt 的 TLS 证书是否应该位于所有容器上?

    mysql - 从 Docker 卷恢复 mysql 数据

    docker - Docker 源代码在哪里(github repo)?

    docker - Nextcloud 在 traefik 后面的 docker 中 unraid

    docker - 在Traefik中禁用每个容器的访问日志

    kubernetes - 在 traefik TCP 路由映射中使用 hostSNI(*) 有什么缺点

    linux - 无法在ubuntu中执行bash命令: permissions denied

    python-3.x - PyCharm Django docker-compose 未解析的引用模型

    docker - 无法与具有多个Flask容器实例的Nginx docker反向代理