This project provides a lightweight, automatic DNS service for Docker containers using CoreDNS and a Python event listener.
It dynamically maps container hostnames and IP addresses into CoreDNS, supports reverse DNS (PTR) lookups automatically, and allows you to specify for what interfaces you want to create DNS records.
✅ Automatic DNS Record Management
- Every container attached to a matched network (see
NETWORK_PREFIXbelow) gets a fully-qualified domain name (FQDN) automatically added to/etc/coredns/hosts. - CoreDNS automatically provides forward (A) and reverse (PTR) resolution.
- A matched container that's currently stopped (no IP assigned yet) gets a
FALLBACK_IPplaceholder entry instead of being omitted. - Containers with no matched network get no entry at all — they don't resolve, rather than resolving to a placeholder.
✅ Event-Based Updates
- The DNS host file updates instantly on Docker events (start, stop, destroy, etc.).
- No need for polling — it’s lightweight and real-time.
✅ Multi-Network Support
- Supports one or more Docker network prefixes.
- Example:
NETWORK_PREFIX=macvlan,dmzwill include all containers in any network that starts withmacvlanordmz.
✅ Custom Domain
- Easily define your internal domain via the
DOMAINenvironment variable. - Example:
DOMAIN=docker.local. - If no variable is entered the default value 'docker.local' will be used.
✅ Reverse DNS Support
- CoreDNS automatically provides reverse (PTR) lookups for all listed IPs.
✅ No DNS forwarding
- CoreDNS will not forward requests to upstream. Use this only for automated Docker container resolving.
coredns-docker/
├── Dockerfile # Builds the CoreDNS + Python container
├── Corefile # CoreDNS configuration
├── scripts/update_hosts.py # Watches Docker events and updates hosts file
└── docker-compose.yml # Example compose setup
Environment variables:
| Variable | Default | Description | Example |
|---|---|---|---|
DOMAIN |
docker.local |
DNS suffix for containers | docker.local |
NETWORK_PREFIX |
macvlan |
Comma-separated list of Docker network name prefixes to include | macvlan, bridge |
FALLBACK_IP |
0.0.0.0 |
Placeholder IP used for a matched container that has no address yet (e.g. it's stopped) | 0.0.0.0 |
- On startup, the Python script scans all Docker containers (running and stopped).
- For each container connected to a network matching one of the configured
NETWORK_PREFIXvalues (e.g.macvlan*,bridge*), it writes an entry to/etc/coredns/hosts. Containers with no matching network are skipped entirely — no entry, no resolution. - CoreDNS reads this file using the
hostsplugin and automatically serves both:
- Forward lookup:
container.docker.local → 192.168.10.100 - Reverse lookup:
192.168.10.100 → container.docker.local
- The script subscribes to Docker events (
start,die,destroy,connect,disconnect) and rewrites the hosts file whenever containers change; CoreDNS itself reloads that file every 2 seconds (seeCorefile), so propagation is near-instant but not synchronous.
Forward lookup:
dig @192.168.10.10 container.docker.localReverse lookup:
dig -x 192.168.10.100 @192.168.10.10