Docker的Traefik反向代理

时间:2018-11-06 19:50:20

标签: docker nginx reverse-proxy traefik

我让docker用以下配置运行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"

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

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显示以下内容

enter image description here

致电我的域名www.example.com时,我超时了。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:-1)

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

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

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

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