A recent project, we have to send TCP and UDP data from Host to Docker, back and forth. Docker is great and it is easy to expose port to provide service inside Docker instance. For example:
docker run -itd --name my_instance -p 1111:2222 -p 8888:9999/udp my_image
Here we map the TCP port 1111 from host to 2222 inside docker instance, and UDP port 8888 on host to 9999 in docker, and some help commands we use frequently such as
docker exec -it my_instance /bin/bash
Now we come to the TCP/UDP communication testing. Basically the command netcat/nc is good enough for make data coming through.
Normally this two pairs work perfectly for most testing purpse, just be careful for the port forwarding of docker. However there is an issue for macOS, and there is a workaround suggested by Docker offical sites. In short, use the command below to attach an unused IP to lo0 interface
sudo ifconfig lo0 alias 192.168.0.111/24