How to use a host USB device in a container in Docker Desktop?

Kindly take a note of each and every point I will be sending

  1. I have two docker images one is of mysql headlessev-302-evmysqlcont and other is springboot application where its base image is debian-10.6 name is headlessev-302-projcont

  2. these two images are in two containers but they are in same network

  3. My host system is windows machine

  4. one of my docker container is of debian based
    5 . I have written docker-compose file to run my both containers for building i use docker-compose build and run i use docker-compose up
    6.now my host system is connected to external vector hardware using usb now how can my docker container can detect that usb ?
    7.use this information if required
    Vector-Hardware
    Vector Informatik GmbH
    Port_#0001.Hub_#0002
    This is my device instance path USB\VID_1248&PID_1001\5&271999B4&0&1
    This is my hardware id USB\VID_1248&PID_1001&REV_0001

  5. Docker file for mysql application
    FROM mysql:latest
    ENV MYSQL_ROOT_PASSWORD=‘PASSWORD’
    COPY mysqlinit.sql /docker-entrypoint-initdb.d/
    EXPOSE 3306

  6. docker-compose.yml file
    version: “1.0”
    services:
    projcont:
    build:
    context: .
    dockerfile: Dockerfile
    ports:

    • 27997:27997
    • 5005:5005
    • “127.0.0.1:4500:4500”

    depends_on:

    • evmysqlcont

    devices:

    - “/dev/bus/usb:/dev/bus/usb” # Mount the entire USB bus

    devices:

    - “//./USB/VID_1248&PID_1001/5&271999B4&0&1”

    cap_add:

    - SYS_ADMIN

evmysqlcont:
build:
context: mysqlContainer
dockerfile: Dockerfile
ports:
- 27999:3306

Can anyone help how external hardware connected to windows can be detected in debian docker container ?

When running Docker on Windows, do you use Docker Desktop? That creates a Linux VM in which containers are usually run, so you need to attach the USB device to the VM, too.

1 Like

According official Docker Desktop FAQs, it is not possible to pass through a USB device:

Though, you could try if you get https://github.com/dorssel/usbipd-win working somehow.

1 Like

Hello,

Accessing external vector hardware in a Docker container is a bit like connecting your computer to a special tool.
Let’s understand with an example, Imagine you have a cool gadget outside your house, and you want to use it on your computer. To make it work inside a Docker container, you have to set up a sort of digital tunnel, so your computer can talk to the gadget. It’s like creating a secret pathway. In tech terms, you might need to use specific commands or configurations to tell the Docker container how to reach out and grab information from the external hardware. Think of it as helping your computer and the gadget have a smooth conversation, even though they’re in different places. Just like giving your computer the right map to find and use the cool tool outside.

I hope this will help you.

Regards

1 Like

Yes I use docker desktop


Yes I need to attach the usb to VM too
or can you recommend any tool like VM Box where i can use docker and usb both

This really helped a lot Thanks for giving the clear idea can you suggest any way like that so that I can communicate with the external hardware any suggestions

Thanks for the reply will try to implement this method But I don’t think it might work
Is it possible to pass through usb from linux host to linux container is that possible ?

Of course, as long as you run docker-ce, and not Desktop Desktop.

Can you send the way how to use that ?

Apologies, my wording was kind of ambiguous with run I mean install and run dock-ce.

You can find the official installation instructions here:

USBIP (USB over IP) will definitely work, you can specify the host server (where the USB devices reside) only any machine (Windows, Linux, Mac, etc), which includes the Windows Host of a WSL2 setup.

You then use a usbip client on the target device to bind/import the remote USB resource, will appear as a local USB device. USBIP exchanges URB (USB Request Blocks) between the server/client, which are then “passed up/down” the USB driver stack as if the device was local.

So the steps are:

  1. From the “server”, publish (bind) the USB devices
  2. From the WSL2 distro, attach to the published devices (specifying host/port if not default)
  3. From the docker container, map the devices per the names in the WSL2 distro attach above.

The one problem is if the USB device is unplugged you need to re-attach via the WSL2 distro and you won’t be guaranteed the device name will be the same (in most cases it won’t). There are ways of using the cgroups to ensure consistent name mapping, or take a look at the python package serial-usbipclient for how to directly connect any Python instance to a USB serial device hosted by a USBIP server.

Good luck!

1 Like

Thank you for the input. It is known to work with WSL2 distros, though did you manage to make it work with Docker Desktop for Windows, where the distro is more like an appliance? If so, can you share the exact steps necessary to make containers in Docker Desktop for Windows use usb devices?

Metin,

No, I was using a docker-desktop-less implementation.

My last project was a scientific instrument, we chose to host the docker containers on a Windows (10) computer with docker services running on the host (Windows) and the WSL2 distro. We could use the “normal” docker commands and docker-compose to run our containers (both Windows and Linux containers).

The h/w was connected to the host (Windows) and using usbipd-win “published” (shared). An initialization script in the WSL2 distro would attach the published USB ports (3 in total) and assign them to local devices (e.g. /dev/ttyACM0, /dev/ttyACM1, /dev/ttyACM2). When the docker container was launched, it would specify the mapping of the WSL2 distro device to the container.

Problem with this approach is if the devices are unplugged they will re-appear with different device names (e.g. /dev/ttyACM3). Since the binding of the device happens when the docker container is launched, there’s no way to re-map the device.

There are two solutions. One is to re-map the device using scripts that fiddle with the cgroup, the other is to directly attach to the usbipd server (it’s just tcp/ip) and use a network connection within the container. This is what the package serial-usbipclient does, eliminating all the device mapping, you just have to “publish” the devices from the host.

Docker Desktop runs on top of docker, but is limited as running containers from different platforms (Windows & Linux) isn’t well supported. But, if you are just running linux containers you can do all that I previously mentioned.

Let me know if you have any other questions.

Harry

1 Like