Port forwarding rules with Traefik and Docker.Compose
-
I'm trying to use Traefik to route to a number of services all running using docker-compose. What I'd like to do is access each service with a prefix, eg: 10.20.30.40/u10/xyz and 10.20.30.40/a10/abc instead of 10.20.30.40:8000/xyz and 10.20.30.40:8001/abc.
I have a `docker-compose.yml file where I'm starting my services a10,u10,x10, mongo,mosquitto and all are exposed on their own ports - this works. I'm now stuck with the correct parameters for Traefik. You can see some of the attempts and commenting out of parameters.
The services a10,u10 etc should only be accessible using a prefix and all over port 80. Load balancing should be possible, eg:
docker-compose -f dc.yml --scale u10=3 --scale a10=5
When I run this if I try on my local machine: http://127.0.0.1/u10/somepage.html I get a gateway timeout error, or, if I don't try using --scale then I'll get a 404.
Internally these services communicate with each other over the specified ports (REST) without the prefix and this works internally.
version: "3" services: reverse-proxy: image: traefik:v2.9 command: - "--log.level=DEBUG" - "--api.insecure=true" - "--providers.docker=true" - "--providers.docker.exposedByDefault=true" - "--entrypoints.web.address=:80" - "--accesslog=true" ports: - "80:80" - "8080:8080" volumes: - /var/run/docker.sock:/var/run/docker.sock
u10:
image: "u10"container_name: u10
ports:
- 8540:8540 # !!!
depends_on: - messagebus restart: always networks: - attestationnetwork volumes: - ./a10.conf:/etc/a10.conf # !!! labels: - "traefik.enable=true"
- "traefik.port=8540"
- "traefik.docker.network=my-internal-network" - "traefik.http.routers.u10.rule=PathPrefix(`/u10`)" - "traefik.http.routers.u10.entrypoints=web" - "traefik.http.routers.u10.middlewares=u10" - "traefik.http.middlewares.u10.stripprefix.prefixes=/u10" - "traefik.http.middlewares.u10.stripprefix.forceSlash=false" - "traefik.http.routers.u10.service=u10" - "traefik.http.services.u10.loadbalancer.server.port=8540"
networks:
my-internal-network:
driver: bridge
-
I can't tell exactly what you are trying, it looks like you might just not have Traefik and the services on the same network.
Its also helpful to have a domain that's going to resolve to 127.0.0.1 on wildcard queries.
This might be a valuable starting point as a working example.
networks: traefik: name: demo_traefik
services:
proxy:
image: traefik:latest
command: >
--accesslog=true
--api.insecure=true
--log.level=DEBUG
--providers.docker=true
--providers.docker.network=demo_traefik
ports:
- "80:80"
- "8080:8080"
networks:
- traefik
volumes:
- /var/run/docker.sock:/var/run/docker.sock
labels:
traefik.http.routers.demo_traefik.rule: Host(traefik.localtest.me
)
traefik.http.services.demo_traefik.loadbalancer.server.port: 8080example:
image: nginx
networks:
- traefik
scale: 4
labels:
traefik.http.routers.demo_nginx.rule: Host(example.localtest.me
)
traefik.http.services.demo_nginx.loadbalancer.server.port: 80whoami:
image: traefik/whoami
networks:
- traefik
labels:
traefik.http.middlewares.whoami_strip.stripprefix.prefixes: /whoami
traefik.http.routers.whoami.rule: Host(example.localtest.me
) && PathPrefix(/whoami
)
traefik.http.routers.whoami.middlewares: whoami_strip
docker compose up
and then visit http://example.localtest.me/whoami in your browser.