使用traefik时如何在docker容器内映射特定端口? [英] How to map specific port inside docker container when using traefik?

查看:75
本文介绍了使用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/blog localhost/docs 都是 404找不到页面

What I want is access three different node.js websites via different routes. But these three node.js websites actually expose different ports. Now my treafik is running. I can config via localhost:8080 But localhost localhost/blog and localhost/docs are all 404 page not found

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

P.S: I'm not sure whether port is the issue I should investigate, because changing one node.js service to port 80 doesn't solve the puzzle. And I saw on traefik dashboard the rule is Host(blog-dev)

推荐答案

PathPrefix:/blog

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

When you have this as a routing rule, traefix won't automatically remove the prefix when sending to the container.

因此,除非您的集装箱内有路线/blog ,否则您将获得404.

So unless you have a route /blog inside your container you will get a 404.

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

So what you normally do is also add a middleware to strip this -> https://docs.traefik.io/middlewares/stripprefix/

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

Also you appear not to be setting your rules based on your service.

举例来说,您的第一个服务 blog

So as an example for your first service 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 等.

And then do the same for your other routes, don't forget to replace routers.blog with routers.docs etc..

这篇关于使用traefik时如何在docker容器内映射特定端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆