Label definitions for handling container which exposes multiple ports

Upgrading my current v1 setup to v2, struggling with what should be a simple use case.

I have a container that exposes 2 ports, 2202 for main access and 2203 for admin access. With v1, defining multiple frontends and backends worked as expected but I just can't figure out the v2 way to accomplish this.

Defining multiple routers and services works fine but as soon as I try to link each router and service unexpected things happen such as the http routers disappearing from the dashboard and when defining multiple rules only the final one is seen.

Appreciate I'm missing something fundamental but any pointers on what I need to add to the docker-compose definition below to enable both mycont.mydomain.com pointing to port 2202 and mycont2.mydomain.com pointing to port 2203 would be great.

version: "3"

networks:
  proxy:
    external: true

services:                                                                                                                                              │
  mycont:                                                                                                                                              │
    image: mycont                                                                                                                                      
    container_name: mycont                                                                                                                             
    restart: unless-stopped                                                                                                                            
    networks:                                                                                                                                          
      - proxy                                                                                                                                          
    ports:                                                                                                                                             
      - 2202:2202                                                                                                                                      
      - 2203:2203                                                                                                                                      
    labels:                                                                                                                                            
      - traefik.enable=true                                                                                                                            
      - traefik.http.routers.mycont.entrypoints=http                                                                                                   
      - traefik.http.routers.mycont.rule=Host(`mycont.mydomain.com`)                                                                                   
      - traefik.http.routers.mycont.middlewares=https-redirect@file                                                                                    
      - traefik.http.routers.mycont-secure.entrypoints=https                                                                                           
      - traefik.http.routers.mycont-secure.rule=Host(`mycont.mydomain.com`)                                                                            
      - traefik.http.routers.mycont-secure.tls=true                                                                                                    
      - traefik.http.routers.mycont-secure.tls.certresolver=http                                                                                       
      - traefik.http.routers.mycont-secure.service=mycont                                                                                              
      - traefik.http.routers.mycont-secure.middlewares=secured@file                                                                                    
      - traefik.http.services.mycont.loadbalancer.server.port=2202                                                                                     
      - traefik.docker.network=proxy                                          

Hello,

just create a service for port 2203 and define a router with your rule.

version: "3"

networks:
  proxy:
    external: true

services:
  mycont:
    image: mycont
    container_name: mycont
    restart: unless-stopped
    networks:
      - proxy
    ports:
      - 2202:2202
      - 2203:2203
    labels:
      - traefik.enable=true
      - traefik.http.routers.mycont.rule=HostRegexp(`{any:[a-z0-9]+}.mydomain.com`,)
      - traefik.http.routers.mycont.entrypoints=http
      - traefik.http.routers.mycont.middlewares=https-redirect@file

      - traefik.http.routers.mycont-secure.rule=Host(`mycont.mydomain.com`)
      - traefik.http.routers.mycont-secure.entrypoints=https
      - traefik.http.routers.mycont-secure.tls.certresolver=http
      - traefik.http.routers.mycont-secure.service=mycont
      - traefik.http.routers.mycont-secure.middlewares=secured@file
      
      - traefik.http.services.mycont.loadbalancer.server.port=2202

      - traefik.http.routers.mycont2-secure.rule=Host(`mycont2.mydomain.com`)
      - traefik.http.routers.mycont2-secure.entrypoints=https
      - traefik.http.routers.mycont2-secure.tls.certresolver=http
      - traefik.http.routers.mycont2-secure.service=mycont2
      - traefik.http.routers.mycont2-secure.middlewares=secured@file
      
      - traefik.http.services.mycont2.loadbalancer.server.port=2203

      - traefik.docker.network=proxy

Many thanks, that got me a bit further, was trying something similar earlier but had some issues.

One thing I'm seeing and don't understand is when I add mycont2 service I lose the mycont router. If I don't configure "traefik.http.services.mycont2.loadbalancer.server.port=2203" I see all three routers (with 1 error) but as soon as I configure it I only see two and lose http to https redirection.

With the original config I can see two routers:

  • mycont@docker with endpoint http and rule Host(mycont.mydomain.com)
  • mycont-secure@docker with endpoint https and rule Host(mycont.mydomain.com)

With new config and no loadbalancer.server.port defined for mycont2 I see three routers:

  • mycont@docker with endpoint http and rule HostRegexp({any:[a-z0-9]+}.mydomain.com,)
  • mycont-secure@docker with endpoint https and rule Host(mycont.mydomain.com)
  • mycont2-secure@docker with endpoint https and rule Host(mycont2.mydomain.com) which errors as mycont2@docker service doesn't exist.

As soon as I configure "traefik.http.services.mycont2.loadbalancer.server.port=2203" I lose the error but also the mycont@docker router and http to https redirect stops working.

  • mycont-secure@docker with endpoint https and rule Host(mycont.mydomain.com)
  • mycont2-secure@docker with endpoint https and rule Host(mycont2.mydomain.com)

yes I miss something:

version: "3"

networks:
  proxy:
    external: true

services:
  mycont:
    image: mycont
    container_name: mycont
    restart: unless-stopped
    networks:
      - proxy
    ports:
      - 2202:2202
      - 2203:2203
    labels:
      - traefik.enable=true
      - traefik.http.routers.mycont.rule=HostRegexp(`{any:[a-z0-9]+}.mydomain.com`,)
      - traefik.http.routers.mycont.entrypoints=http
      - traefik.http.routers.mycont.middlewares=https-redirect@file
      - traefik.http.routers.mycont.service=mycont # it will never called, but needed

      - traefik.http.routers.mycont-secure.rule=Host(`mycont.mydomain.com`)
      - traefik.http.routers.mycont-secure.entrypoints=https
      - traefik.http.routers.mycont-secure.tls.certresolver=http
      - traefik.http.routers.mycont-secure.service=mycont
      - traefik.http.routers.mycont-secure.middlewares=secured@file
      
      - traefik.http.services.mycont.loadbalancer.server.port=2202

      - traefik.http.routers.mycont2-secure.rule=Host(`mycont2.mydomain.com`)
      - traefik.http.routers.mycont2-secure.entrypoints=https
      - traefik.http.routers.mycont2-secure.tls.certresolver=http
      - traefik.http.routers.mycont2-secure.service=mycont2
      - traefik.http.routers.mycont2-secure.middlewares=secured@file
      
      - traefik.http.services.mycont2.loadbalancer.server.port=2203

      - traefik.docker.network=proxy
1 Like

Thanks again, everything is working 100% now. Still getting to grips with v2, can be confusing when something that was working disappears.