Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save moritz-meier/dfe6b8169e63770b430377ff1e3c8584 to your computer and use it in GitHub Desktop.
Save moritz-meier/dfe6b8169e63770b430377ff1e3c8584 to your computer and use it in GitHub Desktop.
Pass USB devices from a Windows host into a Docker container

Pass USB devices from a Windows host into a Docker container

This is useful for embedded work in devcontainers.

1. Pass USB device from Windows host to WSL2

see: https://docs.microsoft.com/en-us/windows/wsl/connect-usb

  • install usbipd-win on Windows host

  • install usbip client tools in any WSL distro (prefered the default distro, but any distro should work, since all distros share the same kernel and thus the same devices)

    • Ubuntu: follow the standard installation process linked above
    • docker-desktop:
      • apk update
      • apk upgrade
      • update version numbers in /etc/apk/repositories to latest (e.g. "3.9" -> "3.16")
      • apk update --allow-untrusted
      • apk upgrade --available --allow-untrusted && sync
      • apk add alpine-keys --allow-untrusted
      • apk add linux-tools-usbip hwdata-usb hwids-usb usbutils
  • attach device to WSL (if the usbip client tools are not installed in the default distro, use option usbipd -d <distro>)

2. Start docker container and pass device from WSL2 to container

  • start docker container from windows host, use --device= option to pass device from WSL to container:
    • docker run -it --rm --device="/dev/bus/usb/<busid>/<devid>" <some image>
      • the busis:devid in WSL is not the same as on the host machine, use lsusb to get the id

Configure devcontainer:

  • Use "runArgs": ["--device=/dev/bus/usb/<busid>/<devid>"] to pass device to container on devcontainer start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment