7

i am writing a python application that is sending continously UDP messages to a predefined network with other hosts and fixed IPs. I wrote the python application and dockerized it. The application works fine in the docker, no problems there.

Unfortunately i am failing to send the UDP messages from my docker to the host so they will be sent to the other hosts in the network. The same is for receiving messages. Right now i dont know how to set up my docker so it is receiving a UDP message from a host with fixed IP adress in the network.

I tried to set up my docker network with --net host and i sent all the UDP messages from my docker container via localhost to my host. This worked fine, too. I am missing the link where i can sent the messages no to the "outside world". I tried to make a picture of my problem.

Docker host communication problem

My Question: How do i have to set up the network communcation for my docker/host so it can receive messages via UDP from other hosts in the network? Thanks

bumbumquietsch
  • 153
  • 2
  • 10
  • so you want to send messages only for `1.0.0.X` network right? – Mayur Jan 25 '19 at 07:09
  • Yes thats correct. Its a predefined Network with fixed IPs and the docker container will just receive messages from this network and send messages to this network. – bumbumquietsch Jan 25 '19 at 11:07
  • okay, then instead of `--net host` you can create bridge connection for `1.0.0.X` network only so that it will get accessible inside docker container – Mayur Jan 25 '19 at 11:25

2 Answers2

3

So i experimented a lot and i figured out, that i just need to run the docker container with the network configuration as host. The UDP socket in my container is bound to the IP adress of my host and therefore just needs to be linked to the Network of the host. Everyone who is struggeling the same issue, just run

docker run --network=host <YOURCONTAINER>
bumbumquietsch
  • 153
  • 2
  • 10
  • Note, that this only works on Linux hosts, according to Docker's documentation: https://docs.docker.com/network/host/#:~:text=The%20host%20networking%20driver%20only%20works%20on%20Linux%20hosts%2C%20and%20is%20not%20supported%20on%20Docker%20Desktop%20for%20Mac%2C%20Docker%20Desktop%20for%20Windows%2C%20or%20Docker%20EE%20for%20Windows%20Server. – flowest Aug 19 '22 at 13:34
1

Build your own bridge

1.Configure the new bridge.

$ sudo ip link set dev br0 up

$ sudo ip addr add 192.168.5.1/24 dev bridge0

$ sudo ip link set dev bridge0 up

Confirm the new bridge’s settings.

$ ip addr show bridge0

4: bridge0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state UP group default
    link/ether 66:38:d0:0d:76:18 brd ff:ff:ff:ff:ff:ff
    inet 192.168.5.1/24 scope global bridge0
       valid_lft forever preferred_lft forever <br/>       

2. Configure Docker to use the new bridge by setting the option in the daemon.json file, which is located in /etc/docker/ on Linux or C:\ProgramData\docker\config\ on Windows Server. On Docker for Mac or Docker for Windows, click the Docker icon, choose Preferences, and go to Daemon.

If the daemon.json file does not exist, create it. Assuming there are no other settings in the file, it should have the following contents:

{
  "bridge": "bridge0"
}

Restart Docker for the changes to take effect.

3. Confirm that the new outgoing NAT masquerade is set up.

    $ sudo iptables -t nat -L -n

    Chain POSTROUTING (policy ACCEPT)
    target     prot opt source               destination
    MASQUERADE  all  --  192.168.5.0/24      0.0.0.0/0

4.Remove the now-unused docker0 bridge.

$ sudo ip link set dev docker0 down

$ sudo ip link del name br0

$ sudo iptables -t nat -F POSTROUTING

5.Create a new container, and verify that it is in the new IP address range.

(ref.)

Mayur
  • 2,583
  • 16
  • 28
  • Hey, thanks for the answer. First of all, the code `sudo ip link set dev br0 up` does not work because there is no device called `br0`. I can create it with `ip link add name br0 type bridge` but i dont understand the difference between `br0` and `docker0` – bumbumquietsch Jan 28 '19 at 13:30
  • actually `br0` is for bridge network and `docker0` is for host to container NAT – Mayur Jan 28 '19 at 14:00
  • Yes, i got this. But did you try the command by yourself? `sudo ip link set dev br0 up` is not working! – bumbumquietsch Jan 28 '19 at 14:45
  • You can refer reference link for detailed information. – Mayur Jan 28 '19 at 15:29
  • Thanks again. I saw the reference, it is the same options you displayed here. But the command is not working. I think have to create `br0` first with this command: ` sudo ip link add name br0 type bridge ` – bumbumquietsch Jan 28 '19 at 15:45
  • yes, you need to create `br0` which is your host os? – Mayur Jan 28 '19 at 15:46