Python code using ROS in a Docker container

Asked

Viewed 18 times

0

I am a beginner with ROS and am trying to run some Node Listener/talker running a really simple code in python, using ROS in a Docker container.

I am following the guide described in: https://hub.docker.com/_/Ros

I have this structure:

./
    Dockerfile
    docker-compose.yml
    ./teste/
            listener.py

My current goal is to pass the code Listener.py and run in a Node Listener, running as a service container.

My Dockerfile is:

FROM ros:foxy        
 # install ros package 
    RUN apt-get update && apt-get install -y \
              ros-${ROS_DISTRO}-demo-nodes-cpp \
              ros-${ROS_DISTRO}-demo-nodes-py && \
            rm -rf /var/lib/apt/lists/*
        
# launch ros package

CMD ["ros2", "launch", "demo_nodes_cpp", "talker_listener.launch.py"]

My file Docker-Compose.yml is:

version: '3'

services:
  talker:
    build: ./
    command: ros2 run demo_nodes_py talker

  listener:
    build: ./
    environment:
      - "PYTHONUNBUFFERED=1"
    volumes:
      - ./teste:/teste
    command: ros2 run demo_nodes_py listener

volumes:
  teste:

With this standard code, the two services run the Listener/talker of the original Ros examples, but I want to run my own python code. How do I do that? Even if the listener service can access the Listener.py code (via ./test) if I open a shell and try to run it, the ros2 run command is not recognized.

The ros2 command is not recognized if I open a terminal, but by the cmd command in Dockerfile, with the code talker_listener.launch.py, it works.

I was able to run the setup.sh via "/bin/bash source /opt/Ros/Foxy/setup.bash". Still, I cannot execute the ros2 commands. I used "python3 /test/Listener.py", but I got the bug:

ModuleNotFoundError: No module named 'rospy'

As this is possible, if when I run the default command option (ros2 run demo_nodes_py Listener, via Dockerfile) and it works?

What am I doing wrong? Should I do something first to make my python code work in ros2? Any ideas?

Thank you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.