Create and use Docker images for ZED and ROS

In this section, we guide you to create a docker image that can run ROS and ZED SDK.

Dockerfile Overview #

Specify the parent image #

In this section, we first specify the parent Docker image to pull a ZED SDK Docker image. The Ubuntu release year, SDK and CUDA versions are passed as arguments

# Build Arguments
ARG UBUNTU_RELEASE_YEAR
ARG ZED_SDK_MAJOR
ARG ZED_SDK_MINOR
ARG CUDA_MAJOR
ARG CUDA_MINOR

# Specify the parent image from which we build
FROM stereolabs/zed:${ZED_SDK_MAJOR}.${ZED_SDK_MINOR}-gl-devel-cuda${CUDA_MAJOR}.${CUDA_MINOR}-ubuntu${UBUNTU_RELEASE_YEAR}.04

ARG ROS_DISTRO_ARG
ENV LOGNAME root
ENV ROS_DISTRO ${ROS_DISTRO_ARG}
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8

The above section remains the same for both ROS and ROS 2 Dockerfiles.

Install ROS #

Once you have specified the source image, you can go ahead and decide on the ROS distribution to be installed.

Note: This Dockerfile.ros installs dependencies based on the versions mentioned, be careful to check the version availability and compatibility.

# Setup ROS
RUN apt-get update -y || true && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata curl && \
    sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list' && \
    curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add - && \
    apt-get update || true &&\
    DEBIAN_FRONTEND=noninteractive apt-get install -y ros-$ROS_DISTRO-desktop-full build-essential cmake usbutils libusb-1.0-0-dev git -y --allow-unauthenticated

# Install Packages depending on ROS distro
RUN if [ "$ROS_DISTRO_ARG" = "noetic" ] ; then \
	apt-get install -y python3-rosdep python3-rosinstall  python3-rosinstall-generator python3-wstool ;\
    else \
       apt-get install -y python-rosdep  python-rosinstall python-rosinstall-generator python-wstool ; \
    fi 

RUN rm -rf /var/lib/apt/lists/*

Setup the ROS workspace #

This final section lets you set up the ROS workspace and get ready to install ROS packages.

RUN mkdir -p /opt/ros_ws/src
WORKDIR /opt/ros_ws
RUN  . /opt/ros/noetic/setup.sh && \
	catkin_make 		

# setup entrypoint
COPY ./ros_entrypoint.sh /
ENTRYPOINT ["/ros_entrypoint.sh"]

CMD ["bash"]

Entrypoint #

As you can see Dockerfile.ros contain the command ENTRYPOINT ["/ros_entrypoint.sh"]. The ENTRYPOINT specified is the default executable of the image which runs the script ros_entrypoint.sh every time the image is run. You can find this file along with the Dockerfiles here.

#!/bin/bash
set -e

# setup ros environment
source "/opt/ros/$ROS_DISTRO/setup.bash" 
source "$ROS_WS/devel/setup.bash"
exec "$@"

ros_entrypoint.sh makes sure that the setup.bash file is sourced every time when the image is run. Make sure that this file is present along with your Dockerfile when you build and run the image.

Build Script Overview #

Just like the build script for OpenCV dockerfiles, the build-ros-desktop-image.sh file lets you configure the versions which are passed during the build. The script is detailed below.

Configure the arguments #

The build arguments remain the same as that of the OpenCV Dockerfile build arguments, except here instead of having the OPENCV_VERSION we have an argument ROS_2_FLAG.

The ROS distro is selected based on the UBUNTU_RELEASE_YEAR , when the release year is 20 ROS_2_FLAG lets you choose between ROS or ROS 2, when the flag is set to 0 ROS distro noetic is installed and when set to 1 ROS 2 Foxy Fitzroy is installed.

These values are default and can be changed to the versions that you intend to build.

UBUNTU_RELEASE_YEAR=20 	#Specify the Ubuntu release year
ZED_SDK_MAJOR=3 		# ZED SDK major version 
ZED_SDK_MINOR=7 		# ZED SDK minor version
CUDA_MAJOR=11 			# CUDA major version
CUDA_MINOR=4 			# CUDA minor version 
ROS_2_FLAG=0			    # Change it to 1 to setup ROS 2

###Check for version compatibility In addition to checking for the versions compatibility it also assigns corresponding $ROS_DISTRO based on the UBUNTU_RELEASE_YEAR.

#Check for the version compatibilities

if [ ${UBUNTU_RELEASE_YEAR} == "18" ] ; then
	echo "Ubuntu 18.04"
	ROS_DISTRO_ARG="melodic"
	# Not compatible with CUDA <= 9
	if [ ${CUDA_MAJOR} -le "9" ] ; then
		echo "Ubuntu 18.04 Not compatible with CUDA <= 9"
		exit
	fi
elif [ ${UBUNTU_RELEASE_YEAR} == "20" ] ; then
	if [ ${ROS_2_FLAG} == 1 ] ; then 
		ROS_DISTRO_ARG="foxy"
	else 
		ROS_DISTRO_ARG="noetic"
	fi
	# Not compatible with CUDA <= 10
	if [ ${CUDA_MAJOR} -le "10" ] ; then
   		echo "Ubuntu 20.04 is not compatible with CUDA <= 10 "
   		exit
	fi
else
	echo "UBUNTU_RELEASE_YEAR! Allowed values are 18 or 20 "
	exit
fi 

if [ ${CUDA_MAJOR} -ge "11" ] ; then
	if [ ${ZED_SDK_MINOR} -lt "2" ] ; then # CUDA 11.0 was introduced with 3.2
		echo "CUDA 11.0 was introduced with 3.2"
		exit
	fi
	if [ ${CUDA_MINOR} -ge "1" ] ; then
		if [ ${ZED_SDK_MINOR} -lt "3" ] ; then # CUDA 11.1 was introduced with 3.3
			echo "CUDA 11.1 was introduced with 3.3"
		exit
		fi
	fi
	if [ ${CUDA_MINOR} == "2" ] || [ ${CUDA_MINOR} == "3" ]  || [ ${CUDA_MINOR} -ge "6" ] ; then 
		#invalid CUDA versions
   		echo "Invalid CUDA_MINOR! Allowed values : 0,1,4,5"
   		exit
	fi
elif [ ${CUDA_MAJOR} == "10" ] ; then
	if [ ${CUDA_MINOR} != "0" ] || [ ${CUDA_MINOR} != "2" ] ; then 
	   echo "Invalid CUDA_MINOR! Allowed values are 0 or 2"
	   exit
	fi
else
	echo "Invalid CUDA_MAJOR! Allowed values are 10 or 11" 
fi	
	

Docker build #

This section of the script executes docker build for creating the Docker image, since a single script is used to build both ROS and ROS 2 Docker images, the default tag is assigned based on the ROS_2_FLAG. Accordingly, different Dockerfiles are chosen for the build.

# Default Tag based on the selected versions
if [ ${ROS_2_FLAG} == 1 ] ; then
	echo "ROS_2 flag == 1 " 
	TAG="${ZED_SDK_MAJOR}.${ZED_SDK_MINOR}-ros2-gl-devel-cuda${CUDA_MAJOR}.${CUDA_MINOR}-ubuntu${UBUNTU_RELEASE_YEAR}.04"
	DOCKERFILE="Dockerfile.ros2"
else
	TAG="${ZED_SDK_MAJOR}.${ZED_SDK_MINOR}-ros-gl-devel-cuda${CUDA_MAJOR}.${CUDA_MINOR}-ubuntu${UBUNTU_RELEASE_YEAR}.04"
	DOCKERFILE="Dockerfile.ros"
fi
           

echo "Building '${TAG}'" 

docker build --build-arg UBUNTU_RELEASE_YEAR=${UBUNTU_RELEASE_YEAR} \
--build-arg ZED_SDK_MAJOR=${ZED_SDK_MAJOR} \
--build-arg ZED_SDK_MINOR=${ZED_SDK_MINOR} \
--build-arg ROS_DISTRO_ARG=${ROS_DISTRO_ARG} \
--build-arg CUDA_MAJOR=${CUDA_MAJOR} \
--build-arg CUDA_MINOR=${CUDA_MINOR} \
-t "${TAG}" -f "${DOCKERFILE}" .