Docker Community Forums

Share and learn in the Docker community.

Access USBwebcam in docker containers from Ubuntu local machine

I am trying to access the webcam from the docker container but I am getting this error . Can someone please help me with this ?
I committed a container from an image available from the dockerhub (spmallick/opencv-docker )which has openCV and python

docker run -it --device=/dev/video0:/dev/video0 -v /tmp/.X11-unix:/tmp/.X11-unix -v ${HOME}/.Xauthority:/home/.Xauthority -e DISPLAY=$DISPLAY -p 5000:5000 -p 8888:8888 -it finaltest1 /bin/bash

ERROR:
(python3:107): GStreamer-CRITICAL **: gst_element_get_state: assertion ‘GST_IS_ELEMENT (element)’ failed
No protocol specified
QXcbConnection: Could not connect to display :0
Aborted (core dumped)

Is it possible to access the webcam from a container ?

There are a couple of options. You can use the --device flag that use can use to access USB devices without --privileged mode:

docker run -t -i --device=/dev/ttyUSB0 ubuntu bash
Alternatively, assuming your USB device is available with drivers working, etc. on the host in /dev/bus/usb, you can mount this in the container using privileged mode and the volumes option. For example:

docker run -t -i --privileged -v /dev/bus/usb:/dev/bus/usb ubuntu bash
Note that as the name implies, --privileged is insecure and should be handled with care.