Search code examples
dockernetwork-programmingdocker-composetraefik

How to set up subdomains with traefik and docker in a local network?


I have a raspberry pi plugged into my home router, running Ubuntu 20.04 and Docker. I gave it a fixed ip and its hostname in the local network is raspy.local. I can access docker containers via raspy.local:<portnumber>.

What I would like to do is to have docker containers be reachable via subdomains, like influxdb.raspy.local or traefik.raspy.local etc. The only solution that worked was to run traefik as a docker container, set Host(`<subdomain>.raspy.local`) rules and edit the /etc/hosts file on my laptop so that the subdomains point to the IP address of the raspberry pi.

This is a bad solution because I have to edit the /etc/hosts file every time I make a change and anyways this cannot be done on all the devices on my network (e.g. I cannot to it on smartphones).

What is the proper way to do it?

(I have found other similar questions here on SO, but I didn't find one with information on how to do this within a local network)


Solution

  • You need to setup a local DNS server:

    1. Set a static IP on your RPi and install PiHole in it.
    2. Set an A record for each of the subdomains you want in the PiHole DNS configuration, pointing to the IP of the device running Traefik (same RPi in your case) (eg: A subdomain.raspy.local -> 192.168.0.xxx)
    3. Set your main router DNS IP Address to the address of your PiHole Server.

    Now every device connected to the router is going to be able to reach the Traefik server using domain names.

    #30daysofstackoverflow