Skip to content

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.

  1. Create the network dns-net

    Run the following command:

    # Open your terminal application
    sudo docker network create --driver=bridge --subnet=172.19.0.0/16 --gateway=172.19.0.1 dns-net
  2. Create the folders needed by the container

    Run the following commands inside your home folder:

    # Open your terminal application
    cd ~
    mkdir -p pihole/etc-pihole
    mkdir -p pihole/etc-dnsmasq.d
    cd pihole
  3. Create the script needed to run the container

    Save the following script as pihole_run.sh:

    pihole_run.sh
    # To create this script use your text editor application, for example Nano
    docker run -d \
    --hostname=pihole \
    --name=pihole \
    --network=dns-net \
    --ip=172.19.0.4 \
    -p 53:53/tcp \
    -p 53:53/udp \
    -p 67:67/udp \
    -p 81:80/tcp \
    -v $PWD/etc-pihole:/etc/pihole \
    -v $PWD/etc-dnsmasq.d:/etc/dnsmasq.d \
    -e FTLCONF_LOCAL_IPV4=172.19.0.4 \
    -e PIHOLE_DNS_=172.19.0.5#5053 \
    -e WEBPASSWORD=<password> \
    -e TZ="Europe/Amsterdam" \
    --cap-add=NET_ADMIN \
    --dns 127.0.0.1 \
    --restart=unless-stopped \
    pihole/pihole:latest
    # IMPORTANT: Please read the instructions below
    Instructions:
    • Optional Replace docker with podman 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 of 81: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
  4. Run the script to create the container

    Run the following command:

    # Open your terminal application
    sudo sh pihole_run.sh

    The image pihole/pihole is automatically pulled and the container is created.

  5. 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.

Favorites

Comments

    No comments found for this note.

    Join the discussion for this note on Github. Comments appear on this page instantly.

    Copyright 2021- Fiction Becomes Fact