Pi‐hole を docker compose で運用する - aktnk/til GitHub Wiki

改善したいこと

  • これまで280blockerを利用しているが、応答に時間がかかることがある。そこで、自前でAdブロックができる環境としてPi-holeを立ち上げる

Pi-hole立ち上げ手順

  • Raspberry Pi 5のdocker環境上にPi-holeを稼働させる なお、UPSのついていないRaspberry Pi 5が落ちていてもインターネットにアクセスできるよう、UPS上で稼働しているDNSからPi-holeに最初にフォワードするようにしている

Pi-holeコンテナの起動

  1. compose.yamlを作成を下記のように作成、なお、DHCPは稼働させないのでコメントアウト、また、Webサーバが起動しているためPi-holeのWebは8100ポートを公開

    services:
      pihole:
        container_name: pihole
        image: pihole/pihole:2025.02.0
        # network_mode: "host"  # For DHCP server
        ports:
          - "53:53/tcp"
          - "53:53/udp"
          - "8100:80/tcp"
        environment:
          # https://github.com/pi-hole/docker-pi-hole#environment-variables
          TZ: 'Asia/Tokyo'
          PIHOLE_DNS_: 1.1.1.1;1.0.0.1;2606:4700:4700::1111;2606:4700:4700::1001  # Cloudflare
          DNSMASQ_LISTENING: single
          DNSSEC: true
          DHCP_ACTIVE: false
        volumes:
          - './etc-pihole:/etc/pihole'
          - './etc-dnsmasq.d:/etc/dnsmasq.d'
        # cap_add:
        #   - NET_ADMIN # Required only if you are using Pi-hole as your DHCP server
        restart: unless-stopped
    
  2. コンテナを起動

    $ docker compose up -d
    
  3. パスワードを設定

    $ docker comopose exec pihole /bin/bash
    abf412c44321:/# pihole setpassword
    Enter New Password (Blank for no password): 
    Confirm Password: 
    [✓] New password set
    abf412c44321:/# 
    
  4. ブラウザでhttp://localhost:8100/adminへアクセスし、先に設定したパスワードを入力する

  5. ブロック先を追加する

    • https://github.com/StevenBlack/hostsからブロック先として追加するlinkをコピー
    • コンテナ内でpihole -gを実行し有効化する
      abf412c44321:/# pihole -g
      [✓]  DNS resolution is available
      
      [i] Neutrino emissions detected...
      
      [✓]  Preparing new gravity database
      [✓]  Creating new gravity databases
      [✓]  Pulling blocklist source list into range
      [i] Using libz compression
      
      [i] Target: https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
      [✓]  Status: No changes detected
      [✓]  Parsed 127472 exact domains and 0 ABP-style domains (blocking, ignored 0 non-domain entries)
      
      [i] Target: https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn/hosts
      [✓]  Status: No changes detected
      [✓]  Parsed 210794 exact domains and 0 ABP-style domains (blocking, ignored 0 non-domain entries)
      
      [✓]  Building tree
      (省略)
      [✓]  Done.
      abf412c44321:/# exit
      exit
      
  6. DNS settingを変更

    1. SystemメニューのDNS settingsを開き、Expertを選択
    2. Potentially dangerous options欄で、Respond only on interfaceを選択
  7. 別に立ち上げているDNSサーバの設定を変更

    • 先にPi-holeをインストールしたマシンにDNSを問い合わせるよう、IPアドレスを設定する
  8. 動作確認

    • Pi-holeのDashboardを開き、下記のようなグラフが見えれば、OK image