Create Docker images for ZED and OpenCV
We have earlier seen how to create a docker image. In this section we follow the same recommended workflow for creating docker images with slight changes, therefore we highly recommend you go through that tutorial to refresh your memory before continuing here.
A generic Dockerfile skeleton is made available to assemble an image and a build script build-opencv-desktop-image.sh that specifies build arguments. build-opencv-desktop-image.sh lets you configure build arguments and then build the Docker image, thereby enabling the customization of your Docker container.
Dockerfile Overview
The full Dockerfile contains many instructions which are explained in detail later. This file can be altered based on your requirement.
Below is the analysis of the main parts that compose the Dockerfile.
Specify the parent image
First, specify a base ZED SDK docker image from which you want to build the new image. These images come with ZED SDK pre-installed and let you use the ZED camera with SDK applications.
There are multiple Docker images available with different Ubuntu release years, SDK and CUDA versions. Hence, we first choose a specific ZED SDK Docker image as a parent image by configuring the arguments.
The Ubuntu release year, SDK and CUDA versions are passed as arguments during the build stage making it modular. Assigning these arguments with the version of your choice will be discussed in a later section.
Based on the arguments specified in the build script a specific base image will be imported, for example, if the build arguments in build-opencv-ubuntu-image.sh are set to values mentioned below:
Then the base image will be stereolabs/zed:3.7-gl-devel-cuda11.4-ubuntu20.04
Note that the base image is chosen such that it already consists of OpenGL support for display, if you wish to not have it you can follow the section below to build image without display support.
Meanwhile, you can also explore the StereoLabs DockerHub repository that contains official ZED SDK Docker images.
Install dependencies
Once you have specified the parent image you can go ahead and decide which OpenCV version to be installed. Be careful to check the version availability and compatibility.
This part of the Dockerfile installs all the OpenCV dependencies.
In the next stage, you install OpenCV, this can be achieved in two methods and both of them are explained below.
Method 1: Install OpenCV from the source
This section downloads the OpenCV source files of the chosen version and builds it.
Make sure you eliminate this section if you choose to use the 2nd method
Method 2: Install OpenCV using the Ubuntu repository
Alternatively, you can simply install OpenCV from the Ubuntu repository.
Although this method is a much simpler and easier method to install OpenCV that makes your image lighter, building OpenCV from the source allows you to have the latest available version, flexibility and complete control over the build options.
Choose the method that is appropriate for you.
Build Script Overview
As mentioned above the Docker images can be customized to various available versions and the build-opencv-ubuntu-image.sh script lets you configure the versions which are passed during the build and also creates the Docker image using docker build command.
The script is detailed in this section below.
Configure the arguments
Specify the Ubuntu release year, ZED SDK CUDA and OpenCV versions. Here can see the default values set in the script, you should edit it to the version you want. These arguments are later passed as --build-arg during the build.
Check for the version compatibility
This section of the script checks the arguments you have entered in the above section and examines compatibility between different versions. In case of invalid entry, it exits the build.
Docker build
The below part of the script assigns a default tag to the docker image that is to be created based on the chosen arguments and builds the Docker container.
Create your Docker Image with OpenCV
Now that you are familiar with the Dockerfile and the build-opencv-desktop-image.sh it’s time to create your image.
Download the files from this link, edit the arguments to your desired version and simply run the script to create the Docker image
That’s it! You can now change versions and create your own docker containers by just manipulating the arguments. Go ahead and test your images and host them as mentioned in this tutorial.
Docker Image without display support
Display window is an integral part of most OpenCV applications. However, Docker is mainly intended to run command-line applications and the addition of a display window is possible only in containers with OpenGL support.
By eliminating the inclusion of OpenGL we can make the docker images much lighter and besides it eliminates the need for all the dependencies required to include display support. Below are a few ways how it can be achieved.
Choose a parent docker image without OpenGL support which can be simply made by changing the Parent image tag as follows
You can read more about image-specific tags on Stereolab’s DockerHub page.
- In the applications, the output image window can be saved instead of displayed. Below is the code snippet from
zed-opencvsample that uses the ENABLE_DISPLAY flag to either display the image or save it as a video.
Please refer to zed-opencv GitHub repository for the complete code.

