Pi-hole container setup
Introduction
Pi-hole is a network-level ad blocker that acts as a DNS sinkhole, filtering out unwanted advertisements and tracking domains to enhance privacy and improve browsing speed. Using Unbound in combination with Pi-hole allows you to run a recursive DNS server, adding an extra layer of privacy by ensuring DNS queries are resolved locally without relying on third-party DNS providers.
Setup
Below are the steps you can follow for the Linux terminal or NixOS configuration.
To allow Pi-hole to communicate with Unbound, you need to create a Docker network first. You only need to do this if you want to use Unbound as an upstream DNS server. So it is for example also possible to use the Cloudflare DNS server as an upstream DNS server.
-
Create the network
dns-net
Run the following command:
-
Create the folders needed by the container
Run the following commands inside your home folder:
-
Create the script needed to run the container
Save the following script as
pihole_run.sh
:Instructions:
- Optional Replace
docker
withpodman
if needed - Optional The setting
--ip=172.19.0.4
contains the fixed IP address of the container in the range of the dns-net network, you can change this if needed - Optional The setting
-p 67:67/udp
is only required if you are using Pi-hole as your DHCP server - Optional Replace port
81
of81:80/tcp
to whatever port you want to use to to access the web interface - Optional Replace
$PWD/etc-pihole
with your own location if needed. This can be a fileserver - Optional Replace
$PWD/etc-dnsmasq.d
with your own location if needed. This can be a fileserver - Optional The setting
FTLCONF_LOCAL_IPV4=172.19.0.4
contains the fixed IP address of the container in the range of the dns-net network - Optional Replace
172.19.0.5#5053
with the IP address and port of the upstream DNS server, in this case it is the IP address of the Unbound container in the range of the dns-net network. You can also configure this within the web interface - Required Replace
<password>
with your own password to access the web interface - Required Replace
Europe/Amsterdam
with your own timezone - Optional The setting
--cap-add=NET_ADMIN
is only required if you are using Pi-hole as your DHCP server - Optional The setting
--dns 127.0.0.1
is maybe needed so the container can resolve DHCP hostnames from Pi-hole’s DNSMasq, may fix resolution errors on container restart. Here is the link to the documentation
- Optional Replace
-
Run the script to create the container
Run the following command:
The image
pihole/pihole
is automatically pulled and the container is created. -
Check the results
If needed you can check if the container is running properly.
Now you can browse to the Pi-hole web interface by opening a web browser and going to:
http://localhost:81
. Replace localhost with the relevant IP address or FQDN if needed, and adjust the port if you changed it earlier.
-
Add virtualisation to
configuration.nix
Add
virtualisation
and the import to a seperate nix file for the container toconfiguration.nix
: -
Add the macvlan network to
configuration.nix
The container will use a macvlan network (
net_macvlan
) with a dedicated IP address. Add the following toconfiguration.nix
:Instructions:
- Required Replace
192.168.1.1
with your gateway IP address - Required Replace
192.168.1.0
with your subnet - Required Replace
ens18
with the name of own network interface
- Required Replace
-
Add a script to create folders to
configuration.nix
Make sure the folders for use with the container are created by adding the following to
configuration.nix
:Instructions:
- Required Replace
<username>
with your NixOS username
- Required Replace
-
Create the containers folder
Run the following command:
-
Add the container configuration to
pihole.nix
Add the following to
pihole.nix
:Instructions:
-
Required Replace
Europe/Amsterdam
with your own timezone -
Required Replace
<password>
with your password for use with the Pi-hole web interface -
Required Replace
<upstream DNS server>
with the IP address of the upstream DNS server you want to use. In my case this is Unbound -
Required Replace
<IP address>
with the IP address of this container. Make sure it is within the range of the macvlan network -
Optional Replace
pihole.home.arpa
with what your web server ‘virtual host’ is -
Required Replace
<username>
with your NixOS username -
Optional Replace
--pull=newer
with--pull=never
if you do not want the image to be automatically replaced by new versions -
Optional Replace
net_macvlan
with the name of your macvlan network if needed -
Required Replace
<MAC address>
a (randomly generated) MAC address. Otherwise, every time the container is started, a new mac address will be used, which for example will be created as a new device within the Unifi Network Application. Or temporarily disable this option, and add the MAC address that is generated the first time when this container is started. Use inspect to get the MAC address if needed:sudo podman inspect <container name> |grep MacAddress|tr -d ' ,"'|sort -u
-
-
Switch NixOS configuration
Now you can switch to the new configuration within NixOS, the image will be downloaded and the container will be created:
Run the following command:
-
Check the results
Run the following commands to check if the container and Pi-hole is working properly:
Instructions:
- Required Replace
<IP address>
with the IP address of the Pi-hole container
Now you can browse to the Pi-hole web interface by opening a web browser and going to:
http://localhost:81
. Replace localhost with the relevant IP address or FQDN if needed, and adjust the port if you changed it earlier. - Required Replace
No comments found for this note.
Join the discussion for this note on Github. Comments appear on this page instantly.