快轉到主要內容

如何在 Docker 中訪問主機 USB ?

·540 字
Computer-Science Docker USB
目錄

Linux
#

方法一:使用--device參數
#

1docker run -it --device=/dev/ttyUSB0 ubuntu bash

如此一來便能讓其使用/dev/ttyUSB0接口

方法二(不推薦):使用--privileged參數開通所有權限
#

1docker run -it --privileged -v /dev/bus/usb:/dev/bus/usb ubuntu bash

簡單暴力,可以直接將/dev/bus/usb掛載入容器使其得以訪問,但也伴隨著較高的資安風險


Windows
#

方法一(推薦):安裝 WSL 2,以建立 Linux 環境
#

由於其實 docker 也是依賴 linux 環境,因此在 windows 系統下,該方法是最為推薦的。
安裝方法請見官方文檔:https://learn.microsoft.com/en-us/windows/wsl/install

方法二:使用 USB over IP 工具
#

這類工具有很多,如 USB/IP PROJECT 就是一個較常見的工具。
接著只要在容器中安裝usbip,並 attach 上 windows 設備即可:

1sudo apt update
2sudo apt install usbip
3sudo modprobe vhci-hcd
4sudo usbip attach -r <windows-ip> -b <busid>

方法三:將接口轉網路設備
#

一樣工具有很多,以下以其中一個知名的工具便是socket cat (socat)
安裝並設定完後,在容器中執行:

1sudo apt update
2sudo apt install socat
3socat TCP:<windows-ip>:<port> /dev/ttyUSB0,raw,echo=0

References
#

Alpaca
作者
Alpaca
No one can stop my feet.

相關文章

How to run "sudo" command without typing password?
·289 字
Computer-Science Ubuntu
Make your username able to run "sudo" without password.
Line Flex Message 在 iPhone 上出現「錯誤 無法正常執行!」
·663 字
Computer-Science Line Bot iPhone

在開發 Line Bot 時,若有使用到 Flex Message,可能會踩到這個坑。

Ubuntu 使用指令連接 Wifi
·492 字
Computer-Science Wi-Fi

用習慣了 Desktop 版本的 Ubuntu,到 command line 卻不會用指令連接 Wifi!

git diff 顯示一堆「^M」?
·1401 字
Computer-Science Git

在不同 OS 底下共同編輯一個 git repository 時,可能就會出現這個問題,要解決該問題其實也很簡單,其原理及解決方法敬請聽我娓娓道來。

Change username and home folder in ubuntu
·189 字
Computer-Science Ubuntu
Set nginx access password
·353 字
Computer-Science Nginx

How to make nginx website require password to access ?