Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map specific port inside docker container when using traefik?

Here's my 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

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

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)

like image 561
Desmond Avatar asked Nov 26 '19 15:11

Desmond


People also ask

What ports need to be open for Traefik?

We're publishing the default HTTP ports 80 and 443 on the host, and making sure the container is placed within the web network we've created earlier on. Finally, we're giving this container a static name called traefik .

How do you expose a port on a running Docker container?

There are several ways to do this: you can expose a port via the --expose flag at runtime, or include an EXPOSE instruction in the Dockerfile. You can also publish ports by using the -p or -P flags in the Docker run string.


2 Answers

PathPrefix:/blog

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

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

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.

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"

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

like image 171
Keith Avatar answered Sep 21 '22 20:09

Keith


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" #<-----
like image 30
Facty Avatar answered Sep 22 '22 20:09

Facty