/docker-dns-bind9

Dockerize BIND9 DNS server

Primary LanguageShellMIT LicenseMIT

labbsr0x/docker-dns-bind9

Introduction

Dockerfile to create a Docker container image for BIND DNS server.

BIND is open source software that implements the Domain Name System (DNS) protocols for the Internet. It is a reference implementation of those protocols, but it is also production-grade software, suitable for use in high-volume and high-reliability applications.

Contributing

If you find this image useful here's how you can help:

  • Send a pull request with your awesome features and bug fixes
  • Help users resolve their issues.

Issues

Before reporting your issue please try updating Docker to the latest version and check if it resolves the issue. Refer to the Docker installation guide for instructions.

SELinux users should try disabling SELinux using the command setenforce 0 to see if it resolves the issue.

If the above recommendations do not help then report your issue along with the following information:

  • Output of the docker version and docker info commands
  • The docker run command or docker-compose.yml used to start the image. Mask out the sensitive bits.

Getting started

Installation

Automated builds of the image are available on Dockerhub and is the recommended method of installation.

docker pull labbsr0x/dns-bind9

Alternatively you can build the image yourself.

docker build -t labbsr0x/dns-bind9 github.com/labbsr0x/docker-dns-bind9

or

make build

Quickstart

Start BIND using:

docker run --rm --name bind -d --publish 53:53/tcp --publish 53:53/udp --volume ${PWD}/.bind9:/data labbsr0x/dns-bind9

or

make docker-run

Alternatively, you can use the sample docker-compose.yml file to start the container using Docker Compose

Persistence

For the BIND to preserve its state across container shutdown and startup you should mount a volume at /data.

The Quickstart command already mounts a volume for persistence.

mkdir -p .bind9

Maintenance

Upgrading

To upgrade to newer releases:

  1. Download the updated Docker image:
docker pull labbsr0x/dns-bind9
  1. Stop the currently running image:
docker stop bind

or

make docker-stop
  1. Remove the stopped container
docker rm -v bind

and

rm -rf .bind9
  1. Start the updated image
docker run -name bind -d \
  [OPTIONS] \
  labbsr0x/dns-bind9

Shell Access

For debugging and maintenance purposes you may want access the containers shell. If you are using Docker version 1.3.0 or higher you can access a running containers shell by starting bash using docker exec:

docker exec -it bind bash

Example

Prerequisites

  • Two servers that will be our DNS name servers with the following features installed. Referred as ns1 and ns2.
    • docker
    • docker-compose
    • git
  • newdomain.com domain as an example.
Servers Description Example FQDN Example IP
ns1 Primary DNS server ns1.newdomain.com 10.0.10.1
ns2 Secondary DNS server ns2.newdomain.com 10.0.10.2

Primary DNS

Clone github project on ns1 server

git clone https://github.com/labbsr0x/docker-dns-bind9.git

Create a directory that will be used as DNS volume

mkdir /opt/bind9

Copy primary DNS directory and docker-compose file

cp -r /opt/docker-dns-bind9/example/primary /opt/bind9/.

cp /opt/docker-dns-bind9/docker-compose.yml /opt/bind9/.

Set volume path in docker-compose.yml

...
    volumes:
    - /opt/bind9/primary:/data # Change volume path

Rename zone file db.example.com to name of desired zone.

In this example we will rename db.example.com to db.newdomain.com

mv /opt/bind9/primary/bind/etc/db.example.com /opt/bind9/primary/bind/etc/db.novodominio.com

In zone file change everywhere that are example.com to new zone and setup IP.

In this example we will change example.com to newdomain.com

;
; BIND data file for local loopback interface
;
$TTL    604800
@       IN      SOA     newdomain.com. root.newdomain.com. (
                              1         ; Serial
                         604800         ; Refresh
                          86400         ; Retry
                        2419200         ; Expire
                         604800 )       ; Negative Cache TTL
;
@       IN      NS      ns1.newdomain.com.
@       IN      NS      ns2.newdomain.com.
@       IN      A       127.0.0.1
@       IN      AAAA    ::1

ns1             A       10.0.10.1   ; Change to the desired NS1 IP
ns2             A       10.0.10.2   ; Change to the desired NS2 IP

Config the new db file and new zone in named.conf.default-zones.

In this example we will change example.com to newdomain.com and the file path db.example.com to db.newdomain.com and set Secondary DNS IP in allow-transfer.

...
zone "newdomain.com" { // Change to desired zone
        type master;
        file "/etc/bind/db.newdomain.com"; // Change to zone file path
        allow-transfer {10.0.10.2; };        // Change to Secondary DNS IP
//      allow-update {
//          key "example.com";
//  };
};
...

Start the new DNS with docker-compose.

docker-compose up -d

Secondary DNS

Clone github project on ns2 server

git clone https://github.com/labbsr0x/docker-dns-bind9.git

Create a directory that will be used as DNS volume

mkdir /opt/bind9

Copy secondary DNS directory and docker-compose file

cp -r /opt/docker-dns-bind9/example/secondary /opt/bind9/.

cp /opt/docker-dns-bind9/docker-compose.yml /opt/bind9/.

Set volume path in docker-compose.yml

...
    volumes:
    - /opt/bind9/secondary:/data # Change volume path

Config the new db file and new zone in named.conf.default-zones.

In this example we will change example.com to newdomain.com and the file path db.example.com to db.newdomain.com and set Primary DNS IP in master field.

...
zone "newdomain.com" { // Change to desired zone
        type slave;
        file "/etc/bind/db.newdomain.com";  // Change to zone file path
        masters {10.0.10.1;};               // Change to Primary DNS IP
};
...

Start the secondary DNS with docker-compose.

docker-compose up -d

Testing new DNS Server

dig -t ns newdomain.com @localhost +short

Result

ns1.newdomain.com.
ns2.newdomain.com.

Others

Example in Portuguese (pt_BR) on fabiotavarespr.dev's blog

References

References used in these projects

github.com/sameersbn/docker-bind

Deploying a DNS Server using Docker