🍒

Ubuntu24.04+Docker上にPi-holeを導入

に公開

はじめに

以前 Raspberry Pi 4B に入れて運用していたPi-holeを、別途購入した自宅サーバーに導入したいので、検証環境であるwsl2上のUbuntu24.04にsyncthingをインストールしてみます。

環境

以下環境で作成しました。

  • 作成日:2025年3月12日
  • windows 11
    • Pro
    • version 24H2
  • Ubuntu24.04.1(microsoft storeに記載のバージョン)

Pi-hole用ディレクトリの作成

他のcontainerと同じくサーバー用途で使いたいので、/srv/dockerディレクトリをルートディレクトリとし、その下にpiholeディレクトリを作成。

Windows Terminal
cd /srv/docker
sudo mkdir ./pihole
cd ./pihole

Pi-hole用composeファイルの作成

Windows Terminal
sudo mkdir /etc/pihole
sudo vim ./compose.yml

内容はそんなに多くないので公式ページから直接コピペしました。

↓いちぶ内容を自分の環境用に調整します。
HTTPポートとHTTPSポート、肝心なDNS用ポートがホストと被ってしまうので、MACベースVLANの設定を追加します。
MACベースVLANの設定については以下youtubeを参考にしました。

compose.yml
# More info at https://github.com/pi-hole/docker-pi-hole/ and https://docs.pi-hole.net/
services:
  pihole:
    container_name: pihole
+    # MACベースVLAN関連①の設定ここから
+    hostname: pihole-server
+    networks:
+      pih_network:
+        ipv4_address: ${IP_ADDR}
+    # MACベースVLAN関連①の設定ここまで
    image: pihole/pihole:latest
    ports:
      # DNS Ports
      - "53:53/tcp"
      - "53:53/udp"
      # Default HTTP Port
      - "80:80/tcp"
      # Default HTTPs Port. FTL will generate a self-signed certificate
      - "443:443/tcp"
      # Uncomment the below if using Pi-hole as your DHCP Server
      #- "67:67/udp"
    environment:
      # Set the appropriate timezone for your location (https://en.wikipedia.org/wiki/List_of_tz_database_time_zones), e.g:
-      TZ: 'Europe/London'
+      TZ: 'Asia/Tokyo'
      # Set a password to access the web interface. Not setting one will result in a random password being assigned
-      FTLCONF_webserver_api_password: 'correct horse battery staple'
+      FTLCONF_webserver_api_password: ${FTL_PASSWORD}
    # Volumes store your data between container upgrades
    volumes:
      # For persisting Pi-hole's databases and common configuration file
-      - './etc-pihole:/etc/pihole'
+      - type: bind
+        source: "./etc-pihole"
+        target: "/etc/pihole"
      # Uncomment the below if you have custom dnsmasq config files that you want to persist. Not needed for most starting fresh with Pi-hole v6. If you're upgrading from v5 you and have used this directory before, you should keep it enabled for the first v6 container start to allow for a complete migration. It can be removed afterwards
      #- './etc-dnsmasq.d:/etc/dnsmasq.d'
    cap_add:
      # See https://github.com/pi-hole/docker-pi-hole#note-on-capabilities
      # Required if you are using Pi-hole as your DHCP server, else not needed
      - NET_ADMIN
    restart: unless-stopped
+# MACベースVLAN関連②の設定ここから
+networks:
+  pih_network:
+    driver: macvlan
+    driver_opts:
+      parent: enp1s0
+    ipam:
+      config:
+        - subnet: 192.168.1.0/24
+          gateway: 192.168.1.1
+          ip_range: ${IP_ADDR}/32
+# MACベースVLAN関連②の設定ここまで

.envファイルの作成

Windows Terminal
sudo vim .env

IPアドレスはホスト環境のものを入れるので、wsl2に入れたUbuntu24.04のIPアドレスを'ip addr show'で調べて指定。

.env
FTL_PASSWORD=4567changeMyPassword0123
IP_ADDR=168.192.1.99

サービスの開始

Windows Terminal
sudo docker compose up -d

確認してみる

ブラウザで192.168.1.99/adminにアクセスし、Pi-holeのログイン画面が表示されるか? → うまくいった

https://192.168.1.99/admin/loginへのログインも同じくうまくいった。
ログイン画面

ログイン画面でFTL_PASSWORDに設定したパスワードを入力してログインできるか? → うまくいった
トップページ

よしよし上手くいったぞ…あとは…

  1. ルーターのプライマリDNSを192.168.1.99に設定
  2. ルーターの機能でwireguard等のVPNサーバーを構築する
  3. スマホにwireguardなどのVPNアプリを入れて一律自宅ルーター経由で通信する

とでもしておけば、洗練したネット環境が手に入りそうだ…

さいごに

私がdockerを勉強しようと思ったのは、そもそもこのPi-holeの管理者画面とimmichのブラウザ画面がバッティングしてしまったから。
最初はymlファイルのポート番号が簡単に変更できたので喜んでいたが、一部設定で行き詰まるところ(DNSのポートがホストとバッティングするからかログが正常に取得できず、dashboardの表示がおかしくなる)があり、MACベースVLANでの運用に切り替えました。
compose.ymlの設定を少し調整するだけで解決できるのは大きなメリットと感じますね。

Discussion