How To Install PiHole Using Docker on Raspberry Pi

Pi-hole or Pihole is a Linux network-level advertisement and Internet tracker blocking application which acts as a DNS sinkhole and optionally a DHCP server, intended for use on a private network.

Prerequisites

This tutorial will require you to prepare a Raspberry Pi with Docker engine installed. You can follow this tutorial to install Docker on Raspberry Pi. We would recommend you to use Raspberry Pi 4 to get a better performance. You can get it from Cytron Marketplace.

Step 1 : Stop systemd-resolved from using port 53 on Raspberry Pi

By default Raspberry Pi OS is listening on port 53. But unfortunately, port 53 is required for PiHole usage. So we need to configure Raspberry Pi OS to disable the use of port 53.

To do that, uncomment line “DNSStubListener” and change it to “no”

sudo nano /etc/systemd/resolved.conf
[Resolve]
#DNS=
#FallbackDNS=
#Domains=
#LLMNR=yes
#MulticastDNS=yes
#DNSSEC=allow-downgrade
#DNSOverTLS=no
#Cache=yes
DNSStubListener=no
#ReadEtcHosts=yes

Restart systemd-resolved to apply the setting

systemctl restart systemd-resolved

Verify port 53 has been free using net-tools

netstat -tunlp | grep 53

If you are not able to use the command, please install net tools by execute apt install net-tools

tcp        0      0 0.0.0.0:5355            0.0.0.0:*               LISTEN      10583/systemd-resol
tcp6       0      0 :::5355                 :::*                    LISTEN      10583/systemd-resol
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           363/avahi-daemon: r
udp        0      0 0.0.0.0:5355            0.0.0.0:*                           10583/systemd-resol
udp6       0      0 :::5353                 :::*                                363/avahi-daemon: r
udp6       0      0 :::5355                 :::*                                10583/systemd-resol

Step 2 : Create Docker Compose file

Create a file name docker-compose.yml and write below details :

sudo nano docker-compose.yml
version: "3.8"
services:
  pihole:
    container_name: pihole
    hostname: pihole.lan
    image: pihole/pihole:latest
    restart: always
    ports:
      - "53:53/tcp"
      - "53:53/udp"
      - "67:67/udp" #This is required if you enable DHCP server on PiHole
      - "8080:80/tcp"

    volumes:
      - './etc-pihole/:/etc/pihole/'
      - './etc-dnsmasq.d/:/etc/dnsmasq.d/'

    environment:
      - TZ= 'Asia/Hong_Kong'
      - WEBPASSWORD= 'password'
      - DNS1=1.1.1.1
      - DNS2=1.0.0.1

     #Recommended but not required (DHCP needs NET_ADMIN)
    cap_add:
      - NET_ADMIN

Step 3 : Run the docker compose file

To spin up the PiHole container, execute this command.

docker-compose up -d

Step 4 : Verify the installation

To verify the installation, access Pi-Hole using this URL : http://server_ip_address:8080/admin

Syafi
Syafi

Hola ! I am Cloud System Engineer and I am very passionate about Linux, Cloud & Automation !

Articles: 4