Paul Knulst told me last time about Traefik and how simple it is to use. So since I every time learn new topics, I will try traefik out.
The actual situation
I running actually an instance of Homeassistant, FHEM for my Home automation, and also Octoprint for remote printing control of my 3d Printer. Fortunately, these Services already running in docker at the moment. These Services are already accessible (internally) by the domain
mediacenter with a given port. Each Service uses a different port, which was simple for me in the initial setup.
I want now to access Homeassistant through the media center DNS-Name the Homeassistant instance. Then after this, I try to map the mediacenter/fhem URL to the FHEM instance. Same for octoprint, this will be reachable through the mediacenter/octoprint path.
So let’s sta… but wait, first of all…:
What is Traefik?
On the traefik Homepage there is this slogan:
Makes Networking Boring
So hopefully I get ready to go service that I can only use instead of deep diving into the configuration. Because in the past I tried to use an Nginx service to get the approach for this, but I got stuck in their configuration “adventure”.
The first step is to setup traefik
So I run a docker-compose environment, so I add the following to my docker-compose
This will install the traefik (v. 2.8). It also will add port 80 to the incoming host. So every request will be awaited through port 80. For my internal use, this is okay that I do not use SSL. There are several more configurations, but this keep is small and simple.
No run the command
docker-compose up -d and not I navigate through http://mediacenter:80 I see …. nothing… okay but there is another port 8080, so I navigate through http://mediacenter:8080. Et voila I see the dashboard from traefik:
So this is now up and running, after this, we must wire altogether. So let’s first wiring homeassistant to the host mediacenter.
My current section for Homeassistant looks like the following
You see, nothing special about this. So how we can get the binding to the Hostname? As in their Documentation, I must only assign a rule with this host binding. In docker-compose, this will be done by setting labels for each instance. So after looking a little bit around, which settings I need, I ended up with the following configuration.
Let me explain these lines, the first lines will enable the service for me. The second line will describe on which target port the route will be sent, so in my case, the internal port from the homeassistant will listen on to 8123, and traefik will redirect each request to this target port. The third line will map the hostname mediacenter to this service. So that each request to the mediacenter pc will be redirected to the homeassistant service. The last line will describe this as a web endpoint.
So let’s have a look now. We fire it up with
docker-compose up -d.
Okay, that was unexpected. So let’s investigate this.
Taskforce Gateway Timeout
First of all, we look at the logs of the traefik container with
docker logs traefik
So it comes up with a 504 error… Gateway timeout… sure it was a networking problem. Initially, I created my own network at the beginning called
homeautomation in which I hung all my services, there is a custom network that is not accessible externally (through a proxy like traefik).
Let’s create a separate external network called
docker network create homeautomation. After this, we must modify the network configuration for homeassistant and traefik to use the new network. Also, we must describe the network too and make it public. My complete docker-compose looks like this
Now you can recreate your docker environment with
docker-compose up -d. and take some time to let traefik gather the new service (it took about 20 seconds) and type the address
http://mediacenterinto the browser.
Nice! It works! Now let's map FHEM.
As we saw before, we must only set some labels here are the labels settings that I use:
Please note that in the setting exists the keyword fhem. This will indicate the service name. This must be unique. Next, I recreate the service again with our lovely command
docker-compose up -d` and after a while of waiting I tried to get access through the address
http://mediacenter/fhem and I awaiting a seamless result, but unfortunately, I saw this:
Yeah, not the result that I want to see. So I must figure out why it will not be displayed correctly.
After a short look in the developer console I saw this result:
Yeah FHEM is special, it will serve the contents only on port 8083 per default. Yes normally traefik will change it for me, but FHEM is only a Perl script that will not accept any other requests as its own defined port. This is a “security feature” maybe. So I must change it to port 80. Refine the port mappings and recreate the docker container.
Important to know! the Fehm script itself must run as root, otherwise, the port 80 cannot be opened
After the recreation, it will look nicer for now!
Nice, now next Octoprint
Do you know the steps? Yes adding labels assigns it into the same network and recreate the images. After that, I checked it with
http://mediacenter/octo and it looks like it will work like a charm.
Yes there are several ways to get a nice routing through the service, but I must have a handy one, for this, I can use traefik, and yes
It makes networking boring
For me and my requirements. So I used the very basic configuration, but this can be many more and it is extensible to get more features and so it will fit your need.