You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将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 lsusb to find your FTDI device. You should see a line like:
    Bus 003 Device 007: ID 0403:601c Future Technology Devices International, Ltd
    
    Note down the Bus number (003), Device number (007), Vendor ID (0x0403), and Product ID (0x601c).
  • 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.

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:
    • devices mounts 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, run id -u and id -g on 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 lsusb again to confirm.
    • Make sure your host user is in the plugdev or dialout group. Run sudo usermod -aG plugdev $USER and log out/in to apply.
    • In the container, run ls /dev/bus/usb/003/007 to confirm the device node exists. If not, check your devices or volumes mount in docker-compose.
  • Permission errors?
    • Verify the device_cgroup_rules is set correctly.
    • Ensure the container user has the same UID/GID as your host user (the user field in docker-compose helps with this).

内容的提问来源于stack exchange,提问作者ThunderBolt84

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:39:09