Expose USB to docker through Dockerfile

I’m have Dockerfile that run redis on one image, and python code on other on
For now I’m working on windows 10

I’m trying to connect USB port that connect to local comp to my python app on docker through pySerial

That’s my docker file
FROM python:2.7
COPY . /app
RUN pip install -r requirements.txt
CMD [“python”, “main_2.py”]

and the error that I get when I tries to run main_2.py on docker
is that:
Traceback (most recent call last):
File “main_2.py”, line 1, in
from rfbridge import RFBridge
File “/app/rfbridge.py”, line 9, in
ser = serial.Serial(COM,115200)
File “/usr/local/lib/python2.7/site-packages/serial/serialutil.py”, line 240, in init
File “/usr/local/lib/python2.7/site-packages/serial/serialposix.py”, line 268, in open
raise SerialException(msg.errno, “could not open port {}: {}”.format(self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: ‘/dev/ttyUSB0’

I’ll be very great full for your help

I doesn’t work. Docker Desktop runs in a Hyper-V VM, you can’t connect a hardware device to it.


Is this possible from a linux docker host do you know?

I would like to use PySerial from a container to access the host’s serial ports. Any insights are greatly appreciated.


Yes it is possible. Read under Runtime privilege and Linux capabilities in the docs for docker run. There is an example how to access the sound card.
In a compose file you can use the devices option.

Thanks @tekki. I am happy to find that this is possible.

I also found this excellent conversation on stackoverflow that covers the capabilities and shortcomings of available approaches.

It also seems possible on Windows if using the older docker toolkit, by configuring the default VM to access the proper USB driver.