【问题标题】:Why am I getting this error when creating a stack on docker?为什么在 docker 上创建堆栈时出现此错误?
【发布时间】:2022-01-13 16:43:39
【问题描述】:

我是一个完整的码头工人菜鸟。刚刚按照教程安装了 docker 和 docker-compose 以及 portainer。

现在我想在 portainer 上设置 traefik 反向代理。

这是 /etc/traefik 中的 traefik.yml 文件

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

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

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

# (Optional) Enable API and Dashboard
# ---
 api:
  dashboard: true  # true by default
  insecure: true  # Don't do this in production!

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

  websecure:
    address: :443

# Certificates configuration
# ---
# TODO: Custmoize your Cert Resolvers and Domain settings
# 

这是 docker-compose 文件:

version: '3'

volumes:
  traefik-ssl-certs:
    driver: local

services:
  traefik:
    image: "traefik:v2.5"
    container_name: "traefik"
    ports:
      - "80:80"
      - "443:443"
      # (Optional) Expose Dashboard
      - "8080:8080"  # Don't do this in production!
    volumes:
      - /etc/traefik:/etc/traefik
      - traefik-ssl-certs:/ssl-certs
      - /var/run/docker.sock:/var/run/docker.sock:ro

但是当我尝试启动容器时,我得到了这个错误:

2021/12/08 18:08:07 命令 traefik 错误:yaml:第 19 行:没有找到预期的密钥

当我从 docker-compose 文件中删除“服务”下的整个“卷”部分时,我可以让容器运行,但我需要它来设置我的 traefik。我不知道我做错了什么,因为我正在关注这个 1:1 的视频教程

【问题讨论】:

    标签: docker docker-compose dockerfile portainer


    【解决方案1】:

    我无法重新创建您的确切错误消息,但在使用您的确切 traefik.yml 配置文件时出现错误(如问题中所述),因为语法无效(如另一个答案中所指出的)。

    我将撰写文件减少到最小:

    version: '3'
    
    services:
      traefik:
        image: "traefik:v2.5"
        container_name: "traefik"
        volumes:
           - 'c:\temp\traefik\etc\traefik.yml:/etc/traefik/traefik.yml'
    

    如您所见,仅将traefik.yml 文件安装到容器中。该文件如下所示,已删除注释掉的行:

      global:
      checkNewVersion: true
      sendAnonymousUsage: false  # true by default
    
     api:
      dashboard: true  # true by default
      insecure: true  # Don't do this in production!
    
    entryPoints:
      web:
        address: :80
    
      websecure:
        address: :443
    

    对此运行docker-compose up 会出现以下错误:

    c:\Temp\traefik>docker-compose up
    [+] Running 1/0
     - Container traefik  Created                                            0.0s
    Attaching to traefik
    traefik  | 2021/12/09 08:44:36 command traefik error: no valid configuration found in file: /etc/traefik/traefik.yml
    traefik exited with code 1
    

    当我修复 traefik.yml 文件中的缩进(并打开调试日志记录)时,我有这个:

    global:
      checkNewVersion: true
      sendAnonymousUsage: false  # true by default
    
    api:
      dashboard: true  # true by default
      insecure: true  # Don't do this in production!
    
    entryPoints:
      web:
        address: :80
    
      websecure:
        address: :443
    
    log:
      level: DEBUG 
    

    再次运行docker-compose up,我现在得到了这个:

    c:\Temp\traefik>docker-compose up
    [+] Running 2/2
     - Network traefik_default  Created                                      0.2s
     - Container traefik        Created                                     29.5s
    Attaching to traefik
    traefik  | time="2021-12-09T08:49:30Z" level=info msg="Configuration loaded from file: /etc/traefik/traefik.yml"
    traefik  | time="2021-12-09T08:49:30Z" level=info msg="Traefik version 2.5.4 built on 2021-11-08T17:41:41Z"
    traefik  | time="2021-12-09T08:49:30Z" level=debug msg="Static configuration loaded {\"global\":{\"checkNewVersion\":true},\"serversTransport\":{\"maxIdleConnsPerHost\":200},\"entryPoints\":{\"traefik\":{\"address\":\":8080\",\"transport\":{\"lifeCycle\":{\"graceTimeOut\":\"10s\"},\"respondingTimeouts\":{\"idleTimeout\":\"3m0s\"}},\"forwardedHeaders\":{},\"http\":{},\"udp\":{\"timeout\":\"3s\"}},\"web\":{\"address\":\":80\",\"transport\":{\"lifeCycle\":{\"graceTimeOut\":\"10s\"},\"respondingTimeouts\":{\"idleTimeout\":\"3m0s\"}},\"forwardedHeaders\":{},\"http\":{},\"udp\":{\"timeout\":\"3s\"}},\"websecure\":{\"address\":\":443\",\"transport\":{\"lifeCycle\":{\"graceTimeOut\":\"10s\"},\"respondingTimeouts\":{\"idleTimeout\":\"3m0s\"}},\"forwardedHeaders\":{},\"http\":{},\"udp\":{\"timeout\":\"3s\"}}},\"providers\":{\"providersThrottleDuration\":\"2s\"},\"api\":{\"insecure\":true,\"dashboard\":true},\"log\":{\"level\":\"DEBUG\",\"format\":\"common\"},\"pilot\":{\"dashboard\":true}}"
    traefik  | time="2021-12-09T08:49:30Z" level=info msg="\nStats collection is disabled.\nHelp us improve Traefik by turning this feature on :)\nMore details on: https://doc.traefik.io/traefik/contributing/data-collection/\n"
    traefik  | time="2021-12-09T08:49:30Z" level=info msg="Starting provider aggregator.ProviderAggregator {}"
    ...
    ...
    

    所以可以看出traefik启动了。这不一定是您遇到的同一个问题,但它显示了如何解决它。一旦您知道您的 traefik 配置良好,您可以添加 DEBUG 日志记录,然后尝试添加其他卷和配置,看看它们是否也可以。

    【讨论】:

    • 试过但没用。
    【解决方案2】:

    你能分享整个终端日志吗?是容器内部的错误吗?

    无论如何,我认为你应该检查你的 traefik.yml 缩进。有一些不同级别的键,YAML 对此非常明智。我特意说的是:

    • 全球
    • checkNewVersion
    • 发送匿名使用情况
    • API

    检查它们前面的空格数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-10
      • 2020-10-10
      • 1970-01-01
      • 2021-08-24
      • 2012-11-19
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多