learning docker steps(10) ----在容器当中访问主机设备文件
从docker 1.2开始,我们可以在容器内部访问主机设备通过 –device命 令选项, 在早期的版本当中可以通过-v选项实现。
首先来了解一下—device的使用方法:
--device=<Host Device>:<Container Device Mapping>:<Permissions>
如下的样例是映射usb串口进容器当中
1) 插入usb转串口设备至ubuntu当中,通过内核信息可知其枚举成/dev/ttyUSB0.
2)
[240164.248698] usb 1-2: new full-speed USB device number 13 using xhci_hcd
[240164.523420] usb 1-2: New USB device found, idVendor=1a86, idProduct=7523, bcdDevice= 2.63
[240164.523422] usb 1-2: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[240164.523423] usb 1-2: Product: USB2.0-Serial
[240165.490983] usbcore: registered new interface driver usbserial_generic
[240165.491536] usbserial: USB Serial support registered for generic
[240165.518230] usbcore: registered new interface driver ch341
[240165.518275] usbserial: USB Serial support registered for ch341-uart
[240165.518330] ch341 1-2:1.0: ch341-uart converter detected
[240165.519882] usb 1-2: ch341-uart converter now attached to ttyUSB0
2) 使用device命令选项将其映射至ubuntu容器当中:
# docker run -ti --device=/dev/ttyUSB0:/dev/ttyUSB0 ubuntu bash
3) 在容器当中查看映射之后的设备文件:
root@1faf0ed6945d:/# ls /dev/ -al
total 4
drwxr-xr-x 5 root root 380 Mar 20 05:16 .
drwxr-xr-x 1 root root 4096 Mar 20 05:16 ..
crw--w---- 1 root tty 136, 0 Mar 20 05:16 console
lrwxrwxrwx 1 root root 11 Mar 20 05:16 core -> /proc/kcore
lrwxrwxrwx 1 root root 13 Mar 20 05:16 fd -> /proc/self/fd
crw-rw-rw- 1 root root 1, 7 Mar 20 05:16 full
drwxrwxrwt 2 root root 40 Mar 20 05:16 mqueue
crw-rw-rw- 1 root root 1, 3 Mar 20 05:16 null
lrwxrwxrwx 1 root root 8 Mar 20 05:16 ptmx -> pts/ptmx
drwxr-xr-x 2 root root 0 Mar 20 05:16 pts
crw-rw-rw- 1 root root 1, 8 Mar 20 05:16 random
drwxrwxrwt 2 root root 40 Mar 20 05:16 shm
lrwxrwxrwx 1 root root 15 Mar 20 05:16 stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Mar 20 05:16 stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Mar 20 05:16 stdout -> /proc/self/fd/1
crw-rw-rw- 1 root root 5, 0 Mar 20 05:16 tty
crw-rw---- 1 root dialout 188, 0 Mar 20 05:16 ttyUSB0
crw-rw-rw- 1 root root 1, 9 Mar 20 05:16 urandom
crw-rw-rw- 1 root root 1, 5 Mar 20 05:16 zero
root@1faf0ed6945d:/# ls -al /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 0 Mar 20 05:16 /dev/ttyUSB0
root@1faf0ed6945d:/#