dockerros2

ROS2 humble docker container cannot communicate with host


I attempted to learn how to develop ROS2 in Docker, so I started with this tutorial: https://docs.ros.org/en/humble/How-To-Guides/Setup-ROS-2-with-VSCode-and-Docker-Container.html. Everything seemed fine until I tried to establish communication between a talker-listener pair running in separate Docker containers and the host machine. They couldn't see each other's topics. Here's my Dockerfile for reference.

FROM osrf/ros:humble-desktop
ARG USERNAME=jarunyawat
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Create the user
RUN groupadd --gid $USER_GID $USERNAME \
    && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
    #
    # [Optional] Add sudo support. Omit if you don't need to install software after connecting.
    && apt-get update \
    && apt-get install -y sudo \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
    && chmod 0440 /etc/sudoers.d/$USERNAME
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y python3-pip
ENV SHELL /bin/bash
ENV ROS_DOMAIN_ID 42
ENV ROS_LOCALHOST_ONLY 0
ENV RMW_IMPLEMENTATION=rmw_fastrtps_cpp

# ********************************************************
# * Anything else you want to do like clean up goes here *
# ********************************************************

# [Optional] Set the default user. Omit if you want to keep the default as root.
USER $USERNAME
CMD ["/bin/bash"]

build the image and run this command

docker run -it --network=host --ipc=host --pid=host ros2-dev-wat:lastest

In the host terminal, I ran the talker demo node, and in the container terminal, I attempted to list topics. However, the container could not detect the /chatter topic. MY ROS_LOCALHOST_ONLY is set to 0 and ROS_DOMAIN_ID is set to 42 in my host. So, i attempted to run above command with talker node and in the other terminal I run above command with talker node. It couldn't communicate with each other. It appears that the issue is related to the use of the Docker options --network=host --ipc=host --pid=host.

I expected the container to be able to communicate with the host.


Solution

  • It appears that the problem originates from Docker Desktop on Ubuntu. Upon removing Docker Desktop, everything functioned as expected. Furthermore, I discovered that there is no necessity to align the username in the container with that of the host for communication to occur; it can remain as root in the container. This entire section can be removed

    ARG USERNAME=jarunyawat
    ARG USER_UID=1000
    ARG USER_GID=$USER_UID
    
    # Create the user
    RUN groupadd --gid $USER_GID $USERNAME \
        && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
        #
        # [Optional] Add sudo support. Omit if you don't need to install software after connecting.
        && apt-get update \
        && apt-get install -y sudo \
        && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
        && chmod 0440 /etc/sudoers.d/$USERNAME
    

    and I simply disabled the firewall

    sudo ufw disable
    

    then, the container and host can communicate.