<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: vishal-gandhi</title>
    <description>The latest articles on Forem by vishal-gandhi (@ivishalgandhi).</description>
    <link>https://forem.com/ivishalgandhi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F744465%2F89938d75-31e5-48e5-8958-e636c12fffea.png</url>
      <title>Forem: vishal-gandhi</title>
      <link>https://forem.com/ivishalgandhi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ivishalgandhi"/>
    <language>en</language>
    <item>
      <title>Local Home Lab DNS Setup with DNSMasq and NGINX</title>
      <dc:creator>vishal-gandhi</dc:creator>
      <pubDate>Sun, 10 Apr 2022 09:43:17 +0000</pubDate>
      <link>https://forem.com/ivishalgandhi/local-home-lab-dns-setup-with-dnsmasq-and-nginx-8b5</link>
      <guid>https://forem.com/ivishalgandhi/local-home-lab-dns-setup-with-dnsmasq-and-nginx-8b5</guid>
      <description>&lt;p&gt;As I set up an increased number of FOSS applications, containers(Docker and LXD), and virtual machines built using multipass and LXD in my home lab environment, I started to realize the difficulty in remembering the different ports the applications and containers are running on. One of the solutions to address this problem was to have a Domain Name System for the local network which works for resolving local and external addresses with a reverse proxy to redirect calls based on DNS resolution so that one doesn't have to remember IP addresses and port numbers. &lt;/p&gt;

&lt;p&gt;The below command lists the Docker Containers and ports the container are running on, the requirement is to create a domain for home setup &lt;code&gt;homelab.net&lt;/code&gt; and access the containerized applications hosted using Domain URL e.g. &lt;code&gt;appsmith.homelab.net&lt;/code&gt; &lt;code&gt;excalidraw.homelab.net&lt;/code&gt; &lt;code&gt;typesense.homelab.net&lt;/code&gt; &lt;code&gt;nocodb.homelab.net&lt;/code&gt; etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="c"&gt;# get container names and port numbers&lt;/span&gt;
docker container &lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;--format&lt;/span&gt; &lt;span class="s2"&gt;"table {{.ID}}&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;{{.Names}}&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;{{.Ports}}"&lt;/span&gt; &lt;span class="nt"&gt;-a&lt;/span&gt;


CONTAINER ID   NAMES                        PORTS
cbb2ac402270   appsmith                     0.0.0.0:9001-&amp;gt;9001/tcp, 0.0.0.0:70-&amp;gt;80/tcp, 0.0.0.0:444-&amp;gt;443/tcp
c9875323b989   typesense_typesense-1_1      0.0.0.0:8108-&amp;gt;8108/tcp
c453288c8496   excalidraw                   0.0.0.0:3001-&amp;gt;80/tcp
5be5d33f1f50   k8s-control-plane            127.0.0.1:34589-&amp;gt;6443/tcp
4140d2fbf7d5   mysql_nocodb_1               0.0.0.0:8082-&amp;gt;8080/tcp
e7310461bee9   mysql_root_db_1              3306/tcp, 33060/tcp
9b56c33d45d5   meilisearch_ms_1             0.0.0.0:7700-&amp;gt;7700/tcp
9ac6a0e16b0e   mongo2                       0.0.0.0:20002-&amp;gt;27017/tcp
2aaf01d2233f   mongo1                       0.0.0.0:20001-&amp;gt;27017/tcp
860b521f97dc   mongo3                       0.0.0.0:20003-&amp;gt;27017/tcp
d8ad1ec3cab8   rethinkdb_rethinkdb_1        0.0.0.0:28015-&amp;gt;28015/tcp, 0.0.0.0:29015-&amp;gt;29015/tcp, 0.0.0.0:8081-&amp;gt;8080/tcp

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The containers and applications running on the local home network as shown above do not have a public domain name, the option was to look for setting up a DNS server using DNSMasq and NGINX as a reverse proxy. The containers may not be the only use case scenario for local DNS server, there could be many others like accessing a local file share across devices; accessing applications from a mobile device, sharing a printer &lt;/p&gt;

&lt;p&gt;The options to access container applications before implementing DNSMasq and NGINX &lt;/p&gt;

&lt;p&gt;&lt;code&gt;appsmith&lt;/code&gt; - &lt;a href="http://localhost:70"&gt;http://localhost:70&lt;/a&gt; &lt;br&gt;
&lt;code&gt;excalidraw&lt;/code&gt; - &lt;a href="http://localhost:80"&gt;http://localhost:80&lt;/a&gt;&lt;br&gt;
&lt;code&gt;typesense&lt;/code&gt; - &lt;a href="http://localhost:8080"&gt;http://localhost:8080&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;after implementation of DNSMasq and NGINX&lt;/p&gt;

&lt;p&gt;&lt;code&gt;appsmith&lt;/code&gt; - &lt;a href="http://appsmith.homelab.net"&gt;http://appsmith.homelab.net&lt;/a&gt;&lt;br&gt;
&lt;code&gt;excalidraw&lt;/code&gt; - &lt;a href="http://excalidraw.homelab.net"&gt;http://excalidraw.homelab.net&lt;/a&gt;&lt;br&gt;
&lt;code&gt;typesense&lt;/code&gt; - &lt;a href="http://typesense.homelab.net"&gt;http://typesense.homelab.net&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us get started with the implementation steps for DNSMasq and NGINX. The below steps are performed on Ubuntu 20.04 - Debian-based distro.&lt;/p&gt;

&lt;p&gt;Before starting the installation of DNSMasq, disable systemd-resolve which binds to port 53, the default port for DNSMasq&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl stop systemd-resolved
  &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl disable systemd-resolved
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 1: Install DNSUtils, DNSMasq
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;sudo &lt;/span&gt;apt update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;dnsmasq &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;dnsutils
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Create the DNSMasq configuration file
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    &lt;span class="nv"&gt;dnsmasq_conf&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"no-dhcp-interface=enp2s0f0
    bogus-priv
    domain=homelab.net
    expand-hosts
    local=/homelab.net/
    domain-needed
    no-resolv
    no-poll
    server=8.8.8.8
    server=8.8.4.4"&lt;/span&gt;
    &lt;span class="nb"&gt;sudo echo&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$dnsmasq_conf&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /etc/dnsmasq.d/home-lab.net 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl restart dnsmasq
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Add container DNS records in the file./etc/hosts. The records in hosts file will be used by DNSMasq for client responses
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/hosts  
  &lt;span class="c"&gt;# add the below records to the hosts file&lt;/span&gt;
  &lt;span class="c"&gt;#Container DNS records&lt;/span&gt;
  &lt;span class="c"&gt;# appsmith&lt;/span&gt;
  192.168.20.113 appsmith
  &lt;span class="c"&gt;# excalidraw&lt;/span&gt;
  192.168.20.113 excalidraw
  &lt;span class="c"&gt;# typesense&lt;/span&gt;
  192.168.20.113 typesense
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Restart DNSMasq service
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart dnsmasq.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Install NGINX
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;sudo &lt;/span&gt;apt update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 6: To enable reverse proxy feature, create a new NGINX configuration file in &lt;code&gt;sites-enabled&lt;/code&gt; directory
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;sudo &lt;/span&gt;nano /etc/nginx/sites-enabled/homelab.conf
  server &lt;span class="o"&gt;{&lt;/span&gt;
          listen 80&lt;span class="p"&gt;;&lt;/span&gt;
          listen &lt;span class="o"&gt;[&lt;/span&gt;::]:80&lt;span class="p"&gt;;&lt;/span&gt;
          server_name typesense.homelab.net&lt;span class="p"&gt;;&lt;/span&gt;
          location / &lt;span class="o"&gt;{&lt;/span&gt;
                   proxy_bind 192.168.20.113&lt;span class="p"&gt;;&lt;/span&gt;
                   proxy_pass http://localhost:3000&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  server &lt;span class="o"&gt;{&lt;/span&gt;
          listen 80&lt;span class="p"&gt;;&lt;/span&gt;
          listen &lt;span class="o"&gt;[&lt;/span&gt;::]:80&lt;span class="p"&gt;;&lt;/span&gt;
          server_name appsmith.homelab.net&lt;span class="p"&gt;;&lt;/span&gt;
          location / &lt;span class="o"&gt;{&lt;/span&gt;
                  proxy_bind 192.168.20.113&lt;span class="p"&gt;;&lt;/span&gt;
                  proxy_pass http://localhost:70&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="o"&gt;}&lt;/span&gt;

  server &lt;span class="o"&gt;{&lt;/span&gt;
          listen 80&lt;span class="p"&gt;;&lt;/span&gt;
          listen &lt;span class="o"&gt;[&lt;/span&gt;::]:80&lt;span class="p"&gt;;&lt;/span&gt;
          server_name excalidraw.homelab.net&lt;span class="p"&gt;;&lt;/span&gt;
          location / &lt;span class="o"&gt;{&lt;/span&gt;
                  proxy_bind 192.168.20.113&lt;span class="p"&gt;;&lt;/span&gt;
                  proxy_pass http://localhost:3001&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;proxy_pass&lt;/code&gt; argument will forward all incoming client requests to nocodb.homelab.net to respective app. The IP address and port number can be easily changed. &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7 reload nginx
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl reload nginx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>nginx</category>
      <category>dnsmasq</category>
      <category>reverseproxy</category>
    </item>
  </channel>
</rss>
