I have seen many people struggle with USB device management from the context of a docker container.
This is particularly helpful in an embedded development workflow as interacting via USB devices happens almost constantly! (And the removal and reconnection thereof is considered the first debug step in many cases)
Obviously with the default USB management of Docker, this leaves a lot to be desired. Firstly USB devices are only scanned and mounted at the point of containerisation, not dynamically. And then secondly, the container will “hold onto” any USB device, as from within the containers context /dev/ttyUSBx still exists, regardless of whether the actual underline device is connect or not. Leading to constant rebuilds just to rescan USB Devices.
But fear not, I have a solution! A hacky one, but a functional one (as long as you’re on Ubuntu )
So we want to to essentially mount the entire /dev directory into our docker container, along with giving it privileged access. Below is an example setup from my devcontainer.json:
"image": "IMAGE_REPO/IMAGE_NAME:IMAGE_TAG", "mounts": [ "source=/dev,target=/dev,type=bind" ], "runArgs": [ "-it", "--privileged" ]
In this way, the entire /dev directory is mounted, and the changes in the host systems /dev are represented in the containers, allowing dynamic USB management directly from the host system straight into the container!
Please note I work solely in Ubuntu (20.04LTS) and solely with DockerDesktop.
I hope this helps my embedded friends!