cheny163
cheny163

Reputation: 11

Docker containers with ros2 unable to talk to each other

I'm playing around with docker containers with ROS galactic with different network setup but I'm unable to get robot_container_1 and robot_container_2 to talk to each other.

I have robot_container_1 on two networks, rob_net and dev_net, making it kind of like a network hub. And then I have robot_container_2 on rob_net and robot_container_3 on dev_net. Container 2 and 3 should be isolated from each other since they are on different networks but container 1 should be able to communicate with 2 and 3. However, for some reason container 2 is not hearing the topics that gets published from container 1 (container 3 is hearing fine).

I tried pinging container 1 from container 2 and vice versa and was able to confirm that they can ping each other.

Not sure why this is happening...

Any help is appreciated!

Dockerfile:

# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.234.0/containers/ubuntu/.devcontainer/base.Dockerfile
# [Choice] Ubuntu version (use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon): ubuntu-22.04, ubuntu-20.04, ubuntu-18.04
#ARG VARIANT="jammy"
#FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
FROM osrf/ros:galactic-desktop

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
#     && apt-get -y install --no-install-recommends <your-package-list-here>

RUN echo "source /opt/ros/galactic/setup.bash; echo ROS environment sourced" >> /home/.bashrc

docker-compose.yml:

version: '3'

networks:
  rob_net:
    name: robot_net
  dev_net:
    name: custom_net

services:
  robot_container_1:
    build: ./
    command: ros2 run demo_nodes_cpp talker
    networks:
      - rob_net
      - dev_net

  robot_container_2:
    build: ./
    command: ros2 run demo_nodes_cpp listener
    networks:
      - rob_net

  robot_container_3:
    build: ./
    command: ros2 run demo_nodes_cpp listener
    networks:
      - dev_net

Output in terminal:

Starting robot_machine_robot_container_3_1 ... done
Starting robot_machine_robot_container_2_1 ... done
Starting robot_machine_robot_container_1_1 ... done
Attaching to robot_machine_robot_container_3_1, robot_machine_robot_container_2_1, robot_machine_robot_container_1_1
robot_container_1_1  | [INFO] [1653500334.977882081] [talker]: Publishing: 'Hello World: 1'
robot_container_3_1  | [INFO] [1653500334.978566582] [listener]: I heard: [Hello World: 1]
robot_container_1_1  | [INFO] [1653500335.977827578] [talker]: Publishing: 'Hello World: 2'
robot_container_3_1  | [INFO] [1653500335.978318044] [listener]: I heard: [Hello World: 2]
robot_container_1_1  | [INFO] [1653500336.977837587] [talker]: Publishing: 'Hello World: 3'
robot_container_3_1  | [INFO] [1653500336.978320811] [listener]: I heard: [Hello World: 3]
robot_container_1_1  | [INFO] [1653500337.977835219] [talker]: Publishing: 'Hello World: 4'
robot_container_3_1  | [INFO] [1653500337.978418682] [listener]: I heard: [Hello World: 4]
robot_container_1_1  | [INFO] [1653500338.977836173] [talker]: Publishing: 'Hello World: 5'
robot_container_3_1  | [INFO] [1653500338.978390953] [listener]: I heard: [Hello World: 5]
robot_container_1_1  | [INFO] [1653500339.977832089] [talker]: Publishing: 'Hello World: 6'
robot_container_3_1  | [INFO] [1653500339.978390637] [listener]: I heard: [Hello World: 6]
robot_container_1_1  | [INFO] [1653500340.977834528] [talker]: Publishing: 'Hello World: 7'
robot_container_3_1  | [INFO] [1653500340.978335281] [listener]: I heard: [Hello World: 7]
robot_container_1_1  | [INFO] [1653500341.977838098] [talker]: Publishing: 'Hello World: 8'
robot_container_3_1  | [INFO] [1653500341.978314358] [listener]: I heard: [Hello World: 8]
robot_container_1_1  | [INFO] [1653500342.977831499] [talker]: Publishing: 'Hello World: 9'
robot_container_3_1  | [INFO] [1653500342.978335463] [listener]: I heard: [Hello World: 9]
robot_container_1_1  | [INFO] [1653500343.977838141] [talker]: Publishing: 'Hello World: 10'
robot_container_3_1  | [INFO] [1653500343.978360043] [listener]: I heard: [Hello World: 10]
robot_container_1_1  | [INFO] [1653500344.977782701] [talker]: Publishing: 'Hello World: 11'
robot_container_3_1  | [INFO] [1653500344.978188119] [listener]: I heard: [Hello World: 11]
robot_container_1_1  | [INFO] [1653500345.977757730] [talker]: Publishing: 'Hello World: 12'
robot_container_3_1  | [INFO] [1653500345.978233287] [listener]: I heard: [Hello World: 12]
robot_container_1_1  | [INFO] [1653500346.977751814] [talker]: Publishing: 'Hello World: 13'
robot_container_3_1  | [INFO] [1653500346.978220924] [listener]: I heard: [Hello World: 13]
robot_container_1_1  | [INFO] [1653500347.977699661] [talker]: Publishing: 'Hello World: 14'
robot_container_3_1  | [INFO] [1653500347.977952816] [listener]: I heard: [Hello World: 14]
robot_container_1_1  | [INFO] [1653500348.977798119] [talker]: Publishing: 'Hello World: 15'
robot_container_3_1  | [INFO] [1653500348.978270520] [listener]: I heard: [Hello World: 15]
robot_container_1_1  | [INFO] [1653500349.977750424] [talker]: Publishing: 'Hello World: 16'
robot_container_3_1  | [INFO] [1653500349.978302051] [listener]: I heard: [Hello World: 16]
robot_container_1_1  | [INFO] [1653500350.977773543] [talker]: Publishing: 'Hello World: 17'
robot_container_3_1  | [INFO] [1653500350.978304486] [listener]: I heard: [Hello World: 17]
robot_container_1_1  | [INFO] [1653500351.977724151] [talker]: Publishing: 'Hello World: 18'
robot_container_3_1  | [INFO] [1653500351.977892328] [listener]: I heard: [Hello World: 18]
robot_container_1_1  | [INFO] [1653500352.977775013] [talker]: Publishing: 'Hello World: 19'
robot_container_3_1  | [INFO] [1653500352.978231984] [listener]: I heard: [Hello World: 19]
robot_container_1_1  | [INFO] [1653500353.977815266] [talker]: Publishing: 'Hello World: 20'
robot_container_3_1  | [INFO] [1653500353.978300983] [listener]: I heard: [Hello World: 20]

Upvotes: 0

Views: 1392

Answers (2)

dhgarcia
dhgarcia

Reputation: 11

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/

Upvotes: 1

user20361369
user20361369

Reputation: 1

i am facing same issue now. With or without docker-compose. A simple test i do is running "ros2 topic list" which is empty. From forums i learned that solution can be running "ros2 daemon stop && ros2 daemon start". Works, BUT only for one container of the many - the topics of the second, third, etc. running images will not show up (running ros2 topic list on host). Not sure if this is my misunderstanding of docker principles. What i have now is following: foxy-ros-base -> myBaseImage -> image1 | image2

so running (docker run) the image1 and image2 in best case i can list the topics from image1 (by stopping and starting the ros2 daemon inside it) but not both. If i do same stopping and starting the daemon in image2 i will not be able to list the topics from image1.

Upvotes: 0

Related Questions