Pradyumn Joshi
Pradyumn Joshi

Reputation: 61

Unable to access camera in Docker container of Ubuntu

I am writing a simple code of Opencv which captures images and show us. But whenever i am running it it is telling unable to access camera with this index.

I tried running my docker file with this command docker run -ti --device /dev/video0:/dev/video0 pradyumn10/ubuntu-python3 /bin/bash This opens camera for a second and then it closes it and gives a error "unable to display"

Upvotes: 0

Views: 4276

Answers (2)

schrödinbug
schrödinbug

Reputation: 853

My issue was that I was running the container in privileged mode with a user other than root, i.e. with options -u vscode --privileged --device /dev/video0:/dev/video0 (among others). The privileged option caused the webcam owner and group to be root, however since I was running the container as vscode permission to access /dev/video0 was being denied.

The solution was to not run the container in priviledged mode (i.e. omit the --privileged flag--causes the webcam owner to be root, but group video) and add the vscode user to the video group. This was easily done in the by adding this line to my Dockerfile:

RUN usermod -aG vscode video

and rebuilding the container. If you must run the container in privileged mode you can instead sudo chown root:video /dev/video0 once the container is running.

Upvotes: 3

user2932688
user2932688

Reputation: 1694

Your camera is most probably a web camera and most probably mounted to the /dev/video0 or /dev/video{N} - you will have to find out {N} on your host machine first.

once you get it, you can try mounting it into your docker container like this:

mount /dev/video0 /testvideo
docker run -it --rm --read-only -v "/testvideo:/testvideo" bash

after that your app in docker should try to connect to /testvideo instead of /dev/video0

P.S. i didn't try it myself, but feels like it linux should not have any issues mounting web camera as any other device. I would give it a try, but no guarantee :)

Upvotes: 1

Related Questions