Display images using Open CV when using docker container with xhost +


Jetson AGX Orin
GStreamer: 1.16.3
Jetpack: 5.0.2
CUDA Version: 11.4
Operating System + Version: Ubuntu 20.04.4 LT
TensorRT Version:
Python Version: 3.8.2-0

I’m trying to display image with container
Here’s the command I do:
docker run --gpus all -it --name test -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY --ipc=host –v /home/aaeon/test:/workspace lpr

After enter container:
python /workspace/readVideo.py

And the error message:

No protocol specified
Traceback (most recent call last):
File “/workspace/readVideo.py”, line 9, in
cv2.namedWindow(“Demo”, cv2.WINDOW_NORMAL)
cv2.error: OpenCV(4.5.3) /git/opencv-4.5.3/modules/highgui/src/window_gtk.cpp:635: error: (-2:Unspecified error) Can’t initialize GTK backend in function ‘cvInitSystem’

I’ve found a similar topic for my question, but it’s not work before running container

I have to execute “xhost +” every time before executing the python script in container

Here’s the python script

import cv2
rtsp_url = ‘rtsp://’
cap = cv2.VideoCapture(rtsp_url)
cv2.namedWindow(“Demo”, cv2.WINDOW_NORMAL)
cv2.resizeWindow(“Demo”, 960, 540)
if not cap.isOpened():
print(“can not open RTSP stream!”)
while True:
ret, frame = cap.read()
if not ret:
cv2.imshow(‘Demo’, frame)
if cv2.waitKey(1) == ord(‘q’):

Is there any suggestion?
Thanks for help


Could you try if :1 works in your environment?

$ export DISPLAY=:1



it doesn’t work and even if using the command “xhost +”


Is there any other suggestion?

In addition, what’s the reason cause this error?

I’m not sure whether is related to hardware design or just the software issue of opencv.


Have you also exported the DISPLAY variable within the container?

Outside of the container

$ export DISPLAY=:0
$ xhost +

Inside of the container

$ export DISPLAY=:0



I have check the variable by echo and it showed “:0”

Also double check by using “xrandr” and “who”


Could you try to launch the container with the below command?

$ sudo docker run -it --rm --net=host --runtime nvidia -e DISPLAY=$DISPLAY -v /tmp/.X11-unix/:/tmp/.X11-unix -v /home/aaeon/test:/workspace lpr



The result is as same as the beginning


Sorry for the late update.
If you run the script outside of the container, could you display the image with OpenCV?



Yes, only run the script in container failed

GUI backend for your opencv build seems to be GTK.
Is GTK installed in your container ?

If the opencv lib is the same out of docker and it works there, then try installing same GTK version into container if not yet done.
You may check what GUI backend and its version for an opencv build with function getBuildInformation().
For example, if using from python3:

python3 -c 'import cv2; print(cv2.getBuildInformation())'  |& tee | grep -A 8 GUI                

If the versions seems matching, it may be a GTK/X issue.
Can you run this simple GTK test from container or does it also fails to initialize ?

#!/usr/bin/env python

import gi

# You may adapt for your GTK version
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class GTK_Main:
    def __init__(self):
        window = Gtk.Window()
        window.set_title("Test GTK")
        window.set_default_size(100, 100)
        window.connect("destroy", Gtk.main_quit, "WM destroy")
        vbox = Gtk.VBox()
        hbox = Gtk.HBox()
        vbox.pack_start(hbox, False, False, 0)
        self.button = Gtk.Button(label="Quit")
        self.button.connect("clicked", self.exit)
        hbox.pack_start(self.button, False, False, 0)

    def exit(self, w):


If this also fails, you may post the error messages.

Or just checking X display from docker, does this work ?


Hi ,

Here’s log

Is that mean I have installed GTK already? Or I miss some packages?

The log showed

No, it just means that your opencv build installed for python3 has been built for using GTK2 as GUI backend.

  1. The log showed

You may just require version 2.0:

gi.require_version('Gtk', '2.0')
  1. What about xeyes ? This would confirm X is working from container.


Sorry for bothering, but I’ve solved this issue by remaking our rootfs and reinstalling all the components by SDK manager

It can execute normally by only typing the command “xhost +” once before entering the container

The route cause could be the version of components or we use the EA version of jp5.0.2 rootfs

Anyway, it is fine now

Thanks for your help

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.