Create containerized Core DNS server

Vineet Kumar
2 min readAug 7, 2022

Run the following commands to make required Directory
a. mkdir -p /home/coredns/
b. cd /home/coredns/
c. vi domain.name.db (and paste the below content)

domain.name.      IN  SOA dns.domain.name. domain.name.in. 2015082541 7200 3600 1209600 3600   
service-name IN A 10.10.x.x
service2 IN A 10.110.x.x

d. Create the Core File for dns forwarder

vi /home/coredns/Corefile (and paste the below content)

.:53 {   
forward . 164.100.3.1 # Gateway IP
log
errors
}

domain.name:53 {
file /root/domain.name.db
log
errors
}

2. pull this image and run the following commands:

docker run -d --name coredns -p 53:53/udp --restart=always \        --volume=/home/niccloud/coredns/:/root/ \ 
coredns:1.8.4 -conf /root/Corefile

Run the Following command to find the ip of core DNS server

ip a

For example if ip is 10.10.x.x then Add following lines in netplan files of each client nodes

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
ens3:
dhcp4: true
nameservers:
addresses:
- 10.10.x.x
ens4:
dhcp4: true
optional: true
version: 2

After that run below commands

netplan generate
netplan apply

Run Following command To check connectivity from each node to DNS server

netcat -uzv <DNS-SERVER-IP> <PORT>

OR

Run the following script to create CORE-DNS Server

#!/bin/bash
##*********CREATE DIRECTORY***********
mkdir -p /home/coredns/
echo "Directory Successfully Created"
cd /home/coredns/
##************************************
echo
"domain.name. IN SOA dns.cloud.gov.in. pvtdns.domain.name. 2015082541 7200 3600 1209600 3600
service-name1 IN A 10.10.x.x
service2 IN A 10.10.x.x" >/home/coredns/domain.name.db
echo
".:53 {
forward . 164.100.3.1 # GATEWAy IP
log
errors
}
domain.name:53 {
file /root/domain.name.db
log
errors
} " > /home/coredns/Corefile
echo "Both files domain.name.db and Corefile is created successfully"
############ STOP Resolved Service If Running#######################
RESOLVE_SERVICE="systemd-resolve"
if pgrep -x "$RESOLVE_SERVICE" >/dev/null
then
echo "$RESOLVE_SERVICE is running So stopping the resolve service"
systemctl stop systemd-resolved
systemctl disable systemd-resolved
else
echo "$RESOLVE_SERVICE is not running so Start Docker."
fi
############*************************#########################

docker run -d --name coredns -p 53:53/udp --restart=always --volume=/home/coredns/:/root/ coredns:1.8.4 -conf /root/Corefile
docker ps | grep coredns
############***************************************

Git repo For Script

Enjoy!!!

--

--