Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

【docker】【Ubuntu】【bind】DNSのドメインフィルタリング (ブラックリスト方式)

構成図

DNS サーバ (bind) の名前解決において、接続拒止したい宛先ドメインを RPZ (Response Policy Zones)として登録することで、接続のフィルタリングを行う。フィルター対象ドメイン以外は上位のDNSサーバにて名前解決を行う。
また、 DNS サーバは docker を用いて PC 内にコンテナとして起動したものを利用する。

フィルター対象ドメインnhk.or.jp
nhk.jp
nhk
使用コンテナイメージubuntu/bind9

bind の設定

ドメイン定義ファイルにフィルタリング用のドメイン名「rpz」を定義する。
【named.conf.local】

zone "rpz" {
  type master;
  file "rpz.zone";
};

基本的なオプションファイル定義は以下、フィルタリング用の「response-policy」を定義する。
【named.conf.options】

acl my-acl {
  192.168.0.0/16;
  localhost;
};

options {
  forwarders {
    192.168.254.254;
  };
  version "unknown";
  allow-query { my-acl; };
  allow-query-cache { my-acl; };
  allow-recursion { my-acl; };
  allow-transfer { none; };
  response-policy { zone "rpz"; };
};

ドメインデータベースファイルを定義する。
【rpz.zone】

$ORIGIN rpz.
$TTL    3600
@       IN     SOA   localhost. root.localhost. (
    2025031501      ; Serial
    3600            ; Refresh
    900             ; Retry
    604800          ; Expire
    3600            ; Minimum
)

@               IN  NS      localhost.
nhk.or.jp       IN  A       127.0.0.1
*.nhk.or.jp     IN  A       127.0.0.1
nhk.jp          IN  A       127.0.0.1
*.nhk.jp        IN  A       127.0.0.1
nhk             IN  A       127.0.0.1
*.nhk           IN  A       127.0.0.1

docker の設定

compose ファイルの準備

docker desktop」 をインストールし、「docker compose」用のファイルを作成する。
【compose.yaml】
※「docker-compose.yaml」ファイルは非推奨とのこと
「How Compose works」

services:
  bind9:
    image: ubuntu/bind9:latest
    restart: always
    ports:
      - "53:53/udp"
    volumes:
      - "./volumes/etc_bind/named.conf.local:/etc/bind/named.conf.local:ro"
      - "./volumes/etc_bind/named.conf.options:/etc/bind/named.conf.options:ro"
      - "./volumes/var_cache:/var/cache/bind:rw"
    environment:
      - "TZ=Asia/Tokyo"
      - "BIND9_USER=bind"

ファイル の配置

各種ファイルを以下の様に配置する。

.
├─ compose.yaml
└─ volumes
   ├─ etc_bind
   |  ├─ named.conf.local
   |  └─ named.conf.options
   |
   └─ var_cache
      └─ rpz.zone

docker の起動

docker compose からの起動

上記のファイル階層に「cd」コマンドで移動する。または以下の様にファイルパスを指定して
「docker compose」を実行する。
※初回起動時はコンテナイメージのダウンロードが行われる。

apple@mac docker % docker compose -f ./bind9/compose.yaml up -d
[+] Running 4/4
 ✔ bind9 Pulled                                                  72.0s
   ✔ a2defac7906c Pull complete                                   1.8s
   ✔ 5edf575356dc Pull complete                                  53.9s
   ✔ dd4130317f5d Pull complete                                  65.2s
[+] Running 2/2
 ✔ Network bind9_default    Created                               0.3s
 ✔ Container bind9-bind9-1  Started                               1.4s

apple@mac docker % docker compose -f ./bind9/compose.yaml ps
NAME            IMAGE                 COMMAND                  SERVICE   CREATED         STATUS         PORTS
bind9-bind9-1   ubuntu/bind9:latest   "docker-entrypoint.sh"   bind9     2 minutes ago   Up 2 minutes   53/tcp, 953/tcp, 0.0.0.0:53->53/udp

動作確認

以下の様に DNS 応答が「127.0.0.1」となることを確認する。フィルター対象外のドメインは正常に名前解決が出来ている。

apple@mac docker % dig www.nhk.or.jp @localhost

; <<>> DiG 9.10.6 <<>> www.nhk.or.jp @localhost
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 34163
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 2

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;www.nhk.or.jp.			IN	A

;; ANSWER SECTION:
www.nhk.or.jp.		5	IN	A	127.0.0.1

;; ADDITIONAL SECTION:
rpz.			3600	IN	SOA	localhost. root.localhost. 2025031501 3600 900 604800 86400

;; Query time: 322 msec
;; SERVER: ::1#53(::1)
;; WHEN: Sat Mar 15 22:00:18 JST 2025
;; MSG SIZE  rcvd: 111
apple@mac docker % dig google.com @localhost

; <<>> DiG 9.10.6 <<>> google.com @localhost
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 25966
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1232
;; QUESTION SECTION:
;google.com.			IN	A

;; ANSWER SECTION:
google.com.		300	IN	A	142.250.196.110

;; Query time: 241 msec
;; SERVER: ::1#53(::1)
;; WHEN: Sat Mar 15 22:05:29 JST 2025
;; MSG SIZE  rcvd: 55

docker compose からの停止

停止する場合は以下の様にする。

apple@mac docker % docker compose -f bind9/compose.yaml down
[+] Running 2/2
 ✔ Container bind9-bind9-1  Removed                            0.6s
 ✔ Network bind9_default    Removed                            0.5s

参考URL
bindのRPZを使ってDNSレコードを偽装する
自宅ネットワークのbind9をdocker composeに移行した
ubuntu/bind9
MinIOを用いたS3ローカル開発環境の構築ガイド(AWS SDK for Java 2.x)