Search code examples
dockerubuntudockerfileubuntu-18.04ros

What is the best practice to start "roscore" followed by "rosrun ...." on Docker Container startup?


I have created through a Dockerfile a Docker image on which Ubuntu 18.04 and ROS Melodic are properly installed. Please note that I have not used the official ROS-Docker image for creating my Docker image.

The Docker container derived from this Docker image is working fine. However, every time I want to work with the container, I need to execute the following commands:

  1. Terminal window:

docker run -d -it --name container_name docker_image; docker exec -it container_name bash

Then, after I am within the Docker container: roscore

  1. Terminal window:

docker exec -it container_name bash

Then, after I am within the Docker container: rosrun ROS_PackageName PythonScript.py

Please note that through the above-mentioned Docker commands, both terminals operate in the same Docker container.

I find my way to start the PythonScript.py inefficient. Therefore, I would like to ask for the best practice to start "roscore" followed by "rosrun ...." on Docker Container startup.

In some Dockerfiles I see at the end of the file the command ENTRYPOINT as well as CMD. However, I do not know if these commands can help me to make my Docker container execute "roscore" followed by "rosrun" on container startup.


Solution

  • This is actually, in part, what roslaunch is for. It makes it easier to launch multiple nodes and nicer parameter input, but it will also start a roscore is one is not already running. In your example it would look something like this:

    <launch>
      <node pkg="ROS_PackageName" type="PythonScript.py" name="my_node_name" output="screen" >
      </node>
    </launch>
    

    When running roslaunch via CLI you would use it the same way you would rosrun. For example if you create the launch file in the same example package you listed and named the file my_launch.launch the command would look like: roslaunch ROS_PackageName my_launch.launch. Simply replace the rosrun command above with this.