Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing a serial USB device to a container

What is the best way to expose a USB device (/dev/cu.usbserial) to a running Docker container? Is it possible to alias the device to a different name within the container similar to how port mapping works? I am trying to read and write to the serial device (flash firmware) from the container since I have all the tools within the container. I have outlined everything that I have tried so far below.

Trial 1: Using --device

Using the --device option, I receive the error: docker: Error response from daemon: error gathering device information while adding custom device "/dev/cu.usbserial": no such file or directory.

Trial 2: Using --mount & --volume

After adding the /dev path to File Sharing under preferences, I tried the following command:

docker run -it --rm --volume $(pwd):/home/app/ --volume /dev/cu.usbserial:/dev/cu.usbserial --entrypoint "/bin/bash" container:tag

But this command actually just hangs forever. And using the --mount analog below:

docker run -it --rm --volume $(pwd):/home/app/ --mount type=bind,source=/dev/cu.usbserial,target=/dev/cu.usbserial --entrypoint "/bin/bash" container:tag

I receive the following error: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /dev/cu.usbserial.

Trial 3: Using symlink

I also tried to symlink the device to the directory that I attach to the container, but the container is not able to follow the link.

Edit (29/6/2020)

As @Peaceful James points out, if your host machine runs OSX, there is no current solution to this problem. The issue is being tracked here.

like image 245
JustKash Avatar asked Sep 06 '25 13:09

JustKash


1 Answers

Try running your docker container in privileged mode:

sudo docker run -it --rm --volume $(pwd):/home/app/ --privileged --entrypoint "/bin/bash" container:tag

See here for details: https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities

NB: this won't work on "Docker for Mac". See here: https://github.com/docker/for-mac/issues/900

like image 179
Peaceful James Avatar answered Sep 09 '25 19:09

Peaceful James