无法从Docker容器连接到主机ADB

时间:2020-01-12 17:21:59

标签: docker networking adb

在这是我的情况之前,请先举例说明我的环境,以更好地了解我的问题

enter image description here

我的机器上运行的是一个docker容器,其配置如下docker-compose

services: 
  addb-client:
    build:
     context: .
     dockerfile: Dockerfile
    ports:
      - 19000:19000
      - 19001:19001
      - 19002:19002
    tty: true
    volumes: 
      - ".:/code"

从我的host中执行以下命令

adb -a -P 5037 server nodaemon

然后我得到以下输出

adb I 01-12 11:06:05  2493  2493 auth.cpp:437] adb_auth_init...
adb I 01-12 11:06:05  2493  2493 auth.cpp:412] adb_auth_inotify_init...
adb I 01-12 11:06:05  2493  2499 transport.cpp:295] emulator-5554: write thread spawning
adb I 01-12 11:06:05  2493  2498 transport.cpp:282] emulator-5554: read thread spawning
adb I 01-12 11:06:05  2493  2493 transport.cpp:1373] fetching keys for transport emulator-5554
adb I 01-12 11:06:05  2493  2493 auth.cpp:489] Calling send_auth_response
adb I 01-12 11:06:05  2493  2493 adb.cpp:114] emulator-5554: offlin

然后从我的泊坞窗container中运行

adb connect yyy.yyy.yyy.yyy

yyy.yyy.yyy.yyy是我的Android仿真器地址,如图所示

failed to connect to 'yyy.yyy.yyy.yyy:5555': Connection timed out

但是我超时了

我尝试过

adb connect xxx.xxx.xxx.xxx

但是我得到了connection refused

注意:

1。容器中的命令,例如

adb -H yyy.yyy.yyy.yyy devices

效果很好。

如果您能为我提供任何帮助,我将不胜感激。如果需要任何其他信息,请告诉我。谢谢。

2 个答案:

答案 0 :(得分:0)

这似乎是一个网络问题。 要从Docker容器中访问主机,您有2个选项:

  1. 访问主机上的docker bridge接口的ip

    $ docker run -ti busybox /bin/sh
    / # netstat -rn | grep ^0.0.0.0 | awk '{print $2}'
    172.17.0.1
    / # nc -vz 172.17.0.1 5037
    172.17.0.1 (172.17.0.1:5037) open
    
  2. 向docker运行时添加参数,以通过'localhost'关键字提供从容器到主机的访问权限

    $ docker run -ti busybox /bin/sh
    / # nc -vz localhost 5037 ### NOT WORKING
    
    $ docker run -ti --net="host" busybox /bin/sh
    / # nc -vz localhost 5037
    localhost (127.0.0.1:5037) open  ### WORKING
    

docker-compose等效项是:

network_mode: host

答案 1 :(得分:0)

如果您在主机上收到键入adb devices的设备列表,并且希望在容器的控制台中使用相同的命令运行该列表,请尝试使用 ssh

请检查this answer或此repo

相关问题