Sharing Devices (Webcam, Usb Drives, etc) with Docker

Docker - a way to give access to a host USB or serial device?

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.

Access webcam using OpenCV (Python) in Docker?

The Dockerfile in the link you provided doesn't specify how opencv was installed, can you provide the Dockerfile you used? Or how you installed opencv?

VideoCapture(0) won't work if you install opencv via pip.

You're using --device=/dev/video0:/dev/video0 correctly.

Docker- Any way to provide multiple USB device Access (Not using --privilaged)?

Docker works with device option as an array. So,
you can specify several devices also with device option:

docker run -ti --device=/dev/ttyUSB4 --device=/dev/video0 --device=/dev/video4 ubuntu bash

In docker-compose is also possible:

docker-compose.yml

...
services:
myservice:
...
devices:
- "/dev/ttyUSB4:/dev/ttyUSB4"
- "/dev/video0:/dev/video0"
- "/dev/video4:/dev/video4"

There's another possibility giving linux capability, but it's unrecommended (dangerous like privileged mode) for production: FOWNER capability:

docker run -ti --cap-add=FOWNER ubuntu bash

Nevertheless, in kubernetes, for example, it's not enough and you need privileged mode.



Related Topics



Leave a reply



Submit