Accessing an Android Service from Docker Using ADB Port Forwarding

Android and Docker logos

You may come across a situation where you need to access a service running within Android. This can be done using ADB port forwarding and Genymotion. However, you may experience an issue when trying to access an Android service from a remote application. This post will cover how to address this issue while using an example of Drozer running in a Docker container on WSL 2.

The Problem

I experienced an issue when trying to connect to a Drozer agent (running in Genymotion) using the Drozer console that was running in a Docker container. To connect to the Drozer agent, you would first need to configure port forwarding using ADB: adb forward tcp:1234 tcp:31415. This would forward port 1234 on the host machine to port 31415 on the Android virtual device and would listen on 127.0.0.1. As a result, the Drozer agent would only be accessible from the host machine and not from the Docker container. This can be verified by running netstat -ano | findstr :1234

netstat localhost

The Solution

The solution would be to make the ADB server listen on all interfaces (ADB has a global flag for this: -a). This should be done on your own network as the forwarded port will be accessible to anyone on the network. However, you can configure a firewall rule on your host machine to restrict access. Viewing the Details tab in Task Manager with the Command line column enabled, indicated that the Genymotion ADB server was running without the -a flag, which was why the ADB was listening on 127.0.0.1.

Task manager

Looking at the Genymotion settings, I did not find a setting to configure the ADB server to run using the -a flag. Additionally, attempting to terminate the ADB server by running adb kill-server didn’t work as the ADB process kept on respawning while Genymotion was running.

The below steps indicate how to address this issue:

  • Make sure that Genymotion and ADB are not running (check for running adb.exe processes by using Task Manager and terminate any that exist)
  • Run the following command to start the ADB server so that it listens on all interfaces: adb -a nodaemon server start – Genymotion will use this ADB process instead of spawning the ADB process without the -a flag
  • Start your Android virtual device in Genymotion
  • Configure port forwarding: adb forward tcp:1234 tcp:31415
  • Run netstat -ano | findstr :1234 on your host machine and you should now find that ADB is listening on all interfaces:
netstat any interface

With the port forwarded, you should now be able to access the Android service from your Docker container – in my case, I could connect to the Drozer agent from the Drozer console by running drozer console connect --server 192.168.1.4:1234 where 192.168.1.4 is the IP address of the host machine.