问题
I've been trying to enable some UDP discovery between a few containers. It tuned out that containers have disabled broadcasts by default, missing brd for inet in:
$ ip addr show dev eth0
27: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 00:00:01:4f:6a:47 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.12/16 scope global eth0
valid_lft forever preferred_lft forever
Stack:
- host: ubuntu 14.04
- container: ubuntu 12.04
- docker 1.8.3
How do I enable the broadcasts? Here's what I've tried so far:
ip link set dev eth0 broadcast 172.17.255.255
givesRTNETLINK answers: Invalid argument
- same with --privileged container
- same with NET_ADMIN and NET_BROADCAST container capabilities
回答1:
As of now (Docker 18.06+) UDP broadcasts work out of the box, as long as your are using the default bridge network and all containers run on the same host (and of course in the same docker network).
Using docker-compose services are automagically run in the same network and thus the following docker-compose.yml
:
version: '3.4'
services:
master-cat:
image: alpine
command: nc -l -u -p 6666
slave-cat:
image: alpine/socat
depends_on:
- master-cat
entrypoint: ''
command: sh -c "echo 'Meow' | socat - UDP4-DATAGRAM:255.255.255.255:6666,so-broadcast"
with docker-compose up
will show Meow on the master-cat (sic!).
If you want to use broadcasts across multiple hosts, this is not possible with the default network plugins that docker ships with. -> https://github.com/moby/moby/issues/17814. But a more sophisticated overlay network plugin, such as Weave should work (I have not tested it...)
来源:https://stackoverflow.com/questions/33379892/enable-broadcasts-between-docker-containers