【问题标题】:Traefik Reverse Proxy with Docker使用 Docker 的 Traefik 反向代理
【发布时间】:2018-11-06 19:50:20
【问题描述】:

我让 docker compose 和 nginx 一起运行,配置如下:

version: "3"

services:
  web:
    image: nginx:alpine
    volumes:
     - ./nginx:/etc/nginx/conf.d/rainloop
    ports:
     - "8081:80"

    labels:
     - "traefik.frontend.rule=Host:www.example.com"
     - "traefik.port=8081"

和 docker-compose 中的 traefik 使用以下配置:

version: '3'

services:
  reverse-proxy:
    image: traefik:alpine 
    command: --api --docker 

    ports:
      - "80:80"     # The HTTP port
      - "8080:8080" # The Web UI (enabled by --api)
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
      - ./traefik.toml:/etc/traefik/traefik.toml

traefik.toml 保持基本,看起来像这样

defaultEntryPoints = ["http"]

[entryPoints]
  [entryPoints.http]
  address = ":80"

Web UI 显示以下内容

当调用我的域 www.example.com 时,我遇到了超时。

【问题讨论】:

  • 将 traefik 服务和您的“网络”服务放在同一个网络上。通常,您会将网络 traefik 指定为您自己服务堆栈中的外部网络,并将您希望通过 traefik 访问的服务分配给该网络。省略 traefik 无法访问的支持容器(例如数据库或 memcached)
  • 你能举个例子吗?我尝试的方法也没有奏效。

标签: docker nginx reverse-proxy traefik


【解决方案1】:

在您的 traefik.toml 中添加以下内容

[docker]
endpoint = "unix:///var/run/docker.sock"
watch = true

使用docker network create traefik-net创建网络

使用 traefik 部署

version: '3'
services:
  traefik:
    image: traefik:latest
    command: --api
    ports:
      - 80:80
      - 8080:8080 # Port for the web UI
    networks:
      - traefik-net

使用 nginx 部署

version: '3'
services:
  frontend:
    image: nginx
    networks:
    - traefik-net
    labels:
    - "traefik.docker.network=traefik-net"
    - "traefik.frontend.rule=Host:${DOMAIN}"
    - "traefik.backend=nginx"
    - "traefik.port=80" # you should use exposed port, not published

【讨论】:

【解决方案2】:

您需要将两个容器放在同一个网络上。

  1. 在您的主机内创建一个 docker 网络。 docker network create {network name}.

  2. 在您的 docker-compose 中,使用您创建的现有网络连接两个容器。你可以阅读https://docs.docker.com/compose/networking/#use-a-pre-existing-network了解如何使用它。

  3. 将每个服务添加到上述网络。

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    相关资源
    最近更新 更多