LoginSignup
18
18

More than 5 years have passed since last update.

自宅サーバのDockerでDNSを運用する

Last updated at Posted at 2015-12-26

自宅サーバを持っていると、外では自宅のIPアドレス、家ではローカルIPアドレスでアクセスなければならず、今まではhostsを直接書き換えるスクリプトを運用していた。しかし、それではiPadなどのモバイル端末では通用しないこともあって、DNSサーバを立てることにした。

Bind9のDockerイメージを作っている人もいたが、うまくいかなかったので、Debianのイメージから作ることにした。

Dockerfile
FROM debian:8.1
RUN apt-get update
RUN apt-get install -y bind9
ADD * /etc/bind/
ENTRYPOINT service bind9 start;tail -f cat /var/log/faillog

dockerコマンドはMakefileに記述して、直接入力しないようにしている。毎回buildし直すのはだるいので、トライアンドエラーをやっているときは、cpでファイルを入れてrestartで起動するようにしている。

Makefile
build:
    docker build -t bind .
run:
    docker run -d --name bind --restart=always --publish 53:53/udp bind
bash:
    docker exec -it bind bash
rm:
    docker rm -f bind
pushconf:
    docker cp ./named.conf bind:/etc/bind/
    docker cp ./named.conf.default-zones bind:/etc/bind/
    docker cp ./named.conf.local bind:/etc/bind/
    docker cp ./named.conf.options bind:/etc/bind/
    docker cp ./db.house.j74th.com bind:/etc/bind/
start: pushconf
    docker start bind
stop:
    docker stop bind
restart: pushconf
    docker restart bind
testyahoo:
    nslookup www.yahoo.co.jp 127.0.0.1
testhouse:
    nslookup house 127.0.0.1

まず、named.conf.localにzoneを定義する。

named.conf.local
zone "house.j74th.com" {
    type master;
    file "/etc/bind/db.house.j74th.com";
};

db.house.j74th.comにDNSレコードを記述する。

db.house.j74th.com
$TTL    604800
@   IN  SOA house.j74th.com. root.house.j74th.com. (
                  2     ; Serial
             604800     ; Refresh
              86400     ; Retry
            2419200     ; Expire
             604800 )   ; Negative Cache TTL
;
@   IN  NS  house.j74th.com.
@   IN  A   192.168.1.65

次に、named.conf.optionsにいかの設定をする。

  • 自宅サーバ以外のドメインでは、プロバイダのDNSを見に行くようにforwardersに記述する。
  • allow-queryにクエリを許可するアドレスとして、自宅内のアドレスを指定する。
named.conf.options
options {
    directory "/var/cache/bind";

    allow-query {
        127.0.0.1;
        192.168.1.0/24;
    };

    forwarders {
        プロバイダのプライマリDNS;
        プロバイダのセカンダリDNS;
    };

    dnssec-validation auto;

    auth-nxdomain no;    # conform to RFC1035
        listen-on-v6 { any; };
};

これで、あとはdockerコマンドを叩く。

make build
make run

なぜか、一度リスタートしなければうまく動かなかった。

make bash
service bind9 restart

これで後は、ルータが見に行くDNSをこのDockerサーバのDNSに変更する。すると、家の中にある全ての機器で自宅サーバにアクセスできるようになった!

18
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18