🍒

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

2025/03/12に公開

はじめに

以前 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ポートを変更しているのは、immichのポートとかぶっちゃうので…
Pi-holeの管理者画面は自分しか使わないので、自分が覚えられそうなポート番号を適当に割り当てました。
公式ページのportsにはIPアドレスの記載がなかったので本番環境では不要なのかもしれませんが、私の開発環境ではポート番号のみだとエラーが出たので記載。

compose.yml
# More info at https://github.com/pi-hole/docker-pi-hole/ and https://docs.pi-hole.net/
services:
  pihole:
    container_name: pihole
    image: pihole/pihole:latest
    ports:
      # DNS Ports
-      - "53:53/tcp"
-      - "53:53/udp"
+      - "${HOST_IP}:53:53/tcp"
+      - "${HOST_IP}:53:53/udp"
      # Default HTTP Port
-      - "80:80/tcp"
+      - "${HOST_IP}:8080:80/tcp"
      # Default HTTPs Port. FTL will generate a self-signed certificate
-      - "443:443/tcp"
+      - "${HOST_IP}:8443: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

.envファイルの作成

Windows Terminal
sudo vim .env

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

.env
FTL_PASSWORD=4567changeMyPassword0123
HOST_IP=192.168.92.101

サービスの開始

Windows Terminal
sudo docker compose up -d

確認してみる

ubuntuのIPアドレスをip addr showなどで調べて、ブラウザで(IPアドレス):8080/adminにアクセスし、Pi-holeのログイン画面が表示されるか? → うまくいった
https://192.168.92.101:8443/admin/loginへのログインも同じくうまくいった。
ログイン画面

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

さいごに

私がdockerを勉強しようと思ったのは、そもそもこのPi-holeの管理者画面とimmichのブラウザ画面がバッティングしてしまったから。
ymlファイルのポート番号をこうやって簡単に変更できて、これだけで解決できるのは大きなメリットだと思います。

Discussion