Skip to content

Instantly share code, notes, and snippets.

@stonehippo
Last active January 3, 2024 21:18
Show Gist options
  • Star 42 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save stonehippo/e33750f185806924f1254349ea1a4e68 to your computer and use it in GitHub Desktop.
Save stonehippo/e33750f185806924f1254349ea1a4e68 to your computer and use it in GitHub Desktop.
Getting a USB device to show up in a Docker container on OS X

Getting a USB device to show up in a Docker container on OS X

Some background

I was trying to get the Arduino IDE to work inside a Docker container on OS X. I was able to get the IDE working (see Getting X11 GUI applications to work on OS X with Docker), but I could not figure out how to make the USB port for the Arduino visible.

The solution

I first tried to directly map hardware serial port into the Docker container, doing something like this:

$ docker run -it --rm --device=/dev/tty.usbserial ubuntu bash

This failed. After thinking about it, I realized that of course it would. Docker on OS X runs inside of Virtualbox VM. And it's that VM that a Docker container would need to see. Duh.

With this realization, I was able to make this work pretty easily. First, make sure the Docker VM that you want to use has USB enabled. You can see this in the summary info for the VM. If it's not enabled, make sure the VM is stopped, with

$ docker-machine stop [vm name]

Then go into Virtualbox, locate the VM and enter its Settings. From there, go to Ports -> USB and enable the USB controller (I choose USB 2.0). Save the settings, and fire up the VM again with

$ docker-machine start [vm name]

Once the VM is started, open it in Virtualbox by clicking Show or double-clicking it. Once you're in the VM, select Devices -> USB from the menu and enable what devices you want to use from the HOST in the guest (and, by extension, in the Docker container).

Once that's all set, your device should be available in the VM. For example, after following these steps, I could now see the Arduino as /dev/ttyUSB0 in the guest. And now I can map the USB device into the container like this:

$ docker run -it --rm --device=/dev/ttyUSB0 ubuntu bash

A quick test with the screen command showed that the Arduino was now available. Yay!

@anselanza
Copy link

How does this apply now that Docker for Mac no longer uses Virtualbox VM?

@xpe
Copy link

xpe commented Dec 22, 2017

@anselanza Even though Docker for Mac doesn't use VirtualBox by default, you can still use docker-machine to connect to a VirtualBox VM. This provides an alternative where you can have more control over the VM (as explained above, changing the USB settings). Some instructions that may help: https://linoxide.com/linux-how-to/host-virtualbox-docker-machine/

@loretoparisi
Copy link

@xpe thanks for your suggestions. I have created and activated a docker-machine on my macos that is up and running

NAME    ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
macos   -        virtualbox   Running   tcp://192.168.99.100:2376           v17.12.1-ce  

I have then setup VirtualBox image to attach the USB device to it, but I still cannot see any USB attached devices in /dev/, but I have one attached to the mac...

@loretoparisi
Copy link

loretoparisi commented Mar 8, 2018

Ok I think the problem was that the docker-machine was not active (since I cannot see a * under the ACTIVE flag). The correct command to active on macos is

$ eval $(docker-machine env macos)
$ docker-machine ls
NAME    ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER        ERRORS
macos   *        virtualbox   Running   tcp://192.168.99.100:2376           v17.12.1-ce 

At this point it should work...

@lmapii
Copy link

lmapii commented Mar 12, 2018

i'm struggling right now with the same issue > hypervisor won't allow me to use the --devices directive, which is why i switched to docker-machine. however, my usbserial won't show in the USB list (it's /dev/tty.usbmodem[number]). adding it as a COM-port will have the effect that the machine won't start up. any ideas ?

@rubberduck203
Copy link

Strange seeing you here @lmapii. Seems like I’m following your footsteps.

@pklapperich
Copy link

pklapperich commented Oct 22, 2019

@lmapii and @rubberduck203 - Follow the instructions up above. You don't pass the /dev/tty.usb* to the VirtualMachine, you pass the entire USB device to the VirtualMachine and linux in the VM will enumerate it as a /dev/ttyUSB0.

Make sure that after you enable USB 2.0 you also add a device filter (there's a list and a plus icon; add your device to the list). It should look something like this: https://imgur.com/6bAplMq.png

With the USB devices on the filter list and checked, whenever the VM is running those devices will be given to the VM and MacOS won't even show that they exist.

@stonehippo
Copy link
Author

Thanks for following up, @pklapperich. I missed the earlier questions @lmapii and @rubberduck203, so I hope you comment helps them get it working (assuming they’re still trying to!)

@rubberduck203
Copy link

I eventually got it working, but it doesn’t work for all chipsets. For example, I can attach to Arduino clones that use an FTDI chip, but not official Arduinos.

docker/for-mac#900

@stonehippo
Copy link
Author

@rubberduck203 which Arduino models are you having issues with (in case anyone else is trying and failing to get this to work)?

@rubberduck203
Copy link

In particular, the official Arduino Unos, but I would expect difficulty on any model using the ATMEGA16U2 chip for USB communications.

Clones that use an FTDI, like the Sparkfun Redboard work fine.

@lmapii
Copy link

lmapii commented Oct 22, 2019

Oh my, down memory lane ... @pklapperich as @rubberduck203 mentioned it's not really working for all devices. I'm now still sticking to a very specific setup as mentioned here, these were the only versions that would support all devices I'm using within docker in the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment