Running python code on ROS + Docker
Hello everyone.
I'm begginer with ROS, and i'm trying to run some listener/talker node running a really simple code in python, using ROS on a Docker container.
I'm following the guide described in: https://hub.docker.com/_/ros
I got this structure:
./
Dockerfile
docker-compose.yml
./teste/
listener.py
My current goal is to pass the listener.py code and run in a "listener" node, 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 docker-compose.yml file 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 default code, the two services runs the listener/talker from the original ros examples, but i want to run my own python code. How do i do that? Even that the listener service can access listener.py code (via ./teste) if i open an shell and try to execute it, the ros2 run command is not recognized.
Any ideas?
Thanks!
Edit: ros2 command is not recognized if i open a shell terminal, but the by the cmd command on the Dockerfile, with the talker_listener.launch.py code, it works. Also, if i try to run python /teste/listener.py on the shell terminal, the python command is also not recognized.
Edit2: i managed to run the setup.sh via "/bin/bash source /opt/ros/foxy/setup.bash". Even so, i can't run the ros2 commands. I used "python3 /teste/listener.py", but then i got the error:
ModuleNotFoundError: No module named 'rospy'
How is that possible, if when i run the default command option (ros2 run demo_nodes_py listener, via Dockerfile) and it works?