Sample of broadcasting UDP messages inside the Docker network and receiving them with:
- Ash shell, PHP7, PHP8-alpha and Python3.
- Docker
- Tested version: v19.03.8
- docker-compose
- Tested version: v1.25.5
Build the containers and run with docker-compose
.
$ # Build/create containers
$ docker-compose build
...
$ # Starts the containers
$ docker-compose up
...
What you will see
After running the docker-compose up
, as soon as the sender
containers are up
they will start broadcasting a message. Also the receiver
containers will listen to the broadcasting message as soon as they are up
and echoes the received message.
$ docker-compose up
Starting receiver-php8 ... done
Starting receiver-php7 ... done
Starting receiver-py3 ... done
Starting receiver-ash ... done
Starting sender-py3 ... done
Starting sender-ash ... done
Starting sender-php7 ... done
Starting sender-php8 ... done
Attaching to receiver-php8, receiver-php7, receiver-ash, receiver-py3, sender-php8, sender-py3, sender-php7, sender-ash
receiver-php7 | Msg received: Count:0 Broadcasting meow meow from sender-ash : receiver-php7
receiver-php8 | Msg received: Count:0 Broadcasting meow meow from sender-ash : receiver-php8
receiver-php8 | Msg received: Count:0 Broadcasting meow meow from sender-php7 : receiver-php8
receiver-php7 | Msg received: Count:0 Broadcasting meow meow from sender-php7 : receiver-php7
receiver-php8 | Msg received: Count:0 Broadcasting meow meow from sender-php8 : receiver-php8
receiver-ash | Msg received: Count:0 Broadcasting meow meow from sender-ash : receiver-ash
receiver-ash | Msg received: Count:0 Broadcasting meow meow from sender-php7 : receiver-ash
receiver-ash | Msg received: Count:0 Broadcasting meow meow from sender-php7 : receiver-ash
receiver-ash | Msg received: Count:0 Broadcasting meow meow from sender-php8 : receiver-ash
receiver-php7 | Msg received: Count:0 Broadcasting meow meow from sender-php8 : receiver-php7
receiver-php7 | Msg received: Count:0 Broadcasting meow meow from sender-py3 : receiver-php7
receiver-php8 | Msg received: Count:0 Broadcasting meow meow from sender-py3 : receiver-php8
receiver-ash | Msg received: Count:0 Broadcasting meow meow from sender-py3 : receiver-ash
receiver-php8 | Msg received: Count:1 Broadcasting meow meow from sender-php7 : receiver-php8
receiver-ash | Msg received: Count:1 Broadcasting meow meow from sender-php7 : receiver-ash
receiver-php7 | Msg received: Count:1 Broadcasting meow meow from sender-php7 : receiver-php7
receiver-php7 | Msg received: Count:1 Broadcasting meow meow from sender-php8 : receiver-php7
receiver-ash | Msg received: Count:1 Broadcasting meow meow from sender-php8 : receiver-ash
receiver-php8 | Msg received: Count:1 Broadcasting meow meow from sender-php8 : receiver-php8
receiver-php7 | Msg received: Count:1 Broadcasting meow meow from sender-ash : receiver-php7
receiver-php8 | Msg received: Count:1 Broadcasting meow meow from sender-ash : receiver-php8
receiver-ash | Msg received: Count:1 Broadcasting meow meow from sender-ash : receiver-ash
receiver-php7 | Msg received: Count:1 Broadcasting meow meow from sender-py3 : receiver-php7
receiver-ash | Msg received: Count:1 Broadcasting meow meow from sender-py3 : receiver-ash
receiver-php8 | Msg received: Count:1 Broadcasting meow meow from sender-py3 : receiver-php8
...
There are 2 types of Docker containers; sender
and receiver
.
The sender
container sends a message and the receiver
container receives the message and echoes it. Simple as that.
To send and receive messages, both types of containers follows the same rules. Such as: Same TCP/IP protocol, data type, broadcast address and port.
The sender
container broadcasts a message to all the containers in the same Docker network.
- Msg to echo : "
Count:[counter] Broadcasting meow meow from [sender name]
"sender name
: Name of the sender container specified byenvironment
directive in "docker-compose.yml" asMY_HOST
.
- Msg frequency : Every 3 seconds which is specified in "ENV_FILE" file and given as a env variables to the container.
- Sample Alpine Linux containers for implementation:
- /sender/bash/
- /sender/php7/
- /sender/php8/
- /sender/python3/
- See the
README.md
in each dir for the details
The receiver
container listens to the broadcast messages and echoes the received message.
- Message to echo : "
Msg received: [received msg] : [receiver name]
"receiver name
: Name of the sender container specified byenvironment
directive in "docker-compose.yml" asMY_HOST
.
- Sample Alpine Linux containers for implementation:
- /receiver/bash/
- /receiver/php7/
- /receiver/php8/
- /receiver/python3/
- See the
README.md
in each dir for the details
Each container follows the below rules to send/receive the message.
- TCP/IP Protocol Type:
UDP
- Message Type : Datagram
- Target Host : "255.255.255.255" which doesn't specify the target host thus works as broadcasting.
- Target Port : 6666 which is specified in "ENV_FILE" as well.
- Note that sending data as
datagram
viaUDP
is fast and lightweight but doesn't ensure if the messages were sent correctly to each host.
- Note that sending data as