使用traefik时如何在docker容器内映射特定端口?

这是我的docker-compose.yml

version: '3'
services:
  website:
    build: ./website
    expose: [3000]
    labels:
      - "traefik.frontend.rule=Host:localhost"
  blog:
    build: ./blog
    expose: [4000]
    labels:
      - "traefik.frontend.rule=Host:localhost;PathPrefix:/blog"
  docs:
    build: ./docs
    expose: [3000]
    labels:
      - "traefik.frontend.rule=Host:localhost;PathPrefix:/docs"
  proxy:
    image: traefik
    command: --api.insecure=true --providers.docker
    networks:
      - webgateway
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

networks:
  webgateway:
    driver: bridge

我想要通过不同的途径访问三个不同的node.js网站。但是这三个node.js网站实际上公开了不同的端口。现在我的Treafik正在运行。我可以通过localhost:8080进行配置,但是localhost localhost/bloglocalhost/docs都是404 page not found

PS :我不确定是否应该研究端口问题,因为将一个node.js服务更改为port 80不能解决难题。我在traefik仪表板上看到了规则是 Host(blog-dev

i530151196 回答:使用traefik时如何在docker容器内映射特定端口?

PathPrefix:/blog

将其作为路由规则时,traefix在发送到容器时不会自动删除前缀。

因此,除非您的集装箱内有路线/blog,否则您会得到404。

所以您通常要做的就是添加一个中间件来剥离它-> https://docs.traefik.io/middlewares/stripprefix/

您似乎也没有根据服务设置规则。

以您的第一个服务blog为例,

try->

labels:
    - "traefik.http.routers.blog.rule=Host(`localhost`) && PathPrefix(`/blog`)"
    - "traefik.http.routers.blog.middlewares=strip-blog"
    - "traefik.http.middlewares.strip-blog.stripprefix.prefixes=/blog"

然后对其他路线执行相同操作,请不要忘记将routers.blog替换为routers.docs等。

,

感谢@Keith我找到了解决方法

version: '3'
services:
  website:
    build: ./website
    expose: [3000]
    networks: # It's essential to specify the same network in every service
      - webgateway
    labels:
      - "traefik.http.routers.website.rule=Host(`localhost`)" # Use the right format
      - "traefik.port=3000" # Let traefik find the right port
  blog:
    build: ./blog
    expose: [4000]
    networks:
      - webgateway
    labels:
      - "traefik.http.routers.blog.rule=Host(`localhost`) && PathPrefix(`/blog`)" # blog has a root as `/blog` so no need to strip otherwise too many redirects
      - "traefik.port=4000"
  docs:
    build: ./docs
    expose: [3000]
    networks:
      - webgateway
    labels:
      - "traefik.http.routers.docs.rule=Host(`localhost`) && PathPrefix(`/docs`)"
      - "traefik.http.routers.docs.middlewares=strip-docs" # Necessary as Keith mentioned
      - "traefik.http.middlewares.strip-docs.stripprefix.prefixes=/docs"
      - "traefik.port=3000"
  proxy:
    image: traefik
    command: --api.insecure=true --providers.docker
    networks:
      - webgateway
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

networks:
  webgateway:
    driver: bridge
,
labels: 
- traefik.http.services.<YOUR-SERVICE-NAME>.loadbalancer.server.port=9763

EG:

services:
  wso:
    image: "my-custom-wso-image"
    volumes:
      - .....
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.wso.tls=true"
      - "traefik.http.routers.wso.rule=Host(`my.nice.url`)"
      - "traefik.http.services.wso.loadbalancer.server.port=9763" #<-----
本文链接:https://www.f2er.com/3027801.html

大家都在问