[snippet] Running Traefik with Docker Swarm

The other day, I wanted to try using Traefik in real world scenario where I’ll have a docker instance running and Traefik will do the routing/proxying based on the domain name that I will provide, sadly this was not so easy, so after few hours of searching I found the solution.

You may ask, why it didnt work, it should work with no problem, and I agree so am going to list the problem I faced and the solution I found. To work with swarm, I created three servers and joined them in one swarm network, where I had one manager and two workers, then I used this simple docker-compose.yml file to create the app

But sadly didnt work, after a small search I found out that I need to activate the swarm mode within traefik, either via the command section or via the config file, I chose the command, so the command is now :

now it worked, the proxy will response but the api was unreliable, I checked the labels section and I can confirm it works nice in my local machine (yes, it works on my machine 😉 ), after a small search I found out that the labels sections which is so important for traefik to work and communicate with the other services should be within the deploy section not outside as I use, as a result of my above code, traefik only communicates with the services which are on the same machine, which mean that my request will not always routed correctly, so the new improved code is:

But still the communication was unstable, and sometimes I dont get a response at all, so another search shows that I need to modify the networks section to either:

  1. Create the traefik network manually, and specify the external : true option in my docker-compose.yml file.
  2. Change the value of the traefik.docker.network label to be <service_name>_traefik as am naming my network traefik in the networks section.

So I chose the section option, since am calling my service web I have changed the network to  , check the following code:

so now I issued the following command and everything worked as planned.

Just a small note, remember to add the DNS records for your domain/subdomains to point to all the servers you have created.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.