Rviz on docker

Dhanoop Karunakaran
Intro to Artificial Intelligence
3 min readAug 29, 2020
Rviz window[2]

Codebase: ROS Melodic

Prerequisite: install docker 19.03 or above

Docker container is really useful for the rapid prototypes or experiment without much worrying about the dependencies of the algorithms or conflict in the local os. I started using docker for my PhD experiments recently and it is extremely useful once it’s set up, we can run it anywhere where docker supports. This is really useful if we want to try someone’s code if they provide docker containers.

Majority of my work in the Robotic Operating System(ROS) and one of the main tool I am using is the standard 3D visualization tool called Rviz. The main challenge here is docker containers doesn’t come up with UI. This poses difficulty in running the GUI application in docker containers. This indeed a challenge in running rviz on docker.

Technically, XServer is essential for GUI application to run and the default XServer application in the Linux environment is not available in the docker containers[1]. Basically, we need to share the host’s XServer with the Container using volume argument -v /tmp/.X11-unix:/tmp/.X11-unix and the host’s display environment variable --env=DISPLAY . It can be done using the docker run command.

For the experiment, we create two docker containers. One is for running roscore which is a ROS master and the second one is our rviz container. The docker run command for rviz is based in [3] and I am extremely thankful for his Github repo[3].

We are going to use the same Dockerfile for both containers. Download the Dockerfile from the link here and save it as Dockerfile.

To create the first container image, run below command on the same directory as the above file saved(It will take a few minutes to complete):

docker build -t roscore_c .

Run below command to create the second container image that is going to be the rviz container:

docker build -t rviz_c .

First, we need to run the ROS master. In order to do that, we run and ssh into the first container ‘roscore_c’.

docker run -it — rm — privileged — net=host roscore_c /bin/bash

We are now in docker container, run roscore

Now, we need to run and ssh into rviz docker that need bit more arguments as we discussed earlier. Follow below commands to display rviz that runs on the docker on a separate terminal window.

xhost +local:docker

docker run -it — rm — privileged — net=host — env=NVIDIA_VISIBLE_DEVICES=all — env=NVIDIA_DRIVER_CAPABILITIES=all — env=DISPLAY — env=QT_X11_NO_MITSHM=1 -v /tmp/.X11-unix:/tmp/.X11-unix — runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=0 rviz_c /bin/bash

If you are running the latest docker, then no need to pass the Nvidia as runtime as shown above. Code for that version is below:

docker run -it — rm — privileged — net=host — env=NVIDIA_VISIBLE_DEVICES=all — env=NVIDIA_DRIVER_CAPABILITIES=all — env=DISPLAY — env=QT_X11_NO_MITSHM=1 -v /tmp/.X11-unix:/tmp/.X11-unix — gpus 2 rviz_c /bin/bash

We are now in the rviz docker container and run rviz. You can see that rviz window is opened up. Yay :)

Here is the video of the example shown above:

The code can be found here.

If you like my write up, follow me on Github, Linkedin, and/or Medium profile.

Reference:

  1. https://medium.com/@SaravSun/running-gui-applications-inside-docker-containers-83d65c0db110
  2. http://docs.ros.org/kinetic/api/moveit_tutorials/html/doc/quickstart_in_rviz/quickstart_in_rviz_tutorial.html
  3. https://github.com/koenlek/docker_ros_nvidia

--

--