【发布时间】: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