Docker cannot connect to X server

Kasper Hansen picture Kasper Hansen · Jul 8, 2019 · Viewed 9.1k times · Source

I have created a docker image for opencv and facial reckognition to simplify the setup process.

But the recognize.py script needs X Server to show the image result. Here is what I have done so far:

sudo docker run -t -d --name opencv opencv:latest
sudo docker exec -it opencv bash /extract-embeddings.sh
sudo docker exec -it opencv bash /train-model.sh

All is fine so far. The last step is the actual comparison that displays the result in an image.

sudo docker exec -it opencv bash /face-recognition.sh

It gives the output:

[INFO] loading face detector...
[INFO] loading face recognizer...
No protocol specified
: cannot connect to X server :0

I have tried running the container with the following command:

sudo docker run -t -d --name opencv -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix opencv:latest

But it doesn't help.

Answer

Afroz Chakure picture Afroz Chakure · Apr 6, 2020

Try running this,

xhost +

sudo docker run --rm -ti --net=host --ipc=host -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix --env="QT_X11_NO_MITSHM=1" <image_name> <arguments>

Other might face issue regarding the image not getting rendered on screen or getting a blank screen with no image, for them add --env="_X11_NO_MITSHM=1" to the above script while running the docker image. It will solve the problem.

For further information, I would recommend you guys check out the below references.

Reference 1
Reference 2