mDNS in HA docker possible?

I recently migrated my python installation to docker, I use mDNS everywhere and noticed that mpd and few other integrations are broken now.

From what I understand the current mDNS situation in HA Docker is the following:

  • HA uses its own mDNS library in many integrations
  • HA Docker is based on Alpine Linux which doesn’t seem to have mDNS out of the box, there is no avahi or anything else on the docker
  • Integrations like commands or mpd do seem to rely on system name resolution not its own mDNS library - and so far these are all failing on my system
  • Even with network set to host, mDNS will fail in Alpine because Alpine doesn’t use dns glibc but uses musl instead so mdns_minimal in nsswitch.conf will not do anything

I got something working but it does really feel hacked

  • created on my host a virtual interface with an internal IP for DNS queries (I know this could be probably dockerized but I want to keep my docker running in network host mode)
  • I’ve setup dnsmasq to listen to all dns queries on my virtual IP+if
  • I am running avahi2dns on my virtual IP+if on port 5354
  • I added a rule in dnsmasq to forward all .local queries to avahi2dns to the 5354 port.
  • I setup my virtual ip as dns in docker

now my pings in the ha docker image to .local hosts are finally working. There are some intermittent issues but this is likely because of other ipv6 issues on my home network but ping -4 <host>.local works consistently

Macvlan is what you want

Creates virtual nic that is used by container with its on IP and MAC

MDNS is not DNS also.

correct but Alpine linux only supports dns through musl from their official doc, you need to hack dns to get mdns working

Can you explain more how to do it (docker compose)?

Repost from here

To be fair setting up macvlan gets complicated for those using Portainer. I honestly hadn’t realized this until I started rereading the docs posted above.

If using command line you can simply follow docker docs.

If using portainer there are actually 2 steps.

Step 1. Setup the network configuration to use the ethernet interface
step 2. create the network to use macvlan config setup in step1

STEP1
The portainer setup is pretty straigtforward and same settings of command line.
for me I have server network that uses IP range 192.168.10.1 - 224 and I want HA in this IP range so I setup docker macvlan config in portainer to use this

NAME: MacVlan_Config
DRIVER: Macvlan
Macvlan Configuration: configuration
PARENT NETWORK CARD: eth0
SUBNET: 192.168.10.0/24
IP RANGE: 192.168.10.20 -192.168.10.30
GATEWAY: 192.168.10.1

My DHCP does not assign within the IP RANGE I used. Docker will automatically assign the IP but when I create container I manually assign the IP. In either case I want to make sure docker and my router arent assigning same IPs so be to prevent this.
It is also possible to use IPV6 range

STEP2
You need to actually create the docker network that the containers will attach to and use.

NAME: MacVlan_Network
DRIVER: Macvlan
Configuration: MacVlan_Config

after this the network is ready for use like below
I define the mac address and IP for the container so it shows in my router properly and IP is static.


services:

##########################################
#           HOMEASSISTANT                #
##########################################
  hass:
    container_name: homeassistant
    hostname: homeassistant
    user: 1002:1002 # user:group must exist on host with desired permissions 
    mac_address: "02:42:0a:3c:1b:f1"
    privileged: false
    restart: unless-stopped
    stop_grace_period: 10s
    depends_on:
      - mariadb    
    image: ghcr.io/ghcr.io/home-assistant/home-assistant:2023.12.1
    volumes:
      - "/srv/cam/docker/frigate/application/media_frigate:/media/frigate:ro"
      - "/srv/main/docker/homeassistant/application/config:/config"
      - "/etc/localtime:/etc/localtime:ro"
    ports:
      - "8123:8123/tcp" # HA UI
      - "20165:21065/tcp" #
      - "5353:5353/udp" # mDNS
      - "51837:51827/udp" # homekit
    networks:
      dockerlocal:
      homeassistant:
      MacVlan_Net:
        ipv4_address: "192.168.10.15"
      reverseproxy:
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 4096M
        reservations:
          cpus: '1'
          memory: 1500M
          
##########################################
#              HASS_DB                   #
##########################################
  mariadb:
    image: mariadb:latest
    container_name: hass_db
    hostname: hass_db
    user: 1002:1002
    restart: "unless-stopped"
    stop_grace_period: 5s
    security_opt: # see https://github.com/MariaDB/mariadb-docker/issues/434#issuecomment-1136151239
      - seccomp:unconfined
      - apparmor:unconfined
    command: mariadbd --innodb-buffer-pool-size=512M --transaction-isolation=READ-COMMITTED --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --max-connections=512 --innodb-rollback-on-timeout=OFF --innodb-lock-wait-timeout=120
    ## Never store database files on an unreliable device such as a USB flash drive, an SD card, or a shared network folder:
    volumes:
      - "/srv/main/docker/homeassistant/application/var_lib_mysql:/var/lib/mysql" # DO NOT REMOVE
    environment:
      MARIADB_AUTO_UPGRADE: "1"
      MARIADB_INITDB_SKIP_TZINFO: "1"
      MARIADB_DATABASE: "homeassistant"
      MARIADB_USER: "homeassistant"
      MARIADB_PASSWORD: "homeassistant"
      MARIADB_ROOT_PASSWORD: "mariadb"
      PGID: 1002
      PUID: 1002
    networks:
      homeassistant:
    deploy:
      resources:
        limits:
          cpus: '2'
          memory: 2000M
        reservations:
          cpus: '1'
          memory: 512M

          
##########################################
#              NETWORKS                  #
##########################################      
networks:
  dockerlocal:
    external: true 
  homeassistant:
    driver: bridge
  MacVlan_Network:
    external: true
  reverseproxy:
    external: true

ADDED DETAIL
First time I run compose I do not set MAC and let docker create one. It’s not required I just honestly do know what to use so it’s easier for me to allow docker to create it. I add the created Mac to the compose file laterd

I use a static IP on my network. After the MAC is defined a reserve the static address on my router

With this the container looks like a server on my network

1 Like

thanks, I don’t use portainer and the docs give raise more questions than answers, for example should I use bridge v truncated bridge etc etc.

Thanks anyway :slight_smile:

edit: Just noticed that you were replying to me in your original post as well!