Easily Expose Docker Containers on Your Local Network with this DNS Monitoring Tool and hosted on Unraid/Proxmox

Make your Docker containers accessible over local DNS.

What does it do?

This service iterates over Docker containers that have a macvlan network and container_name assigned to them, and exposes a hosts file that can be used with CoreDNS.

Prerequisites

Create a Docker macvlan network:

# This is just an example, adapt to your needs
$ docker network create -d macvlan --subnet=10.1.30.0/24 --gateway=10.1.30.1 -o parent=eth0.30 vlan.30

Usage

docker-compose.yaml

docker-dns-monitor:
  container_name: docker-dns-monitor
  image: ghcr.io/ad-on-is/docker-hosts-provider
  restart: always
  environment:
    - DOMAIN=home.arpa # Use your preferred local DNS (homelab.lan, my.home, etc.)
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    - /path/to/generated/hosts:/etc/docker_hosts

coredns:
  image: coredns/coredns
  container_name: coredns
  restart: always
  ports:
    - 53:53
    - 53:53/udp
  volumes:
    - ./Corefile:/Corefile
    - /path/to/generated/hosts:/etc/coredns/hosts
  command: -conf /Corefile

example-container:
  image: nginx
  container_name: web
  networks:
    vlan.30:
      ipv4_address: 10.1.30.20 # (optional)

networks:
  vlan.30:
    external: true

Corefile

To use with CoreDNS:

. {
    log
    errors
    debug
    health
    ready
    hosts /etc/coredns/hosts {
        reload 10s
        fallthrough
    }
    cache 30
    reload
    loadbalance
}

DNS/PTR Entries

To make it work, you need to tell your DNS server (router, Pi-hole, etc.) about CoreDNS and the subnet range to look for the DNS/PTR entries.

If your LAN spans from 10.1.0.0 to 10.1.255.255, and CoreDNS runs on 10.1.0.2, this is what you’d do:

dnsmasq.conf

# This is just an example, adapt to your needs
server=/1.10.in-addr.arpa/10.1.0.2

Verify It Works

Execute the following commands on a machine that is within your network:

$ ping web.home.arpa
# PING web.home.arpa (10.1.30.20) 56(84) bytes of data.
# 64 bytes from web.home.arpa (10.1.30.20): icmp_seq=1 ttl=64 time=0.182 ms

$ dig -x 10.1.30.20
# ...
# ;; ANSWER SECTION:
# 20.30.1.10.in-addr.arpa. 3600 IN PTR web.home.arpa.
# ...

how to specify both IPv6 and IPv4 addresses for one container for coredns

services:
  coredns:
    image: coredns/coredns
    container_name: coredns
    command: -conf /data/Corefile
    ports:
      - 53:53/udp
      - 53:53/tcp
      - 9153:9153/tcp
    volumes:
      - ./coredns:/data:ro
    networks:
      sapling:
        ipv4_address: 172.16.238.5
        ipv6_address: 2404:xxxx:4314:d::5
    restart: unless-stopped

networks:
  sapling:
    driver: bridge
    driver_opts:
      com.docker.network.bridge.gateway_mode_ipv6: "routed"
    enable_ipv6: true
    ipam:
      driver: default
      config:
        - subnet: 172.16.238.0/24
          gateway: 172.16.238.1
        - subnet: 2404:xxxx:4314:d::/64
          gateway: 2404:xxxx:4314:d::1