Dashmage's Blog

Pi-Hole with Docker

Pi-hole is a service which allows you to block ads on your entire network. That includes your smart TV where you usually wouldn't be able to install an ad-blocker. This technical wizardry happens because we are essentially running a DNS sinkhole server. Pi-hole can do a couple of other nifty tricks but I'll just be focussing on setting up the ad-blocking capabilities for now. You could also pair this with a VPN (in case you want access from outside your home network) and even run it as your local DHCP server instead of your router.

This post will go over how to setup Pi-hole on a Raspberry Pi. Pi-hole will be running from a Docker container and managed using Docker Compose.

Initial Setup

sudo apt update && sudo apt upgrade -y
# set country for date/ time
sudo raspi-config

# install preferred editor
sudo apt install vim

Install Docker and Docker Compose

Run the commands below to install Docker and Docker Compose.

# install docker 
curl -sSL https://get.docker.com | sh
# add user to docker group
sudo usermod -aG docker ${USER}

# install docker-compose
sudo apt install libffi-dev libssl-dev
sudo apt install python3-dev
sudo apt install -y python3 python3-pip
sudo pip3 install docker-compose

# start docker containers on boot
sudo systemctl enable docker

Setting up Pi-hole

version: "3"

services:
  pihole:
    container_name: pihole
    image: jacklul/pihole:latest
    hostname: pihole-docker
    ports:
      - "53:53/tcp"
      - "53:53/udp"
      - "67:67/udp" # Only required if you are using Pi-hole as your DHCP server
      - "80:80/tcp"
    environment:
      TZ: 'yourtimezone'
      WEBPASSWORD: supersecretpassword
      PIHOLE_DNS_: 1.1.1.1;1.0.0.1
    volumes:
      - './etc-pihole:/etc/pihole'
      - './etc-dnsmasq.d:/etc/dnsmasq.d'
      - './etc-pihole-updatelists/:/etc/pihole-updatelists/'
    cap_add:
      - NET_ADMIN
    restart: unless-stopped

Navigate to the web UI for Pi-hole which would be for example 192.168.1.2/admin assuming 192.168.1.2 was the IP assigned to the Pi. Hopefully you are able to see the admin login page where you can login with the password provided in the docker compose file.

.
.
ADLISTS_URL="https://v.firebog.net/hosts/lists.php?type=tick"
.
.
WHITELIST_URL="https://raw.githubusercontent.com/anudeepND/whitelist/master/domains/whitelist.txt"
.
.
# Open an interactive shell on the pihole container
docker exec -it pihole bash

# Now in the container shell, run
sed -e '/pihole updateGravity/ s/^#*/#/' -i /etc/cron.d/pihole
# This will comment out the line in /etc/cron.d/pihole 
# that runs the pihole updateGravity command

Possible Issues


  1. DHCP or Dynamic Host Configuration Protocol describes the process by which the router dynamically assigns IP addresses to all the devices being connected on your home network. You can read more about it here.

#project #self-host