Run Traefik as a service, but route standalone containers


in Docker Swarm, I'd like to launch Traefik as a service, with replicas=1, but not running in swarmMode, so that I could then route all the standalone containers (non-Swarm) running on the cluster nodes.

I've tried the following setup but it isn't working:
docker service create --constraint --hostname traefik --mount type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock --network shared --name traefik traefik:2.3.5 --entrypoints.mqtt.address=:1883 --providers.docker=true --providers.docker.exposedbydefault=false

and then, on node XYZ, I'm running
docker-compose up, where docker-compose.yml (snippet) is

    image: eclipse-mosquitto:1.6.12
      - shared
      - "traefik.enable=true"
      - "traefik.tcp.routers.mytcprouter.rule=HostSNI(`*`)"
      - "traefik.tcp.routers.mytcprouter.entrypoints=mqtt"
      - "traefik.tcp.routers.mytcprouter.service=mosquitto"
      - ""

when the mosquitto container is up, I can ping "traefik" from inside, however, the MQTT routing is not working, cause when I try mosquitto_pub -h shared -t test -m "test msg", the command will hang and timeout, and on the "traefik" side, I'll see the error:

time="2021-02-24T14:52:38Z" level=error msg="Error while connection to backend: dial tcp connect: connection timed out"

any ideas? Is this possible at all? It works well if "traefik" is a container and not a service.

P.S. If I pass, it seems to work. But please let me know whether this setup is problematic or not

Hi :wave:
Is there any specific reason why you are not running everything in Swarm? Based on my personal experience the less complex setup you have the easier maintenance and troubleshooting you will have.


the reason is because I need to route standalone container that will be running in the Swarm nodes, and with Traefik in swarmMode, Traefik will only lookup services, not containers

My solution to this is using a common overlay network that is attachable. Traefik is in swarm. The standalone container is connected to this network. A dynamic file provider is used and backend server url is the container-name.

The best thing I can say about it is that it works. The endgame is to move it in to swarm.

1 Like

yep this is my current solution as well. I needed to add though. I hope this is solid enough on the long term. thanks