how can i map a subdomain to a container port using traefik 2 and docker-compose

Viewed 810

I have 2 subdomains, let's call them foo.example.com and bar.example.com. I am using docker-compose to spin up services foo and bar. I would like to map each service (container foo and bar) to the subdomains using traefik 2.x. For example, visiting foo.example.com on port 80 will forward to container foo listening on 8100. Bar follows a similar pattern, bar.example.com will trigger container bar via port 8200. Here is some of my code so far, but I'm struggling to connect these dots.

service:
  foo:
    networks:
      - default
    ports:
      8100:80
  bar:
    networks:
      - default
      - bar
    ports:
      8200:80

I'm using the following labels for foo:

labels:
  - "traefik.enable=true"
  - "traefik.http.services.foo.loadbalancer.server.port=8100"
  - "traefik.docker.network=traefik"
  - "traefik.http.routers.foo.rule=Host(`foo.example.com`)"

and here are the labels for bar:

- "traefik.enable=true"
- "traefik.http.middlewares.bar-bazboo.redirectregex.permanent=true"
- "traefik.http.middlewares.bar-bazboo.redirectregex.regex=^https://(.*)/.well-known/(baz|fizz)boo"
- "traefik.http.middlewares.bar-bazboo.redirectregex.replacement=https://$${1}/remote.php/boo/"
- "traefik.http.middlewares.bar-https.redirectscheme.scheme=https"
- "traefik.http.routers.bar-http.entrypoints=web"
- "traefik.http.routers.bar-http.rule=Host(`bar.example.com`)"
- "traefik.http.routers.bar-http.middlewares=bar-https@docker"
- "traefik.http.routers.bar.entrypoints=web-secure"
- "traefik.http.routers.bar.rule=Host(`bar.example.com`)"
- "traefik.http.routers.bar.middlewares=bar-bazboo@docker"
- "traefik.http.routers.bar.tls=true"
- "traefik.http.routers.bar.tls.certresolver=le"
- "com.centurylinklabs.watchtower.enable=true"

and networks:

version: "3"

networks:
  default:
    driver: bridge
  traefik:
    internal: true
  bar:

EDIT: bar currently doesn't have any port mappings for debugging, and network bar is exclusively for 2 and only 2 services.

I can help with providing additional information. Thanks.

0 Answers
Related