出现了什么问题
最近要整理下pytest-xdist方面的资料,但是尝试通过虚拟机远程连接模拟器的时候,都是报如下的超时:
解决思路
思路一:换模拟器,失败
因为我用的逍遥模拟器,尝试换成夜神模拟器,连接超时
思路二:把桥接ip换成和虚拟机一样的ip地址,失败
这种直接会报一个连接拒绝
思路三:把桥接静态ip换成DHCP,失败
DHCP是动态分配的ip,所以和静态ip的效果差不多,连接超时
思路四:重启路由器,失败
连接超时
思路五:删除旧容器,创建新容器
就在这一步执行的过程中,偶然发现了异常之处,这里贴下源码看下
[root@localhost ~]# docker run --privileged -d -p 4725:4723 --name appium appium/appium
**WARNING: IPv4 forwarding is disabled. Networking will not work.**
6a5cfdea75630c0fe4494de6ea28e4e9c31b1a93f3b11adf627e92486fcaa980
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a5cfdea7563 appium/appium "/bin/sh -c '/root/w…" 28 seconds ago Up 3 seconds 4567/tcp, 0.0.0.0:4725->4723/tcp appium
[root@localhost ~]# docker exec -it appium adb connect 192.168.0.106:7777
* daemon not running; starting now at tcp:5037
* daemon started successfully
failed to connect to '192.168.0.106:7777': Connection timed out
[root@localhost ~]# vim /usr/lib/sysctl.d/00-system.conf
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a5cfdea7563 appium/appium "/bin/sh -c '/root/w…" 6 minutes ago Up 5 minutes 4567/tcp, 0.0.0.0:4725->4723/tcp appium
[root@localhost ~]# systemctl restart network
[root@localhost ~]# docker rm -f appium
appium
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc5f8a08abd1 centos:7.6.1810 "/bin/bash" 2 months ago Exited (137) 2 months ago elastic_cori
[root@localhost ~]# docker run --privileged -d -p 4725:4723 --name appium appium/appium
9e4cb3a76ab1f8d79c07472e6eb673d3886606c1ce1d4cdcc88fe57f42f3a34f
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e4cb3a76ab1 appium/appium "/bin/sh -c '/root/w…" 5 seconds ago Up 4 seconds 4567/tcp, 0.0.0.0:4725->4723/tcp appium
[root@localhost ~]# docker exec -it appium adb connect 192.168.0.106:7777
* daemon not running; starting now at tcp:5037
* daemon started successfully
connected to 192.168.0.106:7777
[root@localhost ~]#
这里的错误是WARNING: IPv4 forwarding is disabled. Networking will not work
,这是IPV4转发功能已经禁用,会导致主机中的容器无法与其他主机进行通信
解决办法
[root@localhost ~]# vi /usr/lib/sysctl.d/00-system.conf
# 写入如下配置,保存退出
net.ipv4.ip_forward=1
#重启网络服务
[root@localhost ~]# systemctl restart network
从上面贴的代码可以看到,连接成功了
注意
-
夜神模拟器重启后设置的静态ip和实际获得的ip不一样
-
逍遥模拟器设置的静态ip和实际获得的ip一样,但首先要切换MAC地址,再重启
-
设置桥接不能和虚拟机的ip相同,否则会出现两个问题:一个是把xshell连接断开,一个是docker appium adb连接模拟器时被拒绝
参考文章
《docker : 报错 WARNING: IPv4 forwarding is disabled. Networking will not work.》