带有 ros2 的 Docker 容器无法相互通信

问题描述 投票:0回答:2

我正在使用具有不同网络设置的 ROS gactic 的 docker 容器,但我无法让

robot_container_1
robot_container_2
相互交谈。

我在两个网络上有

robot_container_1
rob_net
dev_net
,使其有点像网络中心。然后我在
robot_container_2
上有
rob_net
,在
robot_container_3
上有
dev_net
。容器 2 和 3 应相互隔离,因为它们位于不同的网络上,但容器 1 应该能够与 2 和 3 通信。但是,由于某种原因,容器 2 听不到从容器 1(容器 3)发布的主题听力正常)。

我尝试从容器 2 ping 容器 1,反之亦然,并且能够确认它们可以互相 ping 通。

不知道为什么会发生这种情况...

如有任何帮助,我们将不胜感激!

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

终端输出:

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]
docker network-programming docker-compose data-distribution-service ros2
2个回答
0
投票

我现在面临同样的问题。有或没有 docker-compose。我做的一个简单测试是运行“ros2 topic list”,它是空的。从论坛我了解到解决方案可以运行“ros2 daemon stop && ros2 daemon start”。有效,但仅适用于众多容器中的一个 - 第二个、第三个等的主题。运行图像将不会显示(在主机上运行 ros2 主题列表)。不知道这是否是我对docker原理的误解。我现在拥有的如下: Foxy-ros-base -> myBaseImage -> image1 | 图片2

因此,在最好的情况下运行(docker run)image1和image2,我可以列出image1中的主题(通过停止和启动其中的ros2守护进程),但不能同时列出两者。如果我在 image2 中执行相同的停止和启动守护进程,我将无法列出 image1 中的主题。


0
投票

更快的解决方法是向容器添加共享内存

-v /dev/shm:/dev/shm

我引用

Fast-DDS 的最新版本默认带有共享内存传输。使用 --net=host 意味着两个 DDS 参与者都相信他们在同一台机器上,并且他们尝试使用 SharedMemory 而不是 UDP 进行通信

请参阅此处了解更多完整说明: https://answers.ros.org/question/370595/ros2-foxy-nodes-cant-communicate-through-docker-container-border/

© www.soinside.com 2019 - 2024. All rights reserved.