如何将FTDI串口转USB桥接设备挂载至Docker容器?(适配pyusb/libusb,避免privileged模式)
Hey there! Since you're new to Ubuntu, Docker, and Python, let's break this down step by step to get your FTDI USB device working in a Docker container without using privileged: true. The key issue here is that your FTDI device isn't showing up as a TTY (like Arduino does), so we need to mount the raw USB device node instead, and set up the right permissions for libusb to access it.
1. First: Confirm Your FTDI Device's Details on the Host
You already started this, but let's formalize it to get all the info we need:
- Run
lsusbto find your FTDI device. You should see a line like:
Note down the Bus number (003), Device number (007), Vendor ID (0x0403), and Product ID (0x601c).Bus 003 Device 007: ID 0403:601c Future Technology Devices International, Ltd - The raw USB device path on your host is
/dev/bus/usb/<BUS_NUMBER>/<DEVICE_NUMBER>— in this example, that's/dev/bus/usb/003/007.
2. Option 1: Mount a Single FTDI Device to Your Container (Recommended)
This is the safest approach since it only gives the container access to the specific device, not all system resources.
Update your docker-compose.yml
Modify your service section to include:
services: your-service-name: # ... your existing config (image, volumes, command, etc.) ... devices: # Mount the raw USB device node from host to container (keep the same path) - "/dev/bus/usb/003/007:/dev/bus/usb/003/007" # Grant permission to access USB devices (189 is the major number for USB) device_cgroup_rules: - 'c 189:* rwm' # Run the container with your host user's UID/GID to avoid permission issues user: "${UID}:${GID}"
- Quick breakdown of each setting:
devicesmounts the exact USB device node into the container, so libusb can locate it the same way it does on the host.device_cgroup_rules: 'c 189:* rwm'gives the container read/write/modify access to all USB devices (you can narrow this to your specific device if needed, but this is simpler for starters).user: "${UID}:${GID}"ensures the container user has the same permissions as your host user. To get your UID/GID, runid -uandid -gon the host.
Install Dependencies in the Container
Make sure your Dockerfile (or container setup) installs the required libraries for pyusb and libusb. If you're using a Debian/Ubuntu-based image:
RUN apt-get update && apt-get install -y \ libusb-1.0-0-dev \ python3-pip RUN pip3 install pyusb
Test the Python Script in the Container
Run your script again. If you still hit the NoBackendError, try explicitly specifying the libusb backend in your code (this ensures pyusb uses the correct library in the container):
import usb.core import usb.util import usb.backend.libusb1 # Point to the libusb shared library in the container backend = usb.backend.libusb1.get_backend(find_library=lambda x: "/usr/lib/x86_64-linux-gnu/libusb-1.0.so.0") # Find the device using the specified backend device = usb.core.find(idVendor=0x0403, idProduct=0x601c, backend=backend) if device is None: raise ValueError('Device not found') else: print(device)
3. Option 2: Mount All USB Devices to the Container (Less Secure, But Convenient)
If you want the container to access all USB devices on the host (not just the FTDI one), use these settings in docker-compose.yml:
services: your-service-name: # ... existing config ... volumes: - "/dev/bus/usb:/dev/bus/usb" device_cgroup_rules: - 'c 189:* rwm' user: "${UID}:${GID}"
This mounts the entire USB bus directory into the container, so any USB device connected to the host will be visible inside.
4. Troubleshooting Tips
- Device not found in container?
- Double-check the bus/device numbers — they can change if you unplug/replug the device. Run
lsusbagain to confirm. - Make sure your host user is in the
plugdevordialoutgroup. Runsudo usermod -aG plugdev $USERand log out/in to apply. - In the container, run
ls /dev/bus/usb/003/007to confirm the device node exists. If not, check yourdevicesorvolumesmount in docker-compose.
- Double-check the bus/device numbers — they can change if you unplug/replug the device. Run
- Permission errors?
- Verify the
device_cgroup_rulesis set correctly. - Ensure the container user has the same UID/GID as your host user (the
userfield in docker-compose helps with this).
- Verify the
内容的提问来源于stack exchange,提问作者ThunderBolt84

