Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Mounting USB Devices in a Docker Ubuntu Container on Windows

Tech 1

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

  1. Docker Desktop for Windows installed and running.
  2. 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.

  1. Open a PowerShell or Command Prompt window with administrative privileges.
  2. 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
  1. 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/sda1 for a disk partition or /dev/ttyUSB0 for 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 --privileged flag 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 chmod if the application running inside lacks sufficient access rights.
  • The specific device node name (/dev/ttyUSB0, /dev/sdb1, etc.) can vary. Use ls /dev/ inside the running container to identify the correct node after attachment.
Tags: docker

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.