The faster way to solved is to add shared memory to the containers
-v /dev/shm:/dev/shm
I quote
the last releases of Fast-DDS come with SharedMemory transport by default. Using --net=host implies both DDS participants believe they are in the same machine and they try to communicate using SharedMemory instead of UDP
See here for more in full explanation: https://answers.ros.org/question/370595/ros2-foxy-nodes-cant-communicate-through-docker-container-border/