skip to Main Content

I am using an Appium service to automate a flutter application on Android. I was able to connect to my host’s ADB and install Appium Settings and the flutter application. But I was not able to connect to AndroidUiautomator2Driver as shown in the logs below:

[ADB] Running '/usr/lib/android-sdk/platform-tools/adb -P 5037 -s RR8W802EFNN install -r -g /home/myuser/app-debug.apk'
[ADB] The installation of 'app-debug.apk' took 7979ms
[ADB] Install command stdout: Performing Streamed Install
[ADB] Success
[AndroidUiautomator2Driver@9de5 (9d152cd0)] Performing shallow cleanup of automation leftovers
[AndroidUiautomator2Driver@9de5 (9d152cd0)] No obsolete sessions have been detected (connect ECONNREFUSED 127.0.0.1:8200)
[ADB] Running '/usr/lib/android-sdk/platform-tools/adb -P 5037 -s RR8W802EFNN shell am force-stop io.appium.uiautomator2.server.test'
[AndroidUiautomator2Driver@9de5 (9d152cd0)] Starting UIAutomator2 server 5.12.16
[AndroidUiautomator2Driver@9de5 (9d152cd0)] Using UIAutomator2 server from '/home/myuser/.appium/node_modules/appium-flutter-driver/node_modules/appium-uiautomator2-driver/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v5.12.16.apk' and test from '/home/myuser/.appium/node_modules/appium-flutter-driver/node_modules/appium-uiautomator2-driver/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk'
[AndroidUiautomator2Driver@9de5 (9d152cd0)] Waiting up to 30000ms for UiAutomator2 to be online...
[ADB] Creating ADB subprocess with args: ["-P","5037","-s","RR8W802EFNN","shell","am","instrument","-w","-e","disableAnalytics","true","io.appium.uiautomator2.server.test/androidx.test.runner.AndroidJUnitRunner"]
[AndroidUiautomator2Driver@9de5 (9d152cd0)] Matched '/status' to command name 'getStatus'
[AndroidUiautomator2Driver@9de5 (9d152cd0)] Proxying [GET /status] to [GET http://127.0.0.1:8200/status] with no body
[AndroidUiautomator2Driver@9de5 (9d152cd0)] connect ECONNREFUSED 127.0.0.1:8200
[AndroidUiautomator2Driver@9de5 (9d152cd0)] Matched '/status' to command name 'getStatus'
[AndroidUiautomator2Driver@9de5 (9d152cd0)] Proxying [GET /status] to [GET http://127.0.0.1:8200/status] with no body
[AndroidUiautomator2Driver@9de5 (9d152cd0)] connect ECONNREFUSED 127.0.0.1:8200

And this is how I configure the environment in my Dockerfile:

RUN apt-get update  
    && yes | apt-get install -y maven  
    && yes | apt-get install usbutils 
    && apt install wget 
    && yes | apt-get install android-sdk 
    && yes | apt-get install android-tools-adb 
    && apt clean  
    && rm -rf /var/lib/apt/lists/*


ENV ANDROID_HOME "/usr/lib/android-sdk"
ENV ANDROID_SDK_ROOT $ANDROID_HOME
ENV PATH $PATH:$ANDROID_HOME/platforms:$ANDROID_HOME/tools/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/build-tools
ENV ANDROID_ADB_SERVER_ADDRESS "host.docker.internal"


RUN useradd -ms /bin/bash myuser 
    && usermod -aG root myuser

USER myuser

SHELL ["/bin/bash", "--login", "-c"]

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash 
    && source $HOME/.nvm/nvm.sh 
    && nvm install v16.18.1 
    && npm install -g appium@latest 
    && appium driver install uiautomator2  
    && appium driver install xcuitest  
    && appium driver install --source=npm appium-flutter-driver  
    && appium plugin install relaxed-caps  
    && appium plugin install execute-driver 
    && appium plugin install --source=npm appium-wait-plugin

And this is how my compose.yml look like:

services:
  service:
    image: image
    network_mode: host
    hostname: localhost
    volumes:
      - /Users/.m2:/home/myuser/.m2

I even tried to curl to http://127.0.0.1:8200 on the container, and was Connection refused. But when I curl from my laptop it works fine. I do not understand what am I doing wrong. Since the network_mode is set to host, the container should be able to connect to any localhost port no? How can I make this work?

2

Answers


  1. The issue you are experiencing is probably a docker network issue.
    Try updating your dockerfile :

    FROM openjdk:11-jdk-slim
    
    RUN apt-get update && apt-get install -y 
        wget curl unzip android-tools-adb 
        && rm -rf /var/lib/apt/lists/*
    
    ENV ANDROID_HOME /opt/android-sdk
    ENV PATH ${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools
    
    RUN mkdir -p ${ANDROID_HOME} && cd ${ANDROID_HOME} 
        && wget https://dl.google.com/android/repository/commandlinetools-linux-7583922_latest.zip 
        && unzip commandlinetools-linux-7583922_latest.zip 
        && rm commandlinetools-linux-7583922_latest.zip 
        && yes | sdkmanager --licenses 
        && sdkmanager "platform-tools" "platforms;android-30" "build-tools;30.0.3"
    
    RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash 
        && . $NVM_DIR/nvm.sh 
        && nvm install 16.18.1 
        && npm install -g appium@latest 
        && appium driver install uiautomator2 flutter 
        && appium plugin install relaxed-caps execute-driver appium-wait-plugin
    
    CMD ["appium", "--allow-insecure", "chromedriver_autodownload"]
    

    And your docker compose :

    version: '3'
    services:
      appium:
        build: .
        privileged: true
        network_mode: host
        environment:
          - ANDROID_ADB_SERVER_ADDRESS=host.docker.internal
        volumes:
          - /dev/bus/usb:/dev/bus/usb
          - ~/.m2:/home/appuser/.m2
        ports:
          - "4723:4723"
    

    Ensure your device is connected and visible in ADBm then build and run the docker compose to see if the issue is resolved

    Login or Signup to reply.
    • Verify Appium configuration to ensure it is set up to use the correct
      port and IP address for the AndroidUiautomator2Driver
    • Even with network_mode: host, ensure that no firewall or security
      group settings on your host or Docker daemon are blocking the
      connection.
    • You may need to adjust the Appium server’s binding settings to make
      sure it is accessible on 0.0.0.0.

    If this not solve your issue try to change the docker file

    Update your Dockerfile

    RUN apt-get update 
        && apt-get install -y maven usbutils wget android-sdk android-tools-adb 
        && apt-get clean 
        && rm -rf /var/lib/apt/lists/*
    
    ENV ANDROID_HOME="/usr/lib/android-sdk"
    ENV ANDROID_SDK_ROOT=$ANDROID_HOME
    ENV PATH=$PATH:$ANDROID_HOME/platform-tools
    
    RUN useradd -ms /bin/bash myuser 
        && usermod -aG root myuser
    
    USER myuser
    
    RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash 
        && source $HOME/.nvm/nvm.sh 
        && nvm install v16.18.1 
        && npm install -g appium@latest 
        && appium driver install uiautomator2 
        && appium driver install xcuitest 
        && appium driver install --source=npm appium-flutter-driver 
        && appium plugin install relaxed-caps 
        && appium plugin install execute-driver 
        && appium plugin install --source=npm appium-wait-plugin
    

    and docker-compose.yml

    version: '3'
    services:
      service:
        image: image
        network_mode: host
        hostname: localhost
        volumes:
          - /Users/.m2:/home/myuser/.m2
        environment:
          - ANDROID_ADB_SERVER_ADDRESS=host.docker.internal
    
    • Set ANDROID_ADB_SERVER_ADDRESS to host.docker.internal to ensure
      the container can communicate with the host’s ADB server.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search