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

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


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..


Thanks to @Keith I found the solution

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.socknetworks:  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" #<-----