2

I am trying to create a docker image with opencv in order to display a video. I have the following Dockerfile:

FROM python:3
ADD testDocker_1.py /
ADD video1.mp4 /
RUN pip install opencv-python
CMD [ "python", "./testDocker_1.py" ]

And the following python script:

import cv2
import os

if __name__ == '__main__':
    file_path = './video1.mp4'
    cap = cv2.VideoCapture(file_path)
    ret, frame = cap.read()
    while ret:
        ret, frame = cap.read()
        if ret:
            cv2.imshow('Frame Docker', frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

So first I build the Image:

$ sudo docker build -t test1 .

And the problem comes when I run the container:

$ sudo docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix test1
No protocol specified
: cannot connect to X server :1

Regards.

  • As you've already noticed, getting a Docker container to connect to your host display is awkward at best; Docker really isn't designed for this use case. Why do you want it the program (with access to host hardware devices) to run inside a container? – David Maze Apr 13 '20 at 16:50

1 Answers1

4

Try this

xhost +
sudo docker run -ti --rm -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix test1

Although it would solve this particular use case, but you need to make a note of the following:

Basically, the xhost + allows everybody to use your host x server;

Refrence

A better and recommended solution is present here

Kapil Khandelwal
  • 1,096
  • 12
  • 19
  • `xhost +` allows unrestricted access to your display server to anyone who can reach it. That includes capturing and injecting keystrokes. You almost certainly don't want to do this. – David Maze Apr 13 '20 at 16:49