mDNS inside HA container?

I’m running HA in a docker container. I’ve just started playing with ESPHome and would like HA to be able to connect to the ESPHome devices using their .local hostname - however, HA cannot resolve these names.
After some investigation, the container HA is running in doesn’t seem to resolve the .local addresses. The docker host resolves them ok, but the container doesn’t.
I’ve changed the docker-compose file to use network_mode: host, but it’s still not resolving .local addresses.
Anyone come across this before and any ideas how to resolve it?
Thanks

2 Likes

This helped me fix mdns in ESPHome container : Using mDNS From a Docker Container | by Andrej Taneski | Medium

services:
  esphome:
    container_name: esphome
    image: ghcr.io/esphome/esphome
    volumes:
      - ./esphome/config:/config
      - /etc/localtime:/etc/localtime:ro
      - /etc/resolv.conf:/etc/resolv.conf:ro
      - /etc/nsswitch.conf:/etc/nsswitch.conf:ro
      - /var/run/dbus:/var/run/dbus
      - /var/run/avahi-daemon/socket:/var/run/avahi-daemon/socket
    restart: "no"
    privileged: true
    network_mode: host
    dns:
      - 127.0.0.1
    environment:
      - USERNAME=test
      - PASSWORD=ChangeMe
    devices:
      - "/dev/ttyUSB0:/dev/ttyUSB0"
    ports:
      - 6052:6052

I’m going to paste this around to a few other related threads, because I burnt a whole day trying to figure this out. Hope this helps some of you.

I think I’ve fix this issue for me. If you use Adguard or some other non-conventional DNS server, this might be similar to your situation too.

Summary:
Having something like avahi-browse -ar do discovery and spit out a list of found devices and their hostname+IP address, is completely separate from a tool like ping or curl or whatever that completes mostly standard DNS resolution to figure out the IP address to conga-drums.local. Either if the container isn’t setup to look up mDNS first before calling a DNS server (which neither HA or the ESPHome containers do) or your DNS server doesn’t know about any .local hostnames, regardless of if stuff in that container can find out about devices via avahi-browse -ar, none of the other components part of that service will be able to resolve the IP address and talk with them.

My fix was to spin dnsmasq back up on a different port than 53 with only offering up resolution for .local domains, and then add that to the “Upstream DNS servers” list in Adguard (for specific domains). Once that was done all of my containers could resolve conga-drums.local.

I’ve got a lengthier post going into more details at the tail end of this HA bug.