Run Traefik as a service, but route standalone containers

Hi,

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 node.id==XYZ --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

mosquitto:
    image: eclipse-mosquitto:1.6.12
    networks:
      - shared
    labels:
      - "traefik.enable=true"
      - "traefik.tcp.routers.mytcprouter.rule=HostSNI(`*`)"
      - "traefik.tcp.routers.mytcprouter.entrypoints=mqtt"
      - "traefik.tcp.routers.mytcprouter.service=mosquitto"
      - "traefik.tcp.services.mosquitto.loadbalancer.server.port=1883"

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 172.24.0.2:1883: 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 --providers.docker.network=test, 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.

Hey,

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 --providers.docker.network=test though. I hope this is solid enough on the long term. thanks