为什么在 docker 上创建堆栈时出现此错误?

Why am I getting this error when creating a stack on docker?

我是个彻头彻尾的 docker 菜鸟。刚刚按照教程安装了 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 command traefik error: yaml: line 19: did not find expected key

当我从 docker-compose 文件中删除“服务”下的整个“卷”部分时,我可以将容器放入 运行,但我需要它来设置 traefik。我不知道我做错了什么,因为我正在观看此 1: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 文件中的缩进(并打开 DEBUG 日志记录)时,我有这个:

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 日志记录,然后尝试添加其他卷和配置,看看它们是否也正常。

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

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

  • 全球
  • 检查新版本
  • sendAnonymousUsage
  • api

检查它们之前的空格数。