【问题标题】:Docker-compose networks with Traefik使用 Traefik 构建网络
【发布时间】:2018-10-18 12:34:13
【问题描述】:

我正在尝试使用 docker-compose 和 Traefik 部署三个服务:

version: '3.5'

services:

  reverse-proxy:
    image: traefik
    command: --web --docker --logLevel=INFO
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    labels:
      - "traefik.frontend.rule=Host:traefik.localhost"
      - "traefik.port=8080"

  db:
    build: ./db
    environment:
      - MONGO_INITDB_DATABASE=example
    volumes:
      - ./volumes/db:/data/db
    restart: always
    labels:
      - "traefik.enable=false"

  api:
    build: ./api
    environment:
      - DATABASE_CLIENT=mongo
      - DATABASE_HOST=db
      - DATABASE_PORT=27017
      - DATABASE_NAME=example
      - HOST=localhost
    expose:
      - "1337"
    depends_on:
      - db
    restart: always
    labels:
      - "traefik.frontend.rule=Host:api.localhost"

  website:
    build: ./app
    labels:
      - "traefik.frontend.rule=Host:web.localhost"

最后一个容器 (website) 是使用 create-react-app 构建的静态网站,它使用 API (api):

let response = await fetch(`http://api:1337/news`);

问题是该网站无法解析主机http://api:1337(即通过容器名称),但如果我将该行更改为:

let response = await fetch(`http://<container-ip>:1337/news`);

一切都很完美。我试图设置一个网络,它也不起作用。有什么帮助吗?谢谢!

【问题讨论】:

    标签: node.js docker docker-compose traefik


    【解决方案1】:

    您只能从其他容器访问具有名称的容器。 如果您将反应代码更改为:

    let response = await fetch('http://localhost:8080/news', {
      headers: 'Host': 'api.localhost'
    })
    

    您应该能够访问您的容器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      相关资源
      最近更新 更多