How To Use mDNS Within Docker Container

mDNS is a service that allows you to access hosts within your LAN by using their hostnames. However, it is not working by default in a docker container.

In order to use mDNS in a docker container, you’ll have to:

  • Install avahi-utils within your container.
  • Map these two files into your container, /var/run/dbus and /var/run/avahi-daemon/socket.

For example, you can start your container like:

docker run -v /var/run/dbus:/var/run/dbus -v /var/run/avahi-daemon/socket:/var/run/avahi-daemon/socket -it debian:10

and then run the following commands within your contain:

apt update && apt install avahi-utils -y

Now, you can ping your hosts within your LAN, e.g.

root@cccbde9152f2:/# ping nfs-server.local -c 4
PING nfs-server.local (192.168.1.101) 56(84) bytes of data.
64 bytes from 192.168.1.101 (192.168.1.101): icmp_seq=1 ttl=63 time=0.193 ms
64 bytes from 192.168.1.101 (192.168.1.101): icmp_seq=2 ttl=63 time=0.208 ms
64 bytes from 192.168.1.101 (192.168.1.101): icmp_seq=3 ttl=63 time=0.241 ms
64 bytes from 192.168.1.101 (192.168.1.101): icmp_seq=4 ttl=63 time=0.252 ms

--- nfs-server.local ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 36ms
rtt min/avg/max/mdev = 0.193/0.223/0.252/0.028 ms
root@cccbde9152f2:/#