User Agents

Web browsers and everyday web tech, explained

Docker Compose Pi-hole Setup: DNS Fortress?

· 6 min read · How the Web Works

In this article
  1. Why Traditional Pi-hole Setups Fail
  2. What Docker Compose Actually Changes
  3. How to Set It Up Properly
  4. Port 53 and Systemd-Resolved Conflicts
  5. Why It's More Robust Than Bare-Metal
  6. Common Misconception: Docker Slows DNS
  7. Alternatives and When Not to Use Docker
  8. Frequently Asked Questions

Yes, running Pi-hole in Docker Compose can turn a fragile manual setup into a reliable, reproducible DNS fortress, provided you configure it correctly. The key benefits are automatic restarts, isolated dependencies, and version-controlled configuration that survives reboots and SD card corruption far better than a bare-metal install.

Why Traditional Pi-hole Setups Fail

The classic Pi-hole install is a shell script that drops binaries, config files, and a web server directly onto the Raspberry Pi's OS. This works, but it creates several single points of failure:

  • SD card corruption after an unclean shutdown, which is common with the Pi's lack of a power button.
  • Dependency conflicts when a system update changes a library Pi-hole relies on.
  • Manual drift—you edit /etc/pihole/setupVars.conf by hand, forget what you changed, and can't reproduce it on a new SD card.
  • No automatic recovery—if the Pi-hole process crashes, nothing restarts it until you notice the ads are back.

These issues are especially painful because Pi-hole is often the network's DNS server, so its failure takes down internet name resolution for every device on the LAN.

What Docker Compose Actually Changes

Docker Compose is a declarative tool: you write a docker-compose.yml file that describes the container, its image, ports, volumes, and restart policy. When you run docker compose up -d, Docker creates the container exactly as specified. The practical differences for Pi-hole are:

  • Restart policy: restart: unless-stopped makes Docker automatically restart the container if it crashes or if the Pi reboots.
  • Isolation: Pi-hole's dependencies (lighttpd, PHP, dnsmasq) live inside the container image, not in the host OS. Host updates can't break them.
  • Reproducibility: The YAML file plus a named volume is your entire config. Back up those two things and you can restore Pi-hole on a new Pi in minutes.
  • Clean upgrades: To update Pi-hole, you pull a new image and recreate the container. No leftover files from old versions.

How to Set It Up Properly

Here is a minimal, working docker-compose.yml for Pi-hole on a Raspberry Pi (ARM64 or ARMv7). This is the core pattern most guides use:

  1. Create a directory, e.g. ~/pihole, and place this file inside:
version: "3.8"
services:
  pihole:
    image: pihole/pihole:latest
    container_name: pihole
    ports:
      - "53:53/tcp"
      - "53:53/udp"
      - "80:80/tcp"   # or 8080:80 to avoid conflict
    environment:
      TZ: 'America/New_York'   # change to your timezone
      WEBPASSWORD: 'your-strong-password'
    volumes:
      - './etc-pihole:/etc/pihole'
      - './etc-dnsmasq.d:/etc/dnsmasq.d'
    restart: unless-stopped
  1. Run docker compose up -d.
  2. Set your router's DHCP DNS to the Pi's IP address.
  3. Access the admin console at http://<pi-ip>/admin.

That's the entire setup. The named volumes persist your blocklists, settings, and query logs across container recreations.

Port 53 and Systemd-Resolved Conflicts

The most common problem people hit is that the host OS is already using port 53. On Ubuntu 20.04+ and Debian 11+, systemd-resolved binds to 127.0.0.53:53 by default. Docker will fail to start Pi-hole with an error like Error starting userland proxy: listen tcp4 0.0.0.0:53: bind: address already in use.

Fix it by disabling the stub resolver:

  • Edit /etc/systemd/resolved.conf and set DNSStubListener=no.
  • Restart the service with sudo systemctl restart systemd-resolved.
  • Alternatively, run Pi-hole on a different port (e.g., 5353) and use a router-level DNS forward—but that adds complexity.

On Raspberry Pi OS (which uses dhcpcd and dnsmasq for the built-in hotspot), you may need to disable the default dnsmasq service first. Check with sudo systemctl status dnsmasq and stop it if active.

Why It's More Robust Than Bare-Metal

Docker's restart policy is the headline feature. If Pi-hole's web server crashes, Docker restarts the container in seconds. If the Pi loses power, Docker starts the container automatically on boot, before you log in. This alone eliminates the "internet is down because Pi-hole died" class of problems.

Another subtle benefit: the container runs as a non-root user by default (the official image uses user pihole), so a compromised web interface doesn't give an attacker root on the host. The host's /etc/hosts and network stack are untouched, reducing the chance of a bad config bricking the OS.

Finally, updating is safer. With a bare-metal install, a failed pihole -up can leave you with a half-updated system. With Docker, you pull a new image, and if it fails to start, you simply docker compose up -d with the old image tag and you're back.

Common Misconception: Docker Slows DNS

Many users worry that Docker's network stack adds latency to DNS queries. In practice, the overhead is negligible—typically under 1 millisecond on a local network. Docker uses a bridge network with iptables NAT, but the packet path is still kernel-level. The bigger factor is your upstream DNS provider (e.g., Cloudflare, Google) and the Pi's CPU. A Pi 3 or newer handles thousands of queries per second without issue.

What does add latency is using a custom Docker network with a DNS proxy. Stick to the default bridge with published ports, and you'll see no measurable difference from bare-metal.

Alternatives and When Not to Use Docker

Docker isn't the only option. Here are the main alternatives and their trade-offs:

  • Bare-metal install: simplest for a single Pi, but fragile as discussed.
  • Ansible or shell scripts: automate the bare-metal install, giving reproducibility without Docker. Still vulnerable to OS updates breaking dependencies.
  • Pi-hole in a VM (Proxmox, etc.): good isolation but heavier resource use and more complex networking.
  • AdGuard Home: a different DNS blocker that also has a Docker image. Many find its setup simpler, but it lacks some of Pi-hole's advanced features.

When is Docker not the right choice? If you're running Pi-hole on a device with very little RAM (less than 512 MB), the Docker daemon itself uses about 100–150 MB. A bare-metal install would leave more memory for caching. Also, if you're not comfortable with YAML and command-line basics, the learning curve might not be worth it for a single service.

Frequently Asked Questions

Does Docker Compose work on Raspberry Pi OS Lite?

Yes. Install Docker with curl -fsSL https://get.docker.com | sh and then install the compose plugin with sudo apt install docker-compose-plugin. The official Pi-hole image supports ARMv7 and ARM64, so it runs on Pi 2 and newer.

How do I back up my Docker Pi-hole config?

Back up the two directories you mounted as volumes: ./etc-pihole and ./etc-dnsmasq.d. Copy them to a USB drive or cloud storage. To restore, place them in the same relative paths and run docker compose up -d again.

Can I run Pi-hole in Docker alongside other containers like Unbound?

Yes, this is a common pattern. Add an Unbound service to the same compose file, then set Pi-hole's upstream DNS to unbound (the container name) instead of a public resolver. This gives you a recursive DNS setup that's fully self-contained.

Why is my Pi-hole container restarting in a loop?

Check the logs with docker logs pihole. Common causes: a wrong timezone in the TZ variable, a corrupted volume from a previous version, or a permission issue on the mounted directories. Fix the YAML or clear the volume and start fresh.

Is it safe to use the latest tag?

For Pi-hole, latest is generally fine because the project follows semantic versioning and the image is rebuilt frequently. However, for maximum stability, pin to a specific version like pihole/pihole:2025.02.1 and test before upgrading.

Does Docker make Pi-hole slower on a Raspberry Pi Zero?

On a Pi Zero (single-core 1 GHz), Docker adds noticeable overhead because the daemon competes for CPU. You'll still get acceptable DNS performance for a small home network, but if you have many devices, a Pi 3 or 4 is recommended.