Multicast on Docker

Greetings everyone!

tl;dr:
How can i receive multicast messages of the host network inside an container?


I have recently moved my applications to docker and integrated an autmoatic build of images.

Now im having trouble with 1 of my applications.

The application is an node js app, that connects to an an mutlicast address:

this.broadcastSocket = createSocket({ type: ‘udp4’, reuseAddr: true });
this.broadcastSocket.on(‘listening’, () => {
this.broadcastSocket.addMembership(‘239.255.255.250’);
this.msearch();
});

Purpose of this app/piece of code is to find any upnp devices(medirender etc,) on my network.

Now as i moved to docker, obviously it didnt work, cuz the container run in their own network.

It works if i use “network_mode=host”, but then i have to use it everywhere on my compose file.
This is my current compose file:

version: "3.7"

services:
  redis:
    container_name: redis
    image: redis:latest
    restart: always
    network_mode: host
    volumes:
      - ./redis_data:/data

  mqtt:
    container_name: mqtt
    image: eclipse-mosquitto:latest
    network_mode: host
    environment:
      - TZ=Europe/Berlin
    volumes:
      - ./mosquitto.conf:/mosquitto/config/mosquitto.conf
      - ./mqtt/data:/mosquitto/data
      - ./mqtt/log:/mosquitto/log/mosquitto.log

  database:
    container_name: database
    image: mariadb:latest
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=webapp
      - MYSQL_USER=webapp
      - MYSQL_PASSWORD=webapp
      - MYSQL_DATABASE=homecontrol
    network_mode: host
    volumes:
      - ./database:/var/lib/mysql

  # This doesnt work because the default port of phpmyadmin(80) interferes with the port of the "frontend" service
  # phpmyadmin:
  #   container_name: phpmyadmin
  #   image: phpmyadmin:latest
  #   restart: always
  #   network_mode: host
  #   environment:
  #     - PMA_HOST=database
  #   depends_on:
  #     - database

  upnpControl:
    container_name: upnpControl
    image: 192.168.178.39:5000/sf/upnp_control:latest
    network_mode: host

  backend:
    container_name: backend
    image: 192.168.178.39:5000/sf/backend:latest
    depends_on:
      - database
      - redis
      - upnpControl
      - mqtt
    network_mode: host

  frontend:
    container_name: frontend
    image: 192.168.178.39:5000/sf/frontend:latest
    depends_on:
      - backend
    network_mode: host

Downside is that i cant define the ports on my own and i cant refeer to services by ther name, only by localhost or the ip address of my host, which may change depending on the enviroment.

My prefered compose file would look nearly like this:

version: "3.7"

services:
  redis:
    container_name: redis
    image: redis:latest
    restart: always
    ports:
      - 6379:6379
    volumes:
      - ./redis_data:/data

  mqtt:
    container_name: mqtt
    image: eclipse-mosquitto:latest
    ports:
      - 1883:1883
      - 9001:9001
    environment:
      - TZ=Europe/Berlin
    volumes:
      - ./mosquitto.conf:/mosquitto/config/mosquitto.conf
      - ./mqtt/data:/mosquitto/data
      - ./mqtt/log:/mosquitto/log/mosquitto.log

  database:
    container_name: database
    image: mariadb:latest
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=webapp
      - MYSQL_USER=webapp
      - MYSQL_PASSWORD=webapp
      - MYSQL_DATABASE=homecontrol
    ports:
      - 3306:3306
    volumes:
      - ./database:/var/lib/mysql

  phpmyadmin:
    container_name: phpmyadmin
    image: phpmyadmin:latest
    restart: always
    ports:
      - 8080:80
    environment:
      - PMA_HOST=database
    depends_on:
      - database

  upnpControl:
    container_name: upnpControl
    image: 192.168.178.39:5000/sf/upnp_control:latest
    ports:
      - 18423:18423

  backend:
    container_name: backend
    image: 192.168.178.39:5000/sf/backend:latest
    depends_on:
      - database
      - redis
      - upnpControl
      - mqtt
    ports:
      - 3000:3000

  frontend:
    container_name: frontend
    image: 192.168.178.39:5000/sf/frontend:latest
    depends_on:
      - backend
    ports:
      - 80:80

Note that i removed the “network_mode” and added some ports for the applications. Problematic is the “upnpControl” service. Like i already mentioned, it cant recieve any multicast messages.

  1. How can i get Multicast messages inside my container?

  2. Or how do i connect 1 container running in network host mode with the others, which are in default network?

I tried to setup 2 networks:
1.host - basically the host machine
2.default - custom network

and tried to give the upnp service both networks but that didnt work.

Only solution currently is to give everything “network_mode: host”, but im pretty sure there must be another way.

Any hint, help or ideas would be appreciated :slight_smile:

EDIT:
Machine: Raspberry Pi 3 with Raspian 64 Bit
Docker Version: 18.09.1 - 4c53b90
Compose Version: 1.28.6