Rule for one container forwards to another container

Hello,

I have a container running (e.g., service) and labels defined in the docker-compose file. I wanted to run a clone of this under another subdomain (e.g., service-prod). When I do this, accessing service actually goes to service-prod. In the dashboard everything is looking fine. Is this a bug or what could cause this?

Definition of docker provider:

providers:
  docker:
    exposedByDefault: false

Labels of service:

labels:
      - "traefik.http.middlewares.add-auth.basicauth.users=<snip>"
      - "traefik.enable=true"
      - "traefik.http.routers.foo.rule=Host(`foo-dev.example.com`)"
      - "traefik.http.routers.foo.tls=true"
      - "traefik.http.routers.foo.tls.options=mintls12@file"
      - "traefik.http.routers.foo.tls.certresolver=letsencrypt"
      - "traefik.http.routers.foo.middlewares=add-auth@docker"
      - "traefik.http.services.foo.loadbalancer.server.port=8000"

Labels of service-prod:

labels:
      - "traefik.http.middlewares.add-auth-prod.basicauth.users=<snip>"
      - "traefik.enable=true"
      - "traefik.http.routers.foo-prod.rule=Host(`foo.example.com`)"
      - "traefik.http.routers.foo-prod.tls=true"
      - "traefik.http.routers.foo-prod.tls.options=mintls12@file"
      - "traefik.http.routers.foo-prod.tls.certresolver=letsencrypt"
      - "traefik.http.routers.foo-prod.middlewares=add-auth-prod@docker"
      - "traefik.http.services.foo-prod.loadbalancer.server.port=80"

This sounds very odd.

What happens, if you config just one service and change the URL and traefik route name using an environment variable?

labels:
      - "traefik.http.middlewares.add-auth.basicauth.users=<snip>"
      - "traefik.enable=true"
      - "traefik.http.routers.${SUBDOMAIN?Variable not set}.rule=Host(`${SUBDOMAIN?Variable not set}.example.com`)"
      - "traefik.http.routers.${SUBDOMAIN?Variable not set}.tls=true"
      - "traefik.http.routers.${SUBDOMAIN?Variable not set}.tls.options=mintls12@file"
      - "traefik.http.routers.${SUBDOMAIN?Variable not set}.tls.certresolver=letsencrypt"
      - "traefik.http.routers.${SUBDOMAIN?Variable not set}.middlewares=add-auth@docker"
      - "traefik.http.services.${SUBDOMAIN?Variable not set}.loadbalancer.server.port=8000"

I think you can share middleware, at least I do, so I didn't both to change the add-auth line. Also, I simplified it to use just one environment variable.

So then, for production

export SUBDOMAIN=foo
docker-compose up -d

Go to http://foo.example.com

For dev

export SUBDOMAIN=foo-dev
docker-compose up -d

Go to http://foo-dev.example.com

Thanks for your reply. That's a neat idea, thanks for sharing that.

It actually turned out to be that both were in the same network (I have a user-defined bridge network) and the share the same service name in the docker-compose file (such as db, app etc.) and that seems to "overwrite" the first one.

Not sure if there is any way to prevent that. If anyone has a suggestion on how to run traefik where I don't have to specify the same network for all services please let me know.