How do I advertise AND browse mDNS from within docker container?

Viewed 19103

I'm trying to create a ubuntu 17.04 based docker container that can browse mDNS on my network (outside of the docker network) AND advertise on mDNS to my network (outside of docker network).

I want to be able to run this docker container on a macOS host (during my development) AND a Linux (Debian) host for production.

https://github.com/ianblenke/docker-avahi seems to have solved this for Linux hosts (utilizing avahi daemon and mapping the /var/run/dbus volume to the host). When I'm developing on my macbook, I would like to use mDNSResponder.

How do I create a container that can advertise and browse on my local network, that will also run on my macOS laptop and on a Linux server?

Here is what I have so far.

Dockerfile

FROM ubuntu:17.04    
WORKDIR /app

RUN apt-get update && apt-get install -yq avahi-daemon avahi-utils libnss-mdns \
  && apt-get -qq -y autoclean \
  && apt-get -qq -y autoremove \
  && apt-get -qq -y clean

RUN update-rc.d avahi-daemon enable

COPY docker/etc/nsswitch.conf /etc/nsswitch.conf
COPY docker/etc/avahi-daemon.conf /etc/avahi/avahi-daemon.conf

COPY docker/start.sh /app    

CMD ["/bin/bash","start.sh"]

start.sh

#!/bin/bash

service avahi-daemon restart
service avahi-daemon status
avahi-browse -a

nsswitch.conf

hosts: files mdns_minimal [NOTFOUND=return] dns

avahi-daemon.conf

...
enable-dbus=no
...

Running

docker run --net=host -it mdns1
 * Restarting Avahi mDNS/DNS-SD Daemon avahi-daemon                      [ OK ]
Avahi mDNS/DNS-SD Daemon is running
Failed to create client object: Daemon not running

As you can see avahi-daemon is running, but avahi-browse doesn't think it is. Is this because I disabled dbus?

Running the same commands (except I keep enable-dbus=yes) inside a 17.04 virtualbox image on my mac things work just fine.

Update: it looks like you can not do bridged networking on a macOS host. So is what I am trying to do impossible?

2 Answers

For mdns advertising/listening we run dnssd inside docker containers.

But! In order to be discoverable on a local network the docker container should have an IP address from the network, proper routes from the network to docker container should be configured.

If you do not have control over the default router of the network, you can try to use macvlan/ipvlan network driver. It will allows you to assign multiple mac/IP addresses on the same network interface.

In our case, the network is wifi, so we had to use the ipvlan, because macvlan does not works with wifi. In a wired case you should prefer macvlan.

Related