Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to a USB Android device in a Docker container via ADB

I have created a Docker image which contains the Android SDK and am trying to expose my Android phone in a container running this image. So I used the --privileged flag and mounted the USB devices as follows:

$ docker run --privileged -v /dev/bus/usb:/dev/bus/usb -d -P my-android:0.0.1

However, when I run ADB devices, it does not show me the USB device:

ubuntu@d56b666be455:~/Android/Sdk/platform-tools$ ./adb devices
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached

ubuntu@d56b666be455:~/Android/Sdk/platform-tools$

lsusb inside the container lists the device:

ubuntu@d56b666be455:~$ lsusb
...
Bus 002 Device 017: ID 04e8:6866 Samsung Electronics Co., Ltd GT-I9300 Phone [Galaxy S III] (debugging mode)

The device is however visible on the host:

⇒  ./adb devices
List of devices attached
4d11abcd65b74045    device

Host OS

$ uname -a
Linux ananya 3.16.0-33-generic #44~14.04.1-Ubuntu SMP Fri Mar 13 10:33:29 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Docker version

$ docker --version
Docker version 1.5.0, build a8a31ef

What could be the issue?

like image 317
buzypi Avatar asked Apr 10 '15 13:04

buzypi


4 Answers

I don't think the ADB daemon running on the device can be connected to two ADB servers. Try disconnecting it from your host machine's ADB and then connect it to the Docker container's ADB.

like image 66
jlhonora Avatar answered Oct 11 '22 02:10

jlhonora


While I was trying the same, I ran into some other problems related to that, which I would like to share so that others may save their time:

Problem 1: lsusb was not installed in the container

In my case lsusb was not installed, so I installed it with the below command:

apt-get update apt-get install usbutils 

Problem 2: not able to see the device even after lsusb and ADB SDK installation

You need to restart your container with the -v option, and yes don't forget to kill the ADB server as stated in one of the answers.

On the host:

adb-kill server docker run -ti -d --privileged -v /dev/bus/usb:/dev/bus/usb   container_name 

In case someone wanted do it from scratch, I have written a blog post on it:

How to connect ADB devices to Linux container

like image 25
pankaj mishra Avatar answered Oct 11 '22 04:10

pankaj mishra


This doesn't answer the exact question you were asking, but does address what you were trying to accomplish - connecting to an android device connected to a docker host from an adb client running inside a docker container. I'm including this for anyone trying to accomplish the same thing (like I was).

The adb client supports a -H option which tells it where to find the adb server to connect to. Docker supports the hostname "host.docker.internal" which always maps to the IP address of the docker host. Assuming your device is connected to the docker host, you can do the following to get your containerized adb client to connect to the adb server running on the docker host:

adb -H host.docker.internal devices

Accomplishes the goal without having to mount the USB ports.

Reference: https://developer.android.com/studio/command-line/adb

Update: I recently learned that host.docker.internal is only supported on Docker for Mac in versions 18.0 and above.

like image 39
RChavez Avatar answered Oct 11 '22 02:10

RChavez


Running with just --privileged -v /dev/bus/usb:/dev/bus/usb did not work for me. I tried forwarding the adb daemon's listening port using -p 5037:5037, but that did not help either.

It worked only after I added --net host. This means the host machine's net interfaces are exposed to the docker so use it if you are fine with that. Maybe there are more ports that needed to be forwarded other than 5037....

like image 42
Wildsheep Avatar answered Oct 11 '22 03:10

Wildsheep