Automating Pi-hole updates with addlist customization on Windows with Docker

By Pwigle on woensdag 22 december 2021 15:54 - Comments (8)
Category: -, Views: 5.007

For those that never heard of Pi-hole, it's worth looking into! Instead of browser plugins or other software on each computer, install Pi-hole in one place and your entire network is protected from adds. It's like an SSD for the internet.

In my environment I have a Intel NUC running Windows 10 for web camera recording purposes and decided it could easily run this Pi-hole task next to it.However - I don't want to be in the business of running manual updates and looked into a way to completely automate the update process of Pi-hole including customization of add block-lists - here's what I did.

Pre-reqs:
  • Download Docker for Windows and install
  • Create a new batch file, e.g. “update_pihole.bat” with the content shown below
  • Change the docker run parameters, e.g. change the server IP/password/TimeZone/DNS2 and DNS3 (leave DNS1 to the default 127.17.0.1). Check out line 14 in the script below.
  • Create a scheduled task on the Windows host that runs the script periodically, e.g. every month. Test the task to see if it’s working.
  • Make sure the host can resolve DNS to download the latest pi-hole container after the script stops the running pi-hole docker container. I’ve configured a secondary DNS on my Windows host to point at my ISP’s DNS server directly.
  • Change your router’s DHCP to hand out IP’s with the DNS pointing to your Windows host/pi-hole’s IP.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@echo off
REM Update a pi-hole container on Windows using Docker and adding a bunch of custom addlists 

REM Stop and delete existing pi-hole container
docker stop pihole
docker rm pihole

REM Clear out the Docker cache and all unused images (i.e. the out of date version of pi-hole).
docker image prune -a --force

REM Pull latest pihole container, make sure your host can resolve DNS at this point!
docker pull pihole/pihole

REM Run pihole with customized parameters (change this to match your environment)
docker run -d --name pihole -e [b]ServerIP=192.168.1.5 -e WEBPASSWORD=abcdefgh -e TZ=Europe/Amsterdam[/b] -e DNS1=127.17.0.1 -e [b]DNS2=8.8.8.8 -e DNS3=1.1.1.1[/b] -p 80:80 -p 53:53/tcp -p 53:53/udp -p 443:443 --restart=unless-stopped pihole/pihole:latest

REM Add custom addlist by modifying the database (ugly but only method AFAIK...)
docker exec pihole sqlite3 /etc/pihole/gravity.db  "INSERT INTO adlist (address, enabled, comment) VALUES ('https://dbl.oisd.nl/', 1, 'comment');"
docker exec pihole sqlite3 /etc/pihole/gravity.db  "INSERT INTO adlist (address, enabled, comment) VALUES ('https://raw.githubusercontent.com/PolishFiltersTeam/KADhosts/master/KADhosts.txt', 1, 'comment');"

REM Update Gravity to download contents of custom addlists
docker exec pihole pihole updateGravity


Every time you run the batch file, the latest version of Pi-hole will be downloaded and customized with add lists.

This is my first post on tweakblogs, consider leaving a reply if you found this to be helpful