Docker container and host communication: source behaves oddly
TL;DR Why can't my Docker container read host topics? And why is sourcing behaving in strange ways?
I am trying to launch a Docker
container on a host running ROS2 Foxy
on Ubuntu 20.04
with the default-shipped DDS. The host has ROS nodes running that publish to topics and define services (duh). I want to subscribe to some of the topics and interact with some of the services. I ran into an issue with using host networking and shared memory, and it seemed to expose some oddities with the initial source /opt/ros/foxy/setup.bash
command when I was troubleshooting. I want to be able to launch a simple docker container to read these topics over shared memory
(so not UDP if possible as the containers are on the same host) and not break anything running on the host, and upgrading ROS to a newer distro is not feasible. Some non-breaking upgrades are possible if needed, but I don't know what I might need there. I'm looking for guidance which might help others too. Sorry if this gets long-winded, but I tried to make sure I looked under every rock I know before asking the community.
I ended up launching a vanilla ROS2 Foxy deployment on an EC2 instance to test the issues I am seeing, with the following permutations. I am using the unmodified osrf/ros:foxy-desktop
docker image and the host is running the latest ROS2 foxy-desktop distro (installed yesterday when I was testing). All host publishers are the following:
# in a host terminal
source /opt/ros/foxy/setup.bash
ros2 topic pub /chatter std_msgs/String "data: Hello ROS Developers"
In each example, I start with fresh terminals and/or container runs, and sometimes I noticed I had to reboot the host.
Example 1: publishing a standard msg on /chatter
from a host terminal, reading message from a subscriber on another host terminal
This works as expected, sourcing the secondary host terminal, then ros2 topic list
and ros2 topic echo /chatter
work as expected.
Example 2: publishing a standard msg on /chatter
from a host terminal, reading message from a subscriber on a docker container terminal with network=host
My host terminal publishes messages, but I can't list the topics nor subscribe to them
Example 3: publishing a standard msg on /chatter
from a host terminal, reading message from a subscriber on a docker container terminal with network=host
after listing the topics from another host terminal
# in a second host terminal
source /opt/ros/foxy/setup.bash
ros2 topic list
If i execute this on a second host terminal before doing so on the container's terminal, I can then list ros topics on the container's terminal. I can event see all the topic metadata in the container with ros2 topic info /chatter --verbose
, but I can't subscribe to the topic. It just waits for messages indefinitely though the second host terminal prints out the messages.
A secondary ...