DockerでBIND9動かしてみた
SambaのDocker化作業の一環で急遽BINDのコンテナが必要になったのでコンテナイメージを探していたけれど見つからないので適当なものを自分で準備したよ。と言っても相変わらずのコピペですが。一応Ubuntu20.04LTSで最新版のBIND9を引いてくるようにはしたつもりです。一応意図していることはできている模様。そしていよいよ本丸に突入する
■設定ファイル関連
・Dockerfile
-------------------------------------------------
FROM ubuntu:latest ENV DEBIAN_FRONTEND noninteractive # Bind install RUN apt-get update && apt-get upgrade -y && \ apt-get install -y tzdata vim dnsutils bind9 bind9utils supervisor # Setup shell ADD init.sh /init.sh RUN chmod 755 /init.sh CMD /init.sh setup
-------------------------------------------------
・init.sh
-------------------------------------------------
#!/bin/bash set -e # utility function get_host_ip(){ dnsarray=($(echo $1 | tr "." "\n")) echo ${dnsarray[3]} } function get_reverse_ip(){ iparray=($(echo $1 | tr "." "\n")) str="${iparray[2]}.${iparray[1]}.${iparray[0]}" echo ${str} } function appStart(){ /usr/bin/supervisord } # Set variables DOMAIN=${DOMAIN:-yourdomain} ACLIPRANGE=${ACLIPRANGE:-youracliprange} DNS_IP=${DNS_IP:-dnsserverip} FORWERDER_IP=${FORWERDER_IP:-forworderdnsip} MAINAD_IP=${MAINAD_IP:-main} SUBAD_IP=${SUBAD_IP:-sub} MAINAD_HOSTNAME=${MAINAD_HOSTNAME:-main_hostname} SUBAD_HOSTNAME=${SUBAD_HOSTNAME:-sub_hostname} DNS_HOST_IP=$(get_host_ip ${DNS_IP}) MAINAD_HOST_IP=$(get_host_ip ${MAINAD_IP}) SUBAD_HOST_IP=$(get_host_ip ${SUBAD_IP}) REVERSEIP=$(get_reverse_ip ${DNS_IP}) # Setting named.conf cat << EOF > /etc/bind/named.conf include "/etc/bind/named.conf.options"; include "/etc/bind/named.conf.zones"; EOF # Setting named.conf.options cat << EOF > /etc/bind/named.conf.options acl internal-network { ${ACLIPRANGE}; }; options { directory "/var/cache/bind"; forwarders { ${FORWERDER_IP}; }; allow-query { localhost; internal-network; }; recursion yes; dnssec-validation auto; listen-on-v6 { any; }; }; EOF # Setting named.conf.zones cat << EOF > /etc/bind/named.conf.zones zone "nishimura.local" IN { type master; file "/etc/bind/db.${DOMAIN}"; }; zone "${REVERSEIP}.in-addr.arpa" IN { type master; file "/etc/bind/db.${REVERSEIP}"; }; EOF # Setting Forward cat << EOF > /etc/bind/db.${DOMAIN} \$TTL 86400 @ IN SOA dns.${DOMAIN}. root.${DOMAIN}. ( 2021013001 ;Serial 3600 ;Refresh 1800 ;Retry 604800 ;Expire 865400 ;Minimum TTL ) IN NS dns.${DOMAIN}. dns IN A ${DNS_IP} ${MAINAD_HOSTNAME} IN A ${MAINAD_IP} ${SUBAD_HOSTNAME} IN A ${SUBAD_IP} EOF # Setting Reverse cat << EOF > /etc/bind/db.${REVERSEIP} \$TTL 86400 @ IN SOA dns.${DOMAIN}. root.${DOMAIN}. ( 2021013001 ;Serial 3600 ;Refresh 1800 ;Retry 604800 ;Expire 86400 ;Minimum TTL ) IN NS dns.${DOMAIN}. ${DNS_HOST_IP} IN PTR dns.${DOMAIN}. ${MAINAD_HOST_IP} IN PTR main-ad-srv.${DOMAIN}. ${SUBAD_HOST_IP} IN PTR sub-ad-srv.${DOMAIN}. EOF # Set up supervisor cat << EOF > /etc/supervisor/conf.d/supervisord.conf [supervisord] nodaemon=true [program:named] command=/usr/sbin/named -f EOF appStart exit 0
-------------------------------------------------
■起動方法
yourworkingdirはそれぞれ作業環境に置換する。
$ cd /yourworkingdir/bind $ mkdir conf $ docker build -t "ad-dns:latest" . $ docker run -v /yourworkingdir/bind/conf:/etc/bind --name ad-dns -p 53:53/udp -d ad-dns
■参考サイト
https://serip39.hatenablog.com/entry/2020/06/26/073000
https://blog.kurokobo.com/archives/2466
https://github.com/labbsr0x/docker-dns-bind9
https://www.hiroom2.com/2018/05/06/ubuntu-1804-bind-ja/
http://philosy.com/blog/2019/05/27/bind-%E3%82%92-docker-%E3%81%A7%E5%8B%95%E3%81%8B%E3%81%99/
コメント