I have an app on two containers which should be served with the same domain for both development and production environments from the same docker host and traefik as a reverse proxy.
My traefik docker-compose is:
version: "3.9"
services:
traefik:
image: "traefik:v2.9"
container_name: "traefik"
command:
- "--api.insecure=true" # Enables traefik dashboard that will listen on port 8080
- "--providers.docker=true" # Defines docker as a provider
- "--providers.docker.exposedbydefault=false" # Prevents from exposing all containers by default
- "--entrypoints.web.address=:80" # Will listen to incoming requests on port 80
ports:
- "80:80"
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock:ro"
networks:
- traefik_webgateway
networks:
traefik_webgateway:
external: true
name: traefik_webgateway
I have created two nginx services:
nginx-1:
version: '3.9'
services:
nginx:
container_name: nginx1
image: nginx
restart: always
networks:
- traefik_webgateway
volumes:
- "./html:/usr/share/nginx/html"
labels:
- traefik.enable=true
- traefik.port=80
- traefik.http.routers.nginx-0.rule=Host(`nginx1.ngtech.home`)
- traefik.http.routers.nginx-0.entrypoints=web
- traefik.http.routers.nginx-0.priority=100
networks:
traefik_webgateway:
external: true
name: traefik_webgateway
nginx-dev:
version: '3.9'
services:
nginx:
container_name: nginxdev
image: nginx
restart: always
networks:
- traefik_webgateway
volumes:
- "./html:/usr/share/nginx/html"
labels:
- traefik.enable=true
- traefik.port=80
- "traefik.http.routers.nginx99-dev.rule=ClientIP(`192.168.120.80/32`) && Host(`nginx1.ngtech.home`)"
- traefik.http.routers.nginx99-dev.entrypoints=web
- traefik.http.routers.nginx99-dev.priority=1
networks:
traefik_webgateway:
external: true
name: traefik_webgateway
And the requests always fall on the nginx-1 and never on the nginx-dev despite to the fact the I created a specific rule for this a specific source address.
The only way I managed to route the traffic from 192.168.120.80 to the dev environment was when I have used two ClientIP rules each per container and each ClientIP range was not overlapping each other.