【问题标题】:Traefik can't connect to server with docker-composeTraefik 无法使用 docker-compose 连接到服务器
【发布时间】:2020-05-13 12:07:15
【问题描述】:

我正在尝试在阿里云实例中安装gitea 和traefik,但我无法从traefik 获得任何页面或响应。我该如何调试呢? docker logs traefik 没有错误

这是我的docker-compose.yml

version: "3"

networks:
  gitea_net:
    external: true
  internal:
    external: false

services:
  db:
    image: postgres:9.6
    restart: always
    environment:
      - POSTGRES_USER=gitea
      - POSTGRES_PASSWORD=gitea
      - POSTGRES_DB=gitea
    labels:
      - "traefik.enable=false"
    networks:
      - internal
    volumes:
      - ./postgres:/var/lib/postgresql/data

  traefik:
    image: traefik:latest
    command: --docker
    ports:
      - 80:80
      - 443:443
    labels:
      - "traefik.enable=true"
      - "traefik.backend=dashboard"
      - "traefik.frontend.rule=Host:traefik.mydomain.com"
      - "traefik.port=8080"
    networks:
      - gitea_net
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
      - ./acme.json:/acme.json
    container_name: traefik
    restart: always

  server:
    image: gitea/gitea:latest
    environment:
      - USER_UID=1000
      - USER_GID=1000
    restart: always
    networks:
      - internal
    volumes:
      - ./gitea:/data
    ports:
      - "3000"
      - "22"
    labels:
      - "traefik.enabled=true"
      - "traefik.backend=gitea"
      - "traefik.frontend.rule=Host:gitea.mydomain.com"
      - "traefik.docker.network=gitea_net"
      - "traefik.port=3000"
    networks:
      - internal
      - gitea_net
    depends_on:
      - db
      - traefik  

这是我的traefik.toml

#Traefik Global Configuration
debug = true
checkNewVersion = true
logLevel = "ERROR"

#Define the EntryPoint for HTTP and HTTPS
defaultEntryPoints = ["https","http"]

#Define the HTTP port 80 and
#HTTPS port 443 EntryPoint
#Enable automatically redirect HTTP to HTTPS
[entryPoints]
[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"
[entryPoints.https]
address = ":443"
[entryPoints.https.tls]

#Enable Traefik Dashboard on port 8080
#with basic authentication method

[entryPoints.dash]
address=":8080"
[entryPoints.dash.auth]
[entryPoints.dash.auth.basic]
    users = [
        "admin:$apr1$nw$PAVvqQK30eAdrY0l9KCnK1",
    ]

[api]
entrypoint="dash"
dashboard = true

#Enable retry sending a request if the network error
[retry]

#Define Docker Backend Configuration
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "mydomain.com"
watch = true
exposedbydefault = false

#Letsencrypt Registration
#Define the Letsencrypt ACME HTTP challenge
[acme]
email = "mark@gmail.com"
storage = "acme.json"
entryPoint = "https"
OnHostRule = true
  [acme.httpChallenge]
  entryPoint = "http"

【问题讨论】:

    标签: docker-compose traefik


    【解决方案1】:

    我可能错了,但您的设置可能仅对 2.0 之前的 Traefik 有效。

    您可以通过将 docker 镜像标签从 traefik:latest 更改为 traefik:1.7 来快速检查。

    如果您仍然无法使其工作,请尝试我的设置,我知道它正在工作,因为正在使用。如果我的设置适合​​您,那么您可以将其与您的设置进行比较,看看您是如何让您的设置工作的。我的和你的主要区别是我使用 Treafik 1.7 并且我将 Traefik 作为一个单独的 docker 堆栈部署在服务器中,因为我相信这是正确的做法,至少如果你想利用它来在同一台服务器上调整尽可能多的服务。

    另外需要注意的是,用于存储 Letsencrypt 证书的 acme.json 文件必须具有 600 权限。但是如果我记得很清楚,这个权限问题不会导致 traefik 继续重启,但不确定,我没有在我的bash script 中详细说明设置 Traefik:

    # Traefik will not create the certificates if we don't fix the permissions
    #  for the file where it stores the LetsEncrypt certificates.
    chmod 600 acme.json
    

    docker-compose.yml:

    version: '2.3'
    
    services:
      traefik:
        image: traefik:1.7
        restart: always
        ports:
          - 80:80
          - 443:443
        networks:
          - traefik
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
          - ./traefik.toml:/traefik.toml
          - ./acme.json:/acme.json
        container_name: traefik
        labels:
          - "traefik.acme.email=${TRAEFIK_ACME_EMAIL:? Missing TRAEFIK_ACME_EMAIL env var.}"
          - "traefik.docker.domain=${TRAEFIK_DOCKER_DOMAIN:? Missing TRAEFIK_DOCKER_DOMAIN env var.}"
    networks:
      traefik:
        external: true
    

    网络是外部的,所以你需要运行docker network create traefik。

    docker-compose.yml 文件中的变量来自.env file:

    TRAEFIK_DOCKER_DOMAIN=dev.example.com
    TRAEFIK_ACME_EMAIL=YOUR@EMAIL.COM
    

    traefik.toml:

    debug = false
    
    logLevel = "ERROR"
    defaultEntryPoints = ["https","http"]
    
    [web]
    address = ":8080"
    
    [entryPoints]
      [entryPoints.http]
      address = ":80"
        [entryPoints.http.redirect]
        entryPoint = "https"
    
      [entryPoints.https]
      address = ":443"
      [entryPoints.https.tls]
    
    [retry]
    
    [docker]
    endpoint = "unix:///var/run/docker.sock"
    watch = true
    exposedByDefault = false
    
    [acme]
    storage = "acme.json"
    entryPoint = "https"
    onHostRule = true
    [acme.httpChallenge]
    entryPoint = "http"
    

    从另一个 docker-compose.yml 文件附加 docker compose 服务的示例可以在 here 找到:

      service-name:
        ....
        networks:
          - shipfast
          - traefik
        labels:
          - "traefik.enable=true"
          - "traefik.backend=${ENVIRONMENT:-dev}.shipfast-api"
          - "traefik.docker.network=traefik"
          - "traefik.port=${SHIPFAST_HTTP_PORT}"
          - "traefik.frontend.rule=Host:${SHIPFAST_PUBLIC_DOMAIN:-localhost}"
    
    .....
    
    networks:
      shipfast:
        driver: "bridge"
      traefik:
        external: true
    

    【讨论】:

      猜你喜欢
      • 2016-08-10
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2021-05-17
      • 2023-01-26
      • 2023-02-23
      • 1970-01-01
      相关资源
      最近更新 更多