Docker - a way to give access to a host USB or ser

2019-01-05 07:38发布

Last time I checked, Docker didn't have any means to give container access to host serial or USB port. Is there a trick which allows doing that?

标签: docker
4条回答
三岁会撩人
2楼-- · 2019-01-05 08:01

--device works until your USB device gets unplugged/replugged and then it stops working. You have to use cgroup devices.allow get around it.

Details on my blog: http://marc.merlins.org/perso/linux/post_2018-12-20_Accessing-USB-Devices-In-Docker-ttyUSB0-dev-bus-usb--for-fastboot-adb_-without-using-privileged.html

It's a bit hard to paste, but in a nutshell, you need to get the major number for your character device and send that to cgroup:
root@server:~# echo 'c 189:* rwm' > /sys/fs/cgroup/devices/docker/$A*/devices.allow
(A contains the docker containerID)

without doing this, any newly plugged or rebooting device after the container started, will get a new bus ID and will not be allowed access in the container.

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-05 08:04

There are a couple of options. First and foremost, as noted by @Mark below, Docker version 1.2.0 (released 2014/08) added 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
查看更多
别忘想泡老子
4楼-- · 2019-01-05 08:04

I wanted to extend the answers already given to include support for dynamically connected devices that aren't captured with /dev/bus/usb and how to get this working when using a Windows host along with the boot2docker VM.

If you are working with Windows, you'll need to add any USB rules for devices that you want Docker to access within the VirtualBox manager. To do this you can stop the VM by running:

host:~$ docker-machine stop default

Open the VirtualBox Manager and add USB support with filters as required.

Start the boot2docker VM:

host:~$ docker-machine start default

Since the USB devices are connected to the boot2docker VM, the commands need to be run from that machine. Open up a terminal with the VM and run the docker run command:

host:~$ docker-machine ssh
docker@default:~$ docker run -it --privileged ubuntu bash

Note, when the command is run like this, then only previously connected USB devices will be captures. The volumes flag is only required if you want this to work with devices connected after the container is started. In that case, you can use:

docker@default:~$ docker run -it --privileged -v /dev:/dev ubuntu bash

Note, I had to use /dev instead of /dev/bus/usb in some cases to capture a device like /dev/sg2. I can only assume the same would be true for devices like /dev/ttyACM0 or /dev/ttyUSB0.

The docker run commands will work with a Linux host as well.

查看更多
SAY GOODBYE
5楼-- · 2019-01-05 08:08

With current versions of Docker, you can use the --device flag to achieve what you want, without needing to give access to all USB devices.

For example, if you wanted to make only /dev/ttyUSB0 accessible within your Docker container, you could do something like:

docker run -t -i --device=/dev/ttyUSB0 ubuntu bash
查看更多
登录 后发表回答