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

rynop picture rynop · May 19, 2017 · Viewed 13.8k times · Source

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?

Answer

Michal picture Michal · May 25, 2017

I'm currently trying to get avahi working inside a docker container and in my research came across this:

you can in the Avahi settings configuration disable dbus so it won't use it. Then when you run Avahi in Docker you must pass it the --no-rlimits flag and it'll work without compromising your containers security.

https://www.reddit.com/r/docker/comments/54ufz2/is_there_any_way_to_run_avahi_in_docker_without/

Hopefully this can help with your situation.