On OSX, a docker stack is a bit like an onion. There are a couple of layers between you and your docker container. Boot2Docker is a great little utility that installs all the different layers for us. As a quick overview, from the outside in we have:

Gif animation of how to install the virtual box extension pack.
  1. Your mac. It doesn’t have the necessary goodies to run a Docker host natively.
  2. Boot2Docker installs VirtualBox, this hosts a virtual machine that can run the Docker host.
  3. The boot2docker-vm, A Lightweight virtual machine designed specifically to run Docker containers.
  4. Your docker container. A self-contained environment to run your application.

For Android development we need to be able to get applications from our docker containers into phones and tables. Extra configuration is needed for docker containers to access USB devices.

By default, VirtualBox doesn’t come with USB devices support. The VirtualBox extension pack adds USB support, letting you connect an Android to VirtualBox:

  1. Download the extension pack.
  2. Open up the VirtualBox GUI.
  3. Select ‘VirtualBox -> Preferences’ from the menu.
  4. Select the ‘Extensions’ icon.
  5. Press the funny lookin’ ‘yellow square superimposed on the blue box’ button.
  6. Browse to your downloaded extension pack, and press open.
Gif animation of how to install the virtual box extension pack.

Next we need to give the boot2docker-vm access to our Android device:

  1. Plug your Android into an available USB slot.
  2. Make sure the boot2docker-vm is ‘Powered Off’.
  3. Right click on the boot2docker-vm and select ‘Settings…’
  4. Select the ‘Ports’ icon.
  5. Select the ‘USB’ tab.
  6. Make sure both ‘Enable USB Controller’ and ‘Enable USB 2.0 (EHCI) Controller’ is checked.
  7. Press the ‘green plus superimposed on blue USB plug’ button.
  8. Select the Android device from the list. For me this was ‘Sony D2005’.
  9. Hit OK.
Gif animation of how to share android usb.

Copy this Dockerfile into your Android project. When you build the image, this will install all the Android prerequisites and the SDK. Set all your environment variables and update the SDK with some tooling. Finally it will copy your Android application from OSX into the docker image and compile it.

From the root of your Android project, the image is built with:

$ docker build -t your-android-project .

Now to ‘spin up a container’ on your Android. Well kind of. Docker won’t run a container on the Android. The docker container just uploads your application:

$ docker run --privileged -v /dev/bus/usb:/dev/bus/usb -i -t your-android-project adb install -r bin/your-android-project-debug.apk

The –privileged and -v /dev/bus/usb:/dev/bus/usb are the important options that allow your docker container to access USB devices ‘attached’ to the boot2docker virtual machine.