# Expose USB to docker through Dockerfile

**URL:** <https://forums.docker.com/t/expose-usb-to-docker-through-dockerfile/70459>\
**Category:** General\
**Tags:** docker, build\
**Created:** [March 5, 2019, 7:34pm UTC](https://forums.docker.com/t/expose-usb-to-docker-through-dockerfile/70459 "2019-03-05T19:34:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dormalk](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@dormalk](https://forums.docker.com/u/dormalk)\
**Post date:** [March 5, 2019, 7:34pm UTC](https://forums.docker.com/t/expose-usb-to-docker-through-dockerfile/70459/1 "2019-03-05T19:34:18Z")

</div>

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  
WORKDIR /app  
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**  
self.open()  
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

---

<div class="post-metadata">

**Author:** ![tekki](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/tekki/32/21884_2.png) [@tekki](https://forums.docker.com/u/tekki)\
**Post date:** [March 7, 2019, 7:09am UTC](https://forums.docker.com/t/expose-usb-to-docker-through-dockerfile/70459/2 "2019-03-07T07:09:40Z")

</div>

> [@dormalk](#):
>
> For now I’m working on windows 10  
> I’m trying to connect USB port…

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

---

<div class="post-metadata">

**Author:** ![edwardobjsec](https://avatars.discourse-cdn.com/v4/letter/e/eada6e/32.png) [@edwardobjsec](https://forums.docker.com/u/edwardobjsec)\
**Post date:** [August 26, 2019, 11:01pm UTC](https://forums.docker.com/t/expose-usb-to-docker-through-dockerfile/70459/3 "2019-08-26T23:01:57Z")

</div>

@tekki

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.

Thanks!

---

<div class="post-metadata">

**Author:** ![tekki](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/tekki/32/21884_2.png) [@tekki](https://forums.docker.com/u/tekki)\
**Post date:** [August 27, 2019, 6:31am UTC](https://forums.docker.com/t/expose-usb-to-docker-through-dockerfile/70459/4 "2019-08-27T06:31:50Z")

</div>

Yes it is possible. Read under [Runtime privilege and Linux capabilities](https://docs.docker.com/engine/reference/run/#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](https://docs.docker.com/compose/compose-file/#devices).

---

<div class="post-metadata">

**Author:** ![edwardobjsec](https://avatars.discourse-cdn.com/v4/letter/e/eada6e/32.png) [@edwardobjsec](https://forums.docker.com/u/edwardobjsec)\
**Post date:** [August 28, 2019, 7:35pm UTC](https://forums.docker.com/t/expose-usb-to-docker-through-dockerfile/70459/5 "2019-08-28T19:35:17Z")

</div>

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

I also found this excellent conversation on [stackoverflow](https://stackoverflow.com/questions/24225647/docker-a-way-to-give-access-to-a-host-usb-or-serial-device/53892718#53892718) 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.
