Home > Net >  Using "docker exec" to run ros2 commands
Using "docker exec" to run ros2 commands

Time:10-26

So i'm currently trying to write a shell script to execute many commands, and i have a certain ros2 commands that need to be run in a docker using bash. I saw that I may be able to use "docker exec" in order to run a command in a docker from a shell script but when i try and use docker exec with ros2 commands it doesn't seem to work.

I've tried...

sudo docker exec my_docker /bin/sh -c "ros2 node list"
/bin/sh: 1: ros2: not found
sudo docker exec my_docker /bin/bash -c "source /root/.bashrc && ros2 node list"
/bin/bash: ros2: command not found
sudo docker exec my_docker /bin/bash -c "./node_list.sh"
/bin/bash: ros2: command not found
./node_list.sh: line 4: ros2: command not found

node_list.sh file:

#!/bin/bash
source /root/.bashrc
#
ros2 node list
sudo docker exec my_docker /opt/ros/humble/bin/ros2 node list
OCI runtime exec failed: exec failed: unable to start container process: exec: "ros2": executable file not found in $PATH: unknown

CodePudding user response:

The reason for this problem is that your ROS installation is not sourced. Probably there is some issue with your bashrc file. Your local bashrc file does not work when calling a command within a docker container.

You can try to source your installation within the docker container fist and the run the desired command.

sudo docker exec my_docker /bin/bash -c ". /opt/ros/humble/setup.bash && ros2 node list"
  • Related