Traefik HTTP services only appear if I expose a port in Docker

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.

Anyone found the same problem lately?

Thanks.

Works for me as usual. Maybe check simple Traefik example.

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.

Hi,

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:

labels:
- traefik.enable=true
- traefik.docker.network=development
- traefik.http.routers.project1-nginx.entrypoints=web
- traefik.http.routers.project1-nginx.rule=Host(`project1.test`)

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 :thinking:

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.

Note the wording. EXPOSE in Dockerfile only tells which port to use. If there are multiple, Traefik uses the first one.

You need to publish a port to make it available outside of the container.

If you have Docker issues, check the Docker forum.

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.

Maybe they updated the nginx container and it "exposes" different ports now?

Just checked and I’m using nginx:stable-alpine on both systems and and re-pulled the images to make sure they’re using the same.

Definitely starting to feel like an issue with Windows/Docker Desktop/WSL2 compared to Ubuntu/Docker :thinking:

I’ll keep digging :slightly_smiling_face:

Update:

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.

Further update (for anyone who comes along later and finds this post):

I installed Docker Desktop on Ubuntu as a test and then encountered the same problem then there too.

The issue seems to be Docker Desktop, regardless of operating system.

Looks to be a known issue for Mac too and upcoming releases might fix it:

Last update:

Docker Desktop v4.44.0 seems to have fixed it for me :slightly_smiling_face:

1 Like