I am trying to connect an Android emulator (running on my MacOS) to my linux-based docker container.
Here's my dockerfile:
# Use an official Linux base image (Ubuntu)
FROM cirrusci/flutter:latest
# Install dependencies
RUN apt-get update && apt-get install -y wget curl unzip git \
openjdk-17-jdk lib32stdc++6 lib32z1 \
libglu1-mesa libxi6 libxrender1 libxrandr2 libxcursor1 \
libpulse0 libnss3 libatk-bridge2.0-0 libxcomposite1 libasound2 \
gnupg2 socat net-tools clang cmake ninja-build pkg-config libgtk-3-dev
# Set Java environment variables
ENV JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
ENV PATH="$JAVA_HOME/bin:$PATH"
# Install Android SDK
ENV ANDROID_SDK_ROOT=/opt/android-sdk
RUN mkdir -p $ANDROID_SDK_ROOT && cd $ANDROID_SDK_ROOT && \
wget .zip -O cmdline-tools.zip && \
unzip cmdline-tools.zip -d $ANDROID_SDK_ROOT && \
rm cmdline-tools.zip && \
mv $ANDROID_SDK_ROOT/cmdline-tools $ANDROID_SDK_ROOT/cmdline-tools-latest && \
mkdir -p $ANDROID_SDK_ROOT/cmdline-tools/latest && \
mv $ANDROID_SDK_ROOT/cmdline-tools-latest/* $ANDROID_SDK_ROOT/cmdline-tools/latest/
# Set Android SDK environment variables
ENV PATH="$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/platform-tools:$PATH"
# Accept Android SDK licenses and install required components
RUN yes | sdkmanager --licenses && \
sdkmanager "platform-tools" "platforms;android-34" "build-tools;34.0.0" "cmdline-tools;latest"
# Install Google Chrome (for Flutter Web)
RUN wget .deb && \
apt-get install -y ./google-chrome-stable_current_amd64.deb && \
rm google-chrome-stable_current_amd64.deb
# Set Chrome environment variable for Flutter
ENV CHROME_EXECUTABLE=/usr/bin/google-chrome
# Install Flutter from the official GitHub repository (includes .git metadata)
RUN rm -rf /sdks/flutter && git clone .git /sdks/flutter
# Set Flutter environment variables
ENV FLUTTER_HOME=/sdks/flutter
ENV PATH="$FLUTTER_HOME/bin:$PATH"
# Ensure Flutter is in a valid state
RUN flutter channel stable && flutter upgrade && flutter doctor
# Set working directory
WORKDIR /app
COPY . /app
I build my container like this:
docker build -t joshua/gs_ui:1.2 .
I start my docker container with network access:
docker run --rm -it --network host joshua/gs_ui:1.2 bash
Running flutter doctor -v
from inside the docker container gives me this:
[✓] Flutter (Channel stable, 3.29.2, on Ubuntu 22.04.1 LTS 6.10.14-linuxkit, locale en_US.UTF-8) [584ms]
• Flutter version 3.29.2 on channel stable at /sdks/flutter
• Upstream repository .git
• Framework revision c236373904 (4 days ago), 2025-03-13 16:17:06 -0400
• Engine revision 18b71d647a
• Dart version 3.7.2
• DevTools version 2.42.3
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1) [4.9s]
• Android SDK at /opt/android-sdk-linux
• Platform android-33, build-tools 33.0.1
• ANDROID_HOME = /opt/android-sdk-linux
• ANDROID_SDK_ROOT = /opt/android-sdk
• Java binary at: /usr/lib/jvm/java-17-openjdk-amd64/bin/java
This JDK is specified by the JAVA_HOME environment variable.
To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
• Java version OpenJDK Runtime Environment (build 17.0.14+7-Ubuntu-122.04.1)
• All Android licenses accepted.
[✓] Chrome - develop for the web [47ms]
• CHROME_EXECUTABLE = /usr/bin/google-chrome
[✓] Linux toolchain - develop for Linux desktop [878ms]
• Ubuntu clang version 14.0.0-1ubuntu1.1
• cmake version 3.22.1
• ninja version 1.10.1
• pkg-config version 0.29.2
[!] Android Studio (not installed) [43ms]
• Android Studio not found; download from .html
(or visit /to/linux-android-setup for detailed instructions).
[✓] Connected device (2 available) [704ms]
• Linux (desktop) • linux • linux-x64 • Ubuntu 22.04.1 LTS 6.10.14-linuxkit
• Chrome (web) • chrome • web-javascript • Google Chrome 134.0.6998.88
[✓] Network resources [719ms]
• All expected network resources are available.
! Doctor found issues in 1 category.
- I boot up the emulator and in my zsh terminal I run,
adb shell ip route
to get the IP address of the emulator. - Then from inside the docker container, I run,
adb connect 10.0.2.16:5555
(or whatever the given emulator IP address is). andadb devices
This is the response I typically get:
* daemon not running; starting now at tcp:5037
* daemon started successfully
failed to connect to '10.0.2.16:5555': Connection refused
List of devices attached
I've tried running the following socat commands to forward the emulator's ports like this:
socat tcp-listen:5554,bind=$ip,fork tcp:127.0.0.1:5554 &
socat tcp-listen:5555,bind=$ip,fork tcp:127.0.0.1:5555 &
socat udp-listen:5554,bind=$ip,fork udp:127.0.0.1:5554 &
socat udp-listen:5555,bind=$ip,fork udp:127.0.0.1:5555 &
And then repeating the adb connect command.
I've also tried the above process using different emulators:
- Using Android Studio Pixel_9 Android emulator started up directly from Android Studio.
- A new emulator with Google APIs image by:
a: Installing ARM64,
sdkmanager --install "system-images;android-33;google_apis;arm64-v8a"
b:avdmanager create avd -n Pixel_9_ARM64 -k "system-images;android-33;google_apis;arm64-v8a"
c:emulator -avd Pixel_9_ARM64 -no-snapshot -gpu host -no-boot-anim
- A third-party emulator called, Genymotion.
STILL NO LUCK.
I pretty much keep getting, "failed to connect" without any details as to why it failed to connect.
Anyone know what the issue is/could be?