首页 > 解决方案 > 一台主机上有两个带有 Traefik 的 docker 容器

问题描述

我正在尝试在我的树莓派上设置一个服务器来运行 Nextcloud 和 Firefly III,并使用 traefik 作为反向代理。我正在使用 docker-compose 并且 Portainer 在 https 上运行良好(包含在测试代码中),但我根本无法让 Nextcloud 或 Firefly 运行。当我使用 Firefly 的官方文档时,我可以让它运行,当我使用 NextcloudPi 本身时,我也可以让它运行,但我不能让它们同时工作。FF 给出了 403 Forbidden “您无权访问此服务器上的 / ”。Nextcloud 给出“Bad Gateway”错误时出错。我会很感激我能得到的任何帮助,因为我已经阅读了尽可能多的网站,而且我不知道还有什么可做的。

这是我的 docker-compose.yml:

version: "3.2"

services:
  # Reverse Proxy and Let's Encrypt
  traefik:
    container_name: traefik
    image: traefik:alpine
    restart: always
    networks:
      - srv
      - firefly_iii_net
      - proxy-tier
    ports:
      - 80:80
      - 443:443
    volumes:
      - /opt/traefik/traefik.toml:/traefik.toml
      - /var/run/docker.sock:/var/run/docker.sock
      - /opt/traefik/acme.json:/acme.json

  # Portainer
  portainer:
    container_name: portainer
    image: portainer/portainer
    restart: always
    networks:
      - srv
    ports:
      - "9000:9000"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /opt/portainer:/data
    labels:
      - traefik.enable=true
      - "traefik.frontend.rule=Host:port.skdjfgsl.club"

  db:
    image: postgres:alpine
    restart: always
    volumes:
      - db:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=
      - POSTGRES_DB=nextcloud
      - POSTGRES_USER=nextcloud

  app:
    image: nextcloud:fpm-alpine
    restart: always
    volumes:
      - nextcloud:/var/www/html
    environment:
      - POSTGRES_HOST=db
      - POSTGRES_PASSWORD=
      - POSTGRES_DB=nextcloud
      - POSTGRES_USER=nextcloud
    depends_on:
      - db
    networks:
      - proxy-tier
    expose:
      - 80
      - 443
    labels:
      - traefik.enable=true
      - "traefik.frontend.rule=Host:nc.skdjfgsl.club"

  firefly_iii_app: 
    environment: 
      - FF_DB_HOST=firefly_iii_db
      - FF_DB_NAME=firefly
      - FF_DB_USER=firefly
      - FF_DB_PASSWORD=firefly
      - FF_APP_KEY=S0m3R@nd0mStr1ngOf32Ch@rsEx@ctly
      - FF_APP_ENV=local
      - FF_DB_CONNECTION=pgsql
      - TZ=Europe/Amsterdam
      - APP_LOG_LEVEL=debug
      - TRUSTED_PROXIES=**
    image: jc5x/firefly-iii
    links: 
      - firefly_iii_db
    networks: 
      - firefly_iii_net
    volumes: 
      - 
        source: firefly_iii_export
        target: /var/www/firefly-iii/storage/export
        type: volume
      - 
        source: firefly_iii_upload
        target: /var/www/firefly-iii/storage/upload
        type: volume
    expose:
      - 80
      - 443
    labels:
      - traefik.enable=true
      - "traefik.frontend.rule=Host:ff.skdjfgsl.club"
      - traefik.port=443

  firefly_iii_db: 
    environment: 
      - POSTGRES_PASSWORD=firefly
      - POSTGRES_USER=firefly
    image: "postgres:10"
    networks: 
      - firefly_iii_net
    volumes: 
      - "firefly_iii_db:/var/lib/postgresql/data"

networks:
  srv:
  firefly_iii_net: 
    driver: bridge
  proxy-tier:

volumes:
  db:
  nextcloud:
  firefly_iii_db: ~
  firefly_iii_export: ~
  firefly_iii_upload: ~

这是我的 traefik.toml:

defaultEntryPoints = ["http", "https"]

logLevel = "DEBUG"

[entryPoints]
  [entryPoints.http]
    address = ":80"
      [entryPoints.http.redirect]
        entryPoint = "https"
  [entryPoints.https]
    address = ":443"
      [entryPoints.https.tls]

[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "mydomain.com"
exposedByDefault = false
watch = true

[acme]
email = "email@mydomain.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
  [acme.httpChallenge]
  entryPoint = "http"

标签: dockerdocker-composetraefikdocker-container

解决方案


要访问端口 443 (https),​​您需要调整 traefik 使用的协议以与具有以下标签的容器通信:

- traefik.protocol=https

错误的网关可能意味着 traefik 试图连接到错误的端口、错误的 IP 或 traefik 并且容器未部署到公共网络。您可以通过指定端口和网络名称来修复前两个:

- traefik.port=80
- traefik.docker.network=project_proxy-tier

其中 project 是您的撰写项目的名称(用于docker network ls查看网络的外部名称)。


推荐阅读