I’ve been using Traefik and Docker in my development environment and live servers for a couple of years now.
I have multiple PHP projects running Nginx and PHP-FPM and I can run them all at the same time and access them through Traefik all on port 80 (so on my local environment I can use http://project1.test, http://project2.test etc).
It seems like some time in the last couple of months though I’ve had to expose ports for Nginx in my docker-compose.yml file otherwise Traefik just doesn’t recognise the service.
I never had to expose ports in the past (the Traefik labels on the service seemed to be enough to get Traefik to recognise them) so I’m not sure if Traefik has changed or Docker has changed (both keep being updated)?
I can easily expose ports on my development environment to work around it but I’d rather not have to expose random ports (“81:80”, “82:80” etc.) per Nginx service on the production servers.
Make sure they are in the same Docker network and maybe indicate the internal port to use, in case multiple ports are declared with expose in the Dockerfile.
Thanks, the example works but only as there’s a loadbalancer involved I think.
If I add traefik.http.services.myservicename.loadbalancer.server.port=80 to the services then they appear as HTTP routers and HTTP services. Without the loadbalancer or exposed they don’t work at all.
I’ve realised that by exposing ports on the Docker services this then seems to automatically create a loadbalancer too so it looks like it’s only happy to work with a loadbalancer and if I just have a router then it doesn’t work.
I keep my setup in git and looking back nothing has changed in my setup in the last year or so, it always used to work with just an HTTP router and no load balancers or exposed ports:
Wondered if the default behaviour of Docker or Traefik may have changed but looking at the Docker and Traefik routing guides it does seem like it should work with just an HTTP router
Just tried on a different laptop with exactly the same traefik and service setup. It works without exposing ports but does automatically create the loadbalancer so maybe the loadbalancer is a necessary part of the HTTP router.
So the issue on my primary laptop seems to be it’s not automatically creating the load balancer unless I either add the label for one or expose ports on the service.
The primary laptop is running docker desktop on WSL2 on Windows 11, the other laptop is running Ubuntu so maybe the issue is a Docker/WSL2 issue.
Thanks, yes sorry, my terminology is incorrect. I mean publish rather than expose.
I’ve never needed to publish or expose any HTTP/HTTPS ports before though, other than the ones on the Traefik container itself.
I just used an HTTP router on each Nginx service and Traefik would pick that up and I accessed everything through Traefik on port 80/443. That seems to still work on my Ubuntu laptop but on Windows I need to add a load balancer manually or expose ports which then automatically adds a load balancer.
Going to try later today with a different Windows laptop. Just to see if it’s somewith with new behaviour of Windows/Docker Desktop/WSL2 or just a bug/quirk on this particular laptop.
I installed Docker Desktop on WSL2 on another Windows 11 PC and had exactly the same behaviour. Nginx service doesn’t show up in Traefik unless I either add a load balancer label or publish a port.
I then removed Docker Desktop and install docker-ce directly on WSL2 (added the Docker repository to APT sources on the command line and ran apt-get install docker-ce) and this then reverts back to the orginal behaviour where Nginx shows up in Traefik with just a label for the router.
Looks like it just behaves differently now with Docker Desktop on WSL2, some update to Docker Desktop in the last few months must have changed something.
The good news is I don’t have to publish ports when using Ubuntu so my production setup doesn’t need unecessary ports opening.