Connecting to a USB Android device in a Docker con

2020-02-08 04:07发布

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?

6条回答
forever°为你锁心
2楼-- · 2020-02-08 04:32

Update:

I ended up using --privileged and -v to map the whole of /dev/bus/usb and patching adb to accept one environment variable to specify the root USB device tree - /dev/bus/usb/001 etc.

This allowed us to use different USB busses for different containers for different groups of the same phone, and another environment variable patch allowed different VID:PID lists for different types of phone.


We're trying to allocate different USB buses to different Docker containers running TeamCity clients.

Each container needs ANDROID_ADB_SERVER_PORT setting to a different port (because we're not using segregated networking).

The host machine can't run adbd, because only one adbd can talk to a phone at any given time.

Each container gets one of the /dev/bus/usb/xxx directories, so we can plug phones into particular containers.

We have to synchronise the /dev/bus/usb/xxx directory every few seconds, to allow hot-plugging and reboots - just a shell loop on the host that runs tar cf devxxx.tar /dev/bus/usb/xxx, docker cp to transfer it, then docker exec to untar inside the container's /tmp, diff to detect nodes to delete, and mv -n to move new nodes in.


In fact, because we're running on Linux, we can probably just set up udev scripts, per Howto run a script when a USB device is pluged in.

查看更多
闹够了就滚
3楼-- · 2020-02-08 04:40

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....

查看更多
够拽才男人
4楼-- · 2020-02-08 04:41

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.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-02-08 04:43

Excuse me for offtop. But I cannot answer to this question so I write it down here.

I am using ubuntu:18.04 and tried to connect my Samsung Galaxy A3 2016 and debug it with ADB. Devices was unauthorized and no dialog popped up on the screen!

How I solved the problem:

I was interested will the problem still occure inside a Docker container? The answer was "No!".

  1. Inside a Docker container, that I invoked with following: (this is the answer for this topic :) )

    docker run --name android-adb -it -d --privileged -v /dev/bus/usb:/dev/bus/usb ubuntu:16.04 bash
    
  2. I downloaded platform-tools (distribution with ADB). Added its path to environment. And restarted bash

  3. After restarting ADB server inside Docker container AND killing ADB server on host machine. I found out that confirmation dialog appeared on the smartphone's screen!

So on the host machine the problem was in .android directory I think. Just move it to .android-backup. The next adb start-server command will create new one. Also I replaced platform-tools folder with most recent

查看更多
Rolldiameter
6楼-- · 2020-02-08 04:44

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

查看更多
放我归山
7楼-- · 2020-02-08 04:44

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.

查看更多
登录 后发表回答