How To Use Docker To Run Multiple ROS Distributions on the Same Machine | by Leon Eversberg | Dec, 2022

run python code with ros melodic

Picture of a cargo ship, symbolizing the Docker.
photo by Ian Taylor Feather unsplash

Robot Operating Systems (ROS) are widely used in robotics. but, available separately ROS distribution Can cause software version conflicts.

Each Ubuntu version comes with a different ROS distribution. For example, Ubuntu 18.04 uses ROS Melodic, based on Python 2.7. Ubuntu 20.04 uses ROS Noetic, which is based on Python 3.

Often, our ROS master needs to be an older version of ROS for specific hardware driver support. In that case, using modern Python3-based software libraries – such as OpenCV 4, Tensorflow, and PyTorch – causes problems.

In this article, I will show you how to put together a ROS Melodic master on the same machine with a recent ROS distribution. We can do this by containerizing our software modules using Docker.

Docker is currently the industry standard for containerizing software. It is like a virtual machine but more lightweight as each container uses the host’s operating system.

“A Docker container image is a lightweight, standalone, executable package of software that contains everything needed to run an application: code, runtime, system tools, system libraries, and settings.” [1]

In a Docker container, we can install whatever ROS distribution and software libraries we want to use – without causing problems for our host system.

As an example use case in this article, we will use Ubuntu 18.04 with ROS Melodic as our master. For demonstration purposes, we will publish a webcam image topic. Using Docker, we will build and run a container with ROS Noetic and OpenCV 4 for computer vision tasks.

With the setup shown in the figure below, ROS Melodic nodes can co-exist and communicate with ROS Noetic nodes using Docker. --network host Installation.

Picture showing layered system architecture with Docker: infrastructure, host operating system, docker
How to use Docker for different ROS versions on the same computer. [Source: Author]

If you don’t already have a catkin workspace on your computer, create one with the following commands [2],

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/

First of all, we should start ROS Melodic Master on our host system. to do this type roscore in a new terminal.

Next, we publish a webcam stream using the ROS package cv_camera [3], To clone a package from GitHub, build the package with catkin_make and cv_camera_nodeExecute the following commands:

cd ~/catkin_ws/src
git clone
cd ..
source devel/setup.bash
rosrun cv_camera cv_camera_node

Now you should be able to see the image theme /cv_camera/image_raw,

For our mockup computer vision node, we will create the following three files in the folder ROS_Docker,

|-- Dockerfile


To build and run a Docker container, we need to write a Dockerfile. Dockerfile is like a blueprint. This tells Docker how to build the image. Docker uses images to build and run containers.

in a Dockerfile [4]We start with a base-image FROM and then install all the required dependenciesRUN apt-get update && apt-get install,

For our ROS application, we need to create a catkin workspace [2], Then, we need to install cv_bridge from Vision_opencv [5] package with RUN git clone,

FROM ros:noetic

# install packages with apt-get
RUN apt-get update && apt-get install -y --no-install-recommends \
git \
libopencv-dev python3-opencv \
&& rm -rf /var/lib/apt/lists/*

# create and build a catkin_ws
SHELL ["/bin/bash", "-c", "-l"]
WORKDIR /home/catkin_ws
RUN mkdir src && source "/opt/ros/$ROS_DISTRO/setup.bash" && catkin_make

# get ROS packages from github
WORKDIR /home/catkin_ws/src
RUN git clone -b noetic
WORKDIR /home/catkin_ws/
RUN source /home/catkin_ws/devel/setup.bash && catkin_make

# copy our scripts
COPY ./ .
COPY ./ .
ENTRYPOINT ["/home/"]

cv_nodeThe purpose is to subscribe to our webcam topic, do something with the image, and then publish a new image topic. To convert between ROS images and OpenCV images, we use cv_bridge.

The following code will subscribe to the webcam topic, append the text with OpenCV 4, and publish the new image to a new topic.

#!/usr/bin/env python3

import rospy
import cv2
from sensor_msgs.msg import Image, CompressedImage
from cv_bridge import CvBridge

class cv_node:
def __init__(self):
self.bridge = CvBridge()
image_sub = rospy.Subscriber("/cv_camera/image_raw", Image, self.callback, queue_size=1)
self.image_pub = rospy.Publisher("/cv_camera/image_out/compressed", CompressedImage, queue_size=1)

def callback(self, data):
cv_img = self.bridge.imgmsg_to_cv2(data)
cv_img = cv2.putText(cv_img, "Published with ROS Noetic", (30,30), cv2.FONT_HERSHEY_SIMPLEX, 1.1, (255,0,0), 2)
cv_img = self.bridge.cv2_to_compressed_imgmsg(cv_img)

def main():
node = cv_node()

rate = rospy.Rate(30) # frequency in Hz
while not rospy.is_shutdown():

if __name__ == '__main__':

This node can be adapted to perform more advanced functions. For example, we can use a deep neural network for object detection cv_img,

If you want to import other libraries – for example, PyTorch or Tensorflow – be sure to install them in the Dockerfile RUN pip install [...]

This script is the entry point to our Docker container. This simply starts our ROS node,

source /home/catkin_ws/devel/setup.bash

To build the Docker image, execute the following command in Terminal: docker build -t medium .

This will run the Dockerfile in the current directory and create a Docker image with tag medium.

After the build process is finished, run the following command in the terminal:

docker run -it --network host medium:latest

This will run the latest container with tag medium in interactive mode. Also, it will use the host’s network so that the Docker container and our ROS master can communicate.

this much only! You should now see a new topic /cv_camera/image_out/compressed on the host system. you can check cv_node use command rosnode info cv_node,

Leave a Reply