Mounting USB Devices in a Docker Ubuntu Container on Windows
Overview
This process enables a Docker container running Ubuntu to access USB peripherals connected to the Windows host system, a common requirement for device-dependent software development and debugging.
Prerequisites
- Docker Desktop for Windows installed and running.
- The official Ubuntu container image available locally. It can be obtained using the command:
docker pull ubuntu:latest
Step 1: Locate the Windows USB Device Path
USB device identifiers on Windows are not directly compatible with Linux. Identify the correct path using Windows PowerShell or Command Prompt.
- Open a PowerShell or Command Prompt window with administrative privileges.
- Run the following command to list physical disk drives (for storage devices) or COM ports:
# For disk-like USB devices (e.g., flash drives)
wmic diskdrive list brief
# For serial USB devices (e.g., Arduino, USB-to-Serial adapters)
mode
- Note the physical ID (e.g.,
\\.\PHYSICALDRIVE1) or COM port number (e.g.,COM3).
Step 2: Start the Container with Device Mapping
To pass the USB device into the container, the --device flag is used. The syntax maps the Windows device path to a corrresponding node within the container's /dev directory.
The basic command structure is:
docker run -it --device=<windows_device_path>:<container_device_path> ubuntu
Replace the placeholders as follows:
<windows_device_path>: Use the path discovered in Step 1.<container_device_path>: Choose an appropriate Linux device file path in side the container, commonly/dev/sda1for a disk partition or/dev/ttyUSB0for a serial device.
Example for a serial device on COM3:
docker run -it --device=\\.\COM3:/dev/ttyUSB0 ubuntu
Example for a physical disk (e.g., PHYSICALDRIVE1):
docker run -it --device=\\.\PHYSICALDRIVE1:/dev/sdb ubuntu
After execution, you will be inside the Ubuntu container's shell. Verify the device is acessible:
# Check for the device file
ls -l /dev/sdb # or /dev/ttyUSB0, etc.
Accessing USB via Raw Bus Path (Alternative Method)
If direct mapping fails, you can attempt to mount the entire USB bus using a volume bind mount, which exposes the Windows host's USB device tree to the container. This requires enabling the --privileged flag for elevated container permissions.
docker run -it --privileged -v /dev/bus/usb:/dev/bus/usb ubuntu
Inside the container, tools like lsusb can then be used to list connected USB devices.
apt-get update && apt-get install -y usbutils
lsusb
Notes on Permissions and Use
- The
--privilegedflag grants extensive system access to the container, which can be a security risk. Use it only in trusted development environments. - Device file permissions within the container may require adjustment using
chmodif the application running inside lacks sufficient access rights. - The specific device node name (
/dev/ttyUSB0,/dev/sdb1, etc.) can vary. Usels /dev/inside the running container to identify the correct node after attachment.