我使用 docker 运行一个 React Native Android 项目,它可以正确构建,但永远不会在我的主机的模拟器中启动。 我可以在 docker 实例上的 adb devices 中看到,它有我的主机上的模拟器。
在容器外壳中,它只是位于 > Task :app:installDebug
这是我用来启动实例的 docker-compose.yml
version: "4"
services:
VNC-Service:
image: baseImage
build: .
container_name: react-native
network_mode: "host"
environment:
VNC_PASSWORD: 123
USE_POLLING: true
privileged: true
command:
- bash
- -c
- |
../start_vnc.sh
volumes:
- ./:/app
tty: true
stdin_open: true
这是我的dockerfile
FROM openjdk:17-jdk-slim
#=============================
# Install Dependenices
#=============================
SHELL ["/bin/bash", "-c"]
RUN apt update
RUN apt install -y curl
RUN apt install -y sudo
RUN apt install -y wget
RUN apt install -y unzip
RUN apt install -y bzip2
RUN apt install -y libdrm-dev
RUN apt install -y libxkbcommon-dev
RUN apt install -y libgbm-dev
RUN apt install -y libasound-dev
RUN apt install -y libnss3
RUN apt install -y libxcursor1
RUN apt install -y libpulse-dev
RUN apt install -y libxshmfence-dev
RUN apt install -y xauth xvfb
RUN apt install -y x11vnc
RUN apt install -y fluxbox
RUN apt install -y wmctrl
RUN apt install -y libdbus-glib-1-2
RUN apt install -y usbutils
RUN apt install -y git
RUN apt install -y android-sdk-platform-tools
#==============================
# Android SDK ARGS
#==============================
ARG ARCH="x86_64"
ARG TARGET="google_apis_playstore"
ARG API_LEVEL="34"
ARG BUILD_TOOLS="34.0.0"
ARG ANDROID_ARCH=${ANDROID_ARCH_DEFAULT}
ARG ANDROID_API_LEVEL="android-${API_LEVEL}"
ARG ANDROID_APIS="${TARGET};${ARCH}"
ARG EMULATOR_PACKAGE="system-images;${ANDROID_API_LEVEL};${ANDROID_APIS}"
ARG PLATFORM_VERSION="platforms;${ANDROID_API_LEVEL}"
ARG BUILD_TOOL="build-tools;${BUILD_TOOLS}"
ARG ANDROID_CMD="commandlinetools-linux-11076708_latest.zip"
ARG ANDROID_SDK_PACKAGES="${PLATFORM_VERSION} ${BUILD_TOOL} platform-tools"
#==============================
# Set JAVA_HOME - SDK
#==============================
ENV ANDROID_SDK_ROOT=/opt/android
ENV PATH "$PATH:$ANDROID_SDK_ROOT/cmdline-tools/tools:$ANDROID_SDK_ROOT/cmdline-tools/tools/bin:$ANDROID_SDK_ROOT/emulator:$ANDROID_SDK_ROOT/tools/bin:$ANDROID_SDK_ROOT/platform-tools:$ANDROID_SDK_ROOT/build-tools/${BUILD_TOOLS}"
ENV DOCKER="true"
ENV JAVA_HOME /usr/local/openjdk-17
ENV PATH ${PATH}:${ANDROID_HOME}/cmdline-tools/latest/bin:${ANDROID_HOME}/platform-tools
#============================================
# Install required Android CMD-line tools
#============================================
RUN wget https://dl.google.com/android/repository/${ANDROID_CMD} -P /tmp && \
unzip -d $ANDROID_SDK_ROOT /tmp/$ANDROID_CMD && \
mkdir -p $ANDROID_SDK_ROOT/cmdline-tools/tools && cd $ANDROID_SDK_ROOT/cmdline-tools && mv NOTICE.txt source.properties bin lib tools/ && \
cd $ANDROID_SDK_ROOT/cmdline-tools/tools && ls
#============================================
# Install required package using SDK manager
#============================================
RUN yes Y | sdkmanager --licenses
RUN yes Y | sdkmanager --verbose --no_https ${ANDROID_SDK_PACKAGES}
RUN sdkmanager "ndk;25.1.8937393" "build-tools;33.0.1" "platforms;android-31" "cmake;3.22.1"
#====================================
# Install latest nodejs, npm & appium
#====================================
RUN curl -sL https://deb.nodesource.com/setup_20.x | bash && \
apt-get -qqy install nodejs && \
npm install -g npm && \
npm i -g appium --unsafe-perm=true --allow-root && \
appium driver install uiautomator2 && \
exit 0 && \
npm cache clean && \
apt-get remove --purge -y npm && \
apt-get autoremove --purge -y && \
apt-get clean && \
rm -Rf /tmp/* && rm -Rf /var/lib/apt/lists/*
#===================
# React Native Setup
#===================
RUN npm install -g react-native-cli
#===================
# Gradle Setup
#===================
ENV GRADLE_VERSION 8.3
ENV GRADLE_HOME /root/.gradle
ENV PATH ${PATH}:${GRADLE_HOME}/bin
ENV GRADLE_USER_HOME /root/.gradle
RUN wget -P /root/.gradle/wrapper/dists https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-all.zip && \
unzip /root/.gradle/wrapper/dists/gradle-${GRADLE_VERSION}-all.zip -d /root/.gradle/wrapper/dists
ENV ADB_INSTALL_TIMEOUT=10
#===================
# Alias
#===================
ENV VNC=../start_vnc.sh
ENV HOME=/root
WORKDIR /app
#===================
# Ports
#===================
ENV ADB_SERVER_SOCKET=tcp:host.docker.internal:5037
#=========================
# Copying Scripts to root
#=========================
COPY start_vnc.sh /start_vnc.sh
RUN chmod a+x ../start_vnc.sh
#=======================
# framework entry point
#=======================
CMD ["npx", "react-native", "start"]
你能做到吗?