Error: Unhandled error: Error: connect ECONNREFUSED at errnoException (net.js:904:11)

I am creating appium setup using docker where I have the appium server on one container and adb serve on different container. I need this so that I can multiple appium server connecting to on adb server where all the devices are connected.

To trick appium to use the remote server I do:

  1. On the appium server rename the adb file to adb_original

  2. Replace adb file with text file with command:

    #!/bin/bash
    adb_original -H $ADB_HOST -P 8037 $*

When I run a sample test I get error: error: Unhandled error: Error: connect ECONNREFUSED

Full log:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.saucelabs.appium.AndroidContactsTest
2015-03-03 15:50:38:681 - info: --> POST /wd/hub/session {"desiredCapabilities":{"platformVersion":"4.4","app":"/home/appium/sample-code-master/sample-code/examples/java/junit/../../../apps/ContactManager/ContactManager.apk","platformName":"Android","deviceName":"Android Emulator","appActivity":".ContactManager","appPackage":"com.example.android.contactmanager"}}
2015-03-03 15:50:38:683 - info: Client User-Agent string: Apache-HttpClient/4.3.3 (java 1.5)
2015-03-03 15:50:38:685 - info: [debug] Using local app from desired caps: /home/appium/sample-code-master/sample-code/apps/ContactManager/ContactManager.apk
2015-03-03 15:50:38:686 - info: [debug] Creating new appium session 8c95d1b5-ad5e-46ff-904b-85752cc19682
2015-03-03 15:50:38:686 - info: Starting android appium
2015-03-03 15:50:38:687 - info: [debug] Getting Java version
2015-03-03 15:50:38:740 - info: Java version is: 1.7.0_75
2015-03-03 15:50:38:741 - info: [debug] Checking whether adb is present
2015-03-03 15:50:38:741 - info: [debug] Using adb from /opt/android-sdk-linux/platform-tools/adb
2015-03-03 15:50:38:742 - info: [debug] Using fast reset? false
2015-03-03 15:50:38:742 - info: [debug] Preparing device for session
2015-03-03 15:50:38:742 - info: [debug] Checking whether app is actually present
2015-03-03 15:50:38:742 - info: Retrieving device
2015-03-03 15:50:38:743 - info: [debug] Trying to find a connected android device
2015-03-03 15:50:38:743 - info: [debug] Getting connected devices...
2015-03-03 15:50:38:744 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb devices
2015-03-03 15:50:38:752 - info: [debug] 1 device(s) connected
2015-03-03 15:50:38:752 - info: Found device ZX1G424XWQ
2015-03-03 15:50:38:753 - info: [debug] Setting device id to ZX1G424XWQ
2015-03-03 15:50:38:753 - info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
2015-03-03 15:50:38:754 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ wait-for-device
2015-03-03 15:50:38:762 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ shell "echo 'ready'"
2015-03-03 15:50:38:835 - info: [debug] Starting logcat capture
2015-03-03 15:50:38:964 - info: [debug] Getting device API level
2015-03-03 15:50:38:965 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ shell "getprop ro.build.version.sdk"
2015-03-03 15:50:39:036 - info: [debug] Device is at API Level 21
2015-03-03 15:50:39:037 - info: Device API level is: 21
2015-03-03 15:50:39:038 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ shell "getprop persist.sys.language"
2015-03-03 15:50:39:102 - info: [debug] Current device persist.sys.language: en
2015-03-03 15:50:39:103 - info: [debug] java -jar "/opt/appium/node_modules/appium/node_modules/appium-adb/jars/appium_apk_tools.jar" "stringsFromApk" "/home/appium/sample-code-master/sample-code/apps/ContactManager/ContactManager.apk" "/tmp/com.example.android.contactmanager" en
2015-03-03 15:50:39:306 - info: [debug] No strings.xml for language 'en', getting default strings.xml
2015-03-03 15:50:39:306 - info: [debug] java -jar "/opt/appium/node_modules/appium/node_modules/appium-adb/jars/appium_apk_tools.jar" "stringsFromApk" "/home/appium/sample-code-master/sample-code/apps/ContactManager/ContactManager.apk" "/tmp/com.example.android.contactmanager"
2015-03-03 15:50:39:519 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ push "/tmp/com.example.android.contactmanager/strings.json" /data/local/tmp
2015-03-03 15:50:39:656 - info: [debug] Checking whether aapt is present
2015-03-03 15:50:39:656 - info: [debug] Using aapt from /opt/android-sdk-linux/build-tools/21.1.2/aapt
2015-03-03 15:50:39:657 - info: [debug] Retrieving process from manifest.
2015-03-03 15:50:39:657 - info: [debug] executing cmd: /opt/android-sdk-linux/build-tools/21.1.2/aapt dump xmltree /home/appium/sample-code-master/sample-code/apps/ContactManager/ContactManager.apk AndroidManifest.xml
2015-03-03 15:50:39:662 - info: [debug] Set app process to: com.example.android.contactmanager
2015-03-03 15:50:39:662 - info: [debug] Not uninstalling app since server not started with --full-reset
2015-03-03 15:50:39:663 - info: [debug] Checking app cert for /home/appium/sample-code-master/sample-code/apps/ContactManager/ContactManager.apk.
2015-03-03 15:50:39:663 - info: [debug] executing cmd: java -jar /opt/appium/node_modules/appium/node_modules/appium-adb/jars/verify.jar /home/appium/sample-code-master/sample-code/apps/ContactManager/ContactManager.apk
2015-03-03 15:50:39:794 - info: [debug] App already signed.
2015-03-03 15:50:39:794 - info: [debug] Zip-aligning /home/appium/sample-code-master/sample-code/apps/ContactManager/ContactManager.apk
2015-03-03 15:50:39:795 - info: [debug] Checking whether zipalign is present
2015-03-03 15:50:39:796 - info: [debug] Using zipalign from /opt/android-sdk-linux/build-tools/21.1.2/zipalign
2015-03-03 15:50:39:796 - info: [debug] Zip-aligning apk.
2015-03-03 15:50:39:797 - info: [debug] executing cmd: /opt/android-sdk-linux/build-tools/21.1.2/zipalign -f 4 /home/appium/sample-code-master/sample-code/apps/ContactManager/ContactManager.apk /tmp/11523-2627-zptkh5/appium.tmp
2015-03-03 15:50:39:804 - info: [debug] MD5 for app is b2d2916bb5388e1dc281ec3e71ef1234
2015-03-03 15:50:39:805 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ shell "ls /data/local/tmp/b2d2916bb5388e1dc281ec3e71ef1234.apk"
2015-03-03 15:50:39:905 - info: [debug] Getting install status for com.example.android.contactmanager
2015-03-03 15:50:39:906 - info: [debug] Getting device API level
2015-03-03 15:50:39:906 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ shell "getprop ro.build.version.sdk"
2015-03-03 15:50:39:989 - info: [debug] Device is at API Level 21
2015-03-03 15:50:39:990 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ shell "pm list packages -3 com.example.android.contactmanager"
2015-03-03 15:50:40:581 - info: [debug] App is installed
2015-03-03 15:50:40:582 - info: [debug] Forwarding system:4724 to device:4724
2015-03-03 15:50:40:582 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ forward tcp:4724 tcp:4724
2015-03-03 15:50:40:593 - info: [debug] Pushing appium bootstrap to device...
2015-03-03 15:50:40:593 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ push "/opt/appium/node_modules/appium/build/android_bootstrap/AppiumBootstrap.jar" /data/local/tmp/
2015-03-03 15:50:40:712 - info: [debug] Pushing settings apk to device...
2015-03-03 15:50:40:713 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ install "/opt/appium/node_modules/appium/build/settings_apk/settings_apk-debug.apk"
2015-03-03 15:50:42:106 - info: [debug] Pushing unlock helper app to device...
2015-03-03 15:50:42:107 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ install "/opt/appium/node_modules/appium/build/unlock_apk/unlock_apk-debug.apk"
2015-03-03 15:50:43:419 - info: Starting App
2015-03-03 15:50:43:419 - info: [debug] Attempting to kill all 'uiautomator' processes
2015-03-03 15:50:43:420 - info: [debug] Getting all processes with 'uiautomator'
2015-03-03 15:50:43:420 - info: [debug] executing cmd: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ shell "ps 'uiautomator'"
2015-03-03 15:50:43:548 - info: [debug] No matching processes found
2015-03-03 15:50:43:549 - info: [debug] Running bootstrap
2015-03-03 15:50:43:549 - info: [debug] spawning: /opt/android-sdk-linux/platform-tools/adb -s ZX1G424XWQ shell uiautomator runtest AppiumBootstrap.jar -c io.appium.android.bootstrap.Bootstrap
2015-03-03 15:50:44:365 - info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
2015-03-03 15:50:44:365 - info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
2015-03-03 15:50:44:365 - info: [debug] [UIAUTOMATOR STDOUT] io.appium.android.bootstrap.Bootstrap:
2015-03-03 15:50:44:365 - info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
2015-03-03 15:50:44:366 - info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
2015-03-03 15:50:44:366 - info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
2015-03-03 15:50:44:366 - info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
2015-03-03 15:50:44:367 - info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 1
2015-03-03 15:50:44:395 - info: [debug] [BOOTSTRAP] [debug] Socket opened on port 4724
2015-03-03 15:50:44:395 - info: [debug] [BOOTSTRAP] [debug] Appium Socket Server Ready
2015-03-03 15:50:44:395 - info: [debug] [BOOTSTRAP] [debug] Loading json...
2015-03-03 15:50:44:395 - info: [debug] [BOOTSTRAP] [debug] json loading complete.
2015-03-03 15:50:44:397 - error: Unhandled error: Error: connect ECONNREFUSED
    at errnoException (net.js:904:11)
    at Object.afterConnect [as oncomplete] (net.js:895:19) context: [POST /wd/hub/session {"desiredCapabilities":{"platformVersion":"4.4","app":"/home/appium/sample-code-master/sample-code/examples/java/junit/../../../apps/ContactManager/ContactManager.apk","platformName":"Android","device]
2015-03-03 15:50:44:398 - info: [debug] [BOOTSTRAP] [debug] Registered crash watchers.

If appium uses the adb in the same container the issue is not reproduced.

Searching on the internet I found https://groups.google.com/forum/#!topic/appium-discuss/N00BmBX-Jf8, but the solution is not very clear, it just states that the issue was with some Android settings.

Has anyone else had this problem?

The only time I had this issue was when my device did not have ‘USB debugging’ turned on under developer options.