ROS 2 with Docker [Part 1]

ROS 2 and it’s GUI with docker containers and GIT

Divyanshu Raj
6 min readJan 3, 2023
Photo by Caleb Russell on Unsplash

This is a part 1 of tutorial series, on how to setup and run ROS 2 (humble) within docker containers along with GUI display for rqt_graphs, etc. This will directly pull and push the code to GitHub. We will not dig deeper into ROS 2 itself for this part, but focus on the setup. This will run on any OS with a working docker.

Contents

  • Installation
  • Docker for noVNC (for GUI display)
  • Docker for ROS 2 (Humble)
  • Drawing Circle with TurtleSim

Installation

Docker: Install docker and make sure that docker is up and running and has been tested with “hello-world” image.

https://docs.docker.com/get-docker/ follow this link for installation.

For windows, WSL (Windows Subsystem for Linux) is required. You can use this link for installation: https://learn.microsoft.com/en-us/windows/wsl/install

After installation, run this command:

docker run hello-world

This will print a message, which means it is installed and running. If not, please debug and make sure you have a running docker.

GitHub: Have a github profile and fork this repository. Make sure it is public and copy the link for cloning it.

Docker for noVNC

We can use noVNC, a remote-desktop web client, to visualize the GUIs of ROS applications in a browser.

First of all we need to create a Docker network that our containers can use to communicate. Let’s create a Docker network called ros:

docker network create ros

Set up noVNC:

Thanks to this project, we can launch noVNC in a Docker container using the Docker image theasp/novnc:latest. Download the image:

docker pull theasp/novnc:latest

Then run this image in a new container:

docker run -d --rm --net=ros \
--env="DISPLAY_WIDTH=3000" \
--env="DISPLAY_HEIGHT=1800" \
--env="RUN_XTERM=no" \
--name=novnc -p=8080:8080 \
theasp/novnc:latest

noVNC should now be running as a web application inside the container and listening on port 8080. Since we have mapped that port to 8080 on the host, we should be able to see the noVNC interface at http://<host name>:8080/vnc.html . For example, if the host is our local machine then that will be http://localhost:8080/vnc.html . Open this in a modern web browser (not IE) and click the Connect button. You should see see a blank desktop, something like this:

noVNC browser view (image by Author)

When we run the GUI command in ros2, we can come back to this screen and click on “Connect” to view the GUI.

Docker for ROS 2 Humble

We will create a Dockerfile to define the environment:

We have used the image from docker hub “osrf/ros:humble-desktop-full” as the base image, on top of which we have installed:

  • git: to pull and push code directly from container
  • pip3: to resolve a dependency issue
  • vim: to run a code editor inside container

After the installation is complete, we have cloned a git repository and set user email and name. In your case, you would fork the given repository and put your git clone link, mail and user. When the container is opened in interactive mode, we can write code and commit to github before we exit the container.

We have added a ENV variable to connect to noVNC for the GUI display.

Let’s build the image with tag as “draw_circle”. You need to be in the file path where above Dockerfile is present.

docker build -t draw_circle .

Once it is successfully built, we will launch the container in interactive mode. If you face issues with Windows (Docker Desktop), change the config:

Docker Desktop → SettingsDocker Engine → change the “features”: { buildkit: true} to “features”: { buildkit: false}.

docker run -it --net=ros draw_circle bash

In this command:

  • -it means interactive mode,
  • --net=ros is to connect to docker network that we created named “ros”
  • draw_circle is the tag name of image
  • bash is for getting a terminal to execute commands

After the execution of above command, you will enter into the container in interactive mode.

docker interactive mode (image by Author)

Every time we enter into interactive mode, we will execute the below 2 statements:

source /opt/ros/humble/setup.sh
source /experiments/ros_experiment_1_ws/install/setup.sh

This allows the “ros2” command to be executable.

We are all set now!

Drawing Circle with TurtleSim

We will open 3 terminals connected to the same container ID. To do this, start a interactive container, then open another terminal and fetch the ID using this command:

docker ps -l

This will display all the containers running along with the ID’s. We need to look for the container with IMAGE name as “draw_circle”.

List of running containers (image by Author)

Once we have the container ID, we will connect to the container using this command:

docker exec -it <Container_ID> bash

Once connected, we will run these 2 commands again:

source /opt/ros/humble/setup.sh
source /experiments/ros_experiment_1_ws/install/setup.sh

Similarly we want to have 3 terminals running, one for publisher, one for subscriber and one to run command to view rqt_graph.

Subscriber:

ros2 run turtlesim turtlesim_node

This will start the turtlesim node which is a subscriber, and when we click on the noVNC “Connect”, we can see a static turtle. In one of the terminal we will also run the command “rqt_graph” to view the flow.

Subscriber and rqt_graph (image by Author)
noVNC display for Turtlesim and rqt_graph (image by Author)

We can observe here, that the turtlesim node that we ran is a subscriber to the topic “/turtle1/cmd_vel”.

Publisher:

We will run the publisher that will publish to cmd_vel topic.

The code to draw circle is at this path:

/experiments/ros_experiment_1_ws/src/my_robot_controller/my_robot_controller/draw_circle.py

Let’s have a quick look at the code:

In the above code, we have set the timer for every 0.5 second to publish a message to the given topic, in the Twist format, to move a certain distance with linear and angular momentum.

The structure is created using ROS 2 workspaces. (No need to run this for now)

ros2 pkg create my_robot_controller --build-type ament_python --dependencies rclpy

We will build using colcon build:

cd /experiments/ros_experiment_1_ws/
colcon build --symlink-install

Once the build is successful, we can run the publisher using this command:

ros2 run my_robot_controller draw_circle
draw circle with Turtlesim (image by Author)

As we can see, the turtle is moving in circles. The rqt_graph shows, that draw_circle is publishing to the “/turtle1/cmd_vel” topic to which the turtlesim is subscribed.

These are the 3 terminals running publisher, subscriber and graph within same container:

3 terminal connected to same container in interactive mode (image by Author)

You can try to make changes to draw_circle file such that the turtle draws some other shape such as square, oval or some other patterns and test it out. Make sure to commit the code to github, before you exit the interactive terminal.

Don’t forget to kill all the container at the end. We will do more interesting stuff in the coming parts.

References

--

--

Divyanshu Raj

divraz.github.io Connect with me on LinkedIn @divyanshu-raj. Pursuing MS in Computer Science from ASU. I have an interest in ML and Design.