/docker-localdns

Simple DNS server in go connecting to docker host to respond to queries with containers IP

Primary LanguageMakefileMIT LicenseMIT

Docker local dns

Installation

Compile the project

make build

Start the server (need sudo to expose on port 53) :

sudo ./bin/docker-localdns

Add the entry nameserver 127.0.0.1 to /etc/resolv.conf If resolve.conf is generated by NetworkManager edit /etc/NetworkManager/conf.d/dns-servers.conf file with

[global-dns-domain-*]
servers=127.0.0.1,192.168.1.1

where 192.168.1.1 is NetworkManager local DNS server

Archlinux install

Compile from sources and install binary and systemd service

cd ./package
makepkg
makepkg --install

Usage

▶ docker run -d nginx
38e23fe936015086ab459b7892172777b4678914ca5333c6db4db7ab291a12dd

▶ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
38e23fe93601        nginx               "nginx -g 'daemon of…"   3 seconds ago       Up 1 second         80/tcp              elegant_mcclintock

▶ dig elegant_mcclintock.docker.local
...
;; QUESTION SECTION:
;elegant_mcclintock.docker.local. IN	A

;; ANSWER SECTION:
elegant_mcclintock.docker.local. 3600 IN A	172.17.0.2
...

▶ dig elegant_mcclintock.docker
...
;; QUESTION SECTION:
;elegant_mcclintock.docker. IN	A

;; ANSWER SECTION:
elegant_mcclintock.docker. 3600 IN A	172.17.0.2
...

▶ dig elegant_mcclintock
...
;; QUESTION SECTION:
;elegant_mcclintock. IN	A

;; ANSWER SECTION:
elegant_mcclintock. 3600 IN A	172.17.0.2
...

▶ docker stop elegant_mcclintock
elegant_mcclintock

▶ dig elegant_mcclintock.docker.local
...
;; QUESTION SECTION:
;elegant_mcclintock.docker.local. IN	A

...

▶ docker run --name container1 -d nginx
19fe99c5ae4ddabb9df71d34809a30f667269e73bae3d1d33fd5dd6e0c3e04d4

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
19fe99c5ae4d        nginx               "nginx -g 'daemon of…"   5 minutes ago       Up 5 minutes        80/tcp              container1


▶ dig container1.docker.local

; <<>> DiG 9.12.1 <<>> container1.docker.local
...
;; QUESTION SECTION:
;container1.docker.local.	IN	A

;; ANSWER SECTION:
container1.docker.local. 3600	IN	A	172.17.0.2
...

Features

  • DNS Server
  • Create DNS entries for containers at startup
  • DNS entry for each network of a container
  • Listen docker container start/stop/kill/die events
  • Listen docker network attach
  • Listen docker network detach
  • Change DNS entries for containers on event
  • Install cobra to create good CLI
  • Configure domain
  • Configure docker socket
  • Systemd service sample
  • Register as dns server for host at startup