From 74f8dd8adc94a2f43eca30e381a7561788faa51b Mon Sep 17 00:00:00 2001 From: Halim Qarroum Date: Mon, 13 Mar 2023 11:55:33 +0000 Subject: [PATCH 01/10] Add GPU support on test feature --- Dockerfile | 3 +++ scripts/start-emulator.sh | 1 + 2 files changed, 4 insertions(+) diff --git a/Dockerfile b/Dockerfile index d2267c2..293cae1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,9 @@ RUN apk update && \ libvirt-daemon \ dbus \ polkit \ + mesa \ + mesa-dev \ + mesa-gl \ virt-manager && \ # Compile `redir`. gcc /usr/src/redir.c -o /usr/bin/redir && \ diff --git a/scripts/start-emulator.sh b/scripts/start-emulator.sh index 78cf13b..20deabb 100644 --- a/scripts/start-emulator.sh +++ b/scripts/start-emulator.sh @@ -34,6 +34,7 @@ wait_for_boot & echo "Starting the emulator ..." emulator \ -avd android \ + -gpu swiftshader_indirect \ -noaudio \ -no-boot-anim \ -no-window \ From 908980c1219df37d93988850e4ae81782962ccec Mon Sep 17 00:00:00 2001 From: Halim Qarroum Date: Mon, 13 Mar 2023 12:42:06 +0000 Subject: [PATCH 02/10] Switching to an Nvidia base image --- Dockerfile | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/Dockerfile b/Dockerfile index 293cae1..90ec54a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM adoptopenjdk/openjdk11:alpine-jre +FROM nvidia/cuda:12.1.0-runtime-ubuntu22.04 # Docker labels. LABEL maintainer "Halim Qarroum " @@ -10,26 +10,23 @@ LABEL version "1.0.0" COPY deps/redir/redir.c /usr/src/redir.c # Installing required packages. -RUN apk update && \ - apk upgrade && \ - apk add --no-cache \ - alpine-sdk \ - bash \ - unzip \ - wget \ - libvirt-daemon \ - dbus \ - polkit \ - mesa \ - mesa-dev \ - mesa-gl \ - virt-manager && \ - # Compile `redir`. - gcc /usr/src/redir.c -o /usr/bin/redir && \ - strip /usr/bin/redir && \ - # Cleanup APK. - apk del alpine-sdk && \ - rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/cache/apk/* +RUN apt update -y && \ + apt install -y --no-install-recommends \ + build-essential \ + bash \ + unzip \ + wget \ + libvirt-daemon \ + dbus \ + openjdk-11-jdk \ + ca-certificates-java \ + virt-manager \ + libgl1-mesa-glx \ + libgl1-mesa-dri && \ + # Compile `redir`. + gcc /usr/src/redir.c -o /usr/bin/redir && \ + strip /usr/bin/redir && \ + rm -rf /var/lib/apt/lists/* # Arguments that can be overriden at build-time. ARG INSTALL_ANDROID_SDK=1 From 3e8bdd332381f7ed62d125eb8e62a535b3e8d73e Mon Sep 17 00:00:00 2001 From: Halim Qarroum Date: Mon, 13 Mar 2023 12:53:55 +0000 Subject: [PATCH 03/10] Disable building SDK --- .github/workflows/docker-feature.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/docker-feature.yml b/.github/workflows/docker-feature.yml index 78570d9..2c92851 100644 --- a/.github/workflows/docker-feature.yml +++ b/.github/workflows/docker-feature.yml @@ -20,6 +20,4 @@ jobs: with: push: true tags: halimqarroum/docker-android:test - build-args: | - API_LEVEL=33 - IMG_TYPE=google_apis + build-args: INSTALL_ANDROID_SDK=0 From 4588052e49489082adda94d84e12bb7430e1a41a Mon Sep 17 00:00:00 2001 From: Halim Qarroum Date: Mon, 13 Mar 2023 12:59:08 +0000 Subject: [PATCH 04/10] Add Ip command --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 90ec54a..c82a302 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,7 +22,8 @@ RUN apt update -y && \ ca-certificates-java \ virt-manager \ libgl1-mesa-glx \ - libgl1-mesa-dri && \ + libgl1-mesa-dri \ + iproute2 && \ # Compile `redir`. gcc /usr/src/redir.c -o /usr/bin/redir && \ strip /usr/bin/redir && \ From 1b0d276bfa5c4711c19df2fdb109e1be7ec51198 Mon Sep 17 00:00:00 2001 From: Halim Qarroum Date: Mon, 13 Mar 2023 13:52:25 +0000 Subject: [PATCH 05/10] Add virtual framebuffer --- Dockerfile | 3 +++ scripts/start-emulator.sh | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index c82a302..e89a85d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,6 +21,8 @@ RUN apt update -y && \ openjdk-11-jdk \ ca-certificates-java \ virt-manager \ + libvulkan1 \ + xvfb \ libgl1-mesa-glx \ libgl1-mesa-dri \ iproute2 && \ @@ -46,6 +48,7 @@ ENV ANDROID_SDK_ROOT=/opt/android \ ARCHITECTURE=$ARCHITECTURE \ ABI=${IMG_TYPE}/${ARCHITECTURE} \ QTWEBENGINE_DISABLE_SANDBOX=1 \ + DISPLAY=:99 \ ANDROID_EMULATOR_WAIT_TIME_BEFORE_KILL=10 # Exporting environment variables for keeping in the path diff --git a/scripts/start-emulator.sh b/scripts/start-emulator.sh index 20deabb..bc536e8 100644 --- a/scripts/start-emulator.sh +++ b/scripts/start-emulator.sh @@ -25,6 +25,8 @@ echo "Creating the Android Virtual Emulator ..." echo "Using package '$PACKAGE_PATH', ABI '$ABI' and device '$DEVICE_ID' for creating the emulator" echo no | avdmanager create avd -n android --abi "$ABI" -k "$PACKAGE_PATH" --device "$DEVICE_ID" +Xvfb "$DISPLAY" -screen 0 1920x1080x16 -nolisten tcp & + # Asynchronously write updates on the standard output # about the state of the boot sequence. #cat ~/.android/avd/android.avd/config.ini @@ -33,8 +35,9 @@ wait_for_boot & # Start the emulator with no audio, no GUI, and no snapshots. echo "Starting the emulator ..." emulator \ + -verbose \ -avd android \ - -gpu swiftshader_indirect \ + -gpu host \ -noaudio \ -no-boot-anim \ -no-window \ From c74049d77c22f4336ba917dc6b6964bca57cf568 Mon Sep 17 00:00:00 2001 From: Halim Qarroum Date: Mon, 13 Mar 2023 14:32:20 +0000 Subject: [PATCH 06/10] Add audio support --- scripts/start-emulator.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/start-emulator.sh b/scripts/start-emulator.sh index bc536e8..8e9124d 100644 --- a/scripts/start-emulator.sh +++ b/scripts/start-emulator.sh @@ -38,7 +38,6 @@ emulator \ -verbose \ -avd android \ -gpu host \ - -noaudio \ -no-boot-anim \ -no-window \ -no-snapshot-save \ From 7250977729eb015f376cae83a772855fdc6041db Mon Sep 17 00:00:00 2001 From: Halim Qarroum Date: Mon, 13 Mar 2023 15:32:56 +0000 Subject: [PATCH 07/10] Refactoring the GPU acceleration implementation --- Dockerfile | 3 ++- scripts/start-emulator.sh | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Dockerfile b/Dockerfile index e89a85d..bf7c81e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,6 +38,7 @@ ARG IMG_TYPE=google_apis ARG ARCHITECTURE=x86_64 ARG CMD_LINE_VERSION=9477386_latest ARG DEVICE_ID=pixel +ARG GPU_ACCELERATED=false # Environment variables. ENV ANDROID_SDK_ROOT=/opt/android \ @@ -47,8 +48,8 @@ ENV ANDROID_SDK_ROOT=/opt/android \ DEVICE_ID=$DEVICE_ID \ ARCHITECTURE=$ARCHITECTURE \ ABI=${IMG_TYPE}/${ARCHITECTURE} \ + GPU_ACCELERATED=$GPU_ACCELERATED \ QTWEBENGINE_DISABLE_SANDBOX=1 \ - DISPLAY=:99 \ ANDROID_EMULATOR_WAIT_TIME_BEFORE_KILL=10 # Exporting environment variables for keeping in the path diff --git a/scripts/start-emulator.sh b/scripts/start-emulator.sh index 8e9124d..9658860 100644 --- a/scripts/start-emulator.sh +++ b/scripts/start-emulator.sh @@ -23,13 +23,21 @@ export USER=root # Creating the Android Virtual Emulator. echo "Creating the Android Virtual Emulator ..." echo "Using package '$PACKAGE_PATH', ABI '$ABI' and device '$DEVICE_ID' for creating the emulator" -echo no | avdmanager create avd -n android --abi "$ABI" -k "$PACKAGE_PATH" --device "$DEVICE_ID" +echo no | avdmanager create avd -f -n android --abi "$ABI" -k "$PACKAGE_PATH" --device "$DEVICE_ID" -Xvfb "$DISPLAY" -screen 0 1920x1080x16 -nolisten tcp & +# If GPU acceleration is enabled, we create a virtual framebuffer +# to be used by the emulator when running with GPU acceleration. +if [ "$GPU_ACCELERATED" == "true" ]; then + echo "Running with GPU acceleration enabled" + export DISPLAY=":0.0" + export GPU_MODE="host" + Xvfb "$DISPLAY" -screen 0 1920x1080x16 -nolisten tcp &; +else + export GPU_MODE="auto" +fi # Asynchronously write updates on the standard output # about the state of the boot sequence. -#cat ~/.android/avd/android.avd/config.ini wait_for_boot & # Start the emulator with no audio, no GUI, and no snapshots. @@ -37,9 +45,7 @@ echo "Starting the emulator ..." emulator \ -verbose \ -avd android \ - -gpu host \ + -gpu "$GPU_MODE" \ -no-boot-anim \ -no-window \ - -no-snapshot-save \ - -qemu \ - -enable-kvm || update_state "ANDROID_STOPPED" + -no-snapshot-save || update_state "ANDROID_STOPPED" From 6889f636501d06a9e36d4693b41d667633ad49f1 Mon Sep 17 00:00:00 2001 From: Halim Qarroum Date: Mon, 13 Mar 2023 15:44:52 +0000 Subject: [PATCH 08/10] Refactoring the GPU acceleration implementation --- Dockerfile | 2 +- scripts/start-emulator.sh | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index bf7c81e..ad0ef32 100644 --- a/Dockerfile +++ b/Dockerfile @@ -38,7 +38,7 @@ ARG IMG_TYPE=google_apis ARG ARCHITECTURE=x86_64 ARG CMD_LINE_VERSION=9477386_latest ARG DEVICE_ID=pixel -ARG GPU_ACCELERATED=false +ARG GPU_ACCELERATED=true # Environment variables. ENV ANDROID_SDK_ROOT=/opt/android \ diff --git a/scripts/start-emulator.sh b/scripts/start-emulator.sh index 9658860..f4be0c2 100644 --- a/scripts/start-emulator.sh +++ b/scripts/start-emulator.sh @@ -28,9 +28,9 @@ echo no | avdmanager create avd -f -n android --abi "$ABI" -k "$PACKAGE_PATH" -- # If GPU acceleration is enabled, we create a virtual framebuffer # to be used by the emulator when running with GPU acceleration. if [ "$GPU_ACCELERATED" == "true" ]; then - echo "Running with GPU acceleration enabled" - export DISPLAY=":0.0" - export GPU_MODE="host" + echo "Running with GPU acceleration enabled"; + export DISPLAY=":0.0"; + export GPU_MODE="host"; Xvfb "$DISPLAY" -screen 0 1920x1080x16 -nolisten tcp &; else export GPU_MODE="auto" From 102b3c81339ad00b2ee1615aa64a1c9c5a3501fa Mon Sep 17 00:00:00 2001 From: Halim Qarroum Date: Mon, 13 Mar 2023 15:51:38 +0000 Subject: [PATCH 09/10] Refactoring the GPU acceleration implementation --- scripts/start-emulator.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/start-emulator.sh b/scripts/start-emulator.sh index f4be0c2..8e1058c 100644 --- a/scripts/start-emulator.sh +++ b/scripts/start-emulator.sh @@ -28,10 +28,10 @@ echo no | avdmanager create avd -f -n android --abi "$ABI" -k "$PACKAGE_PATH" -- # If GPU acceleration is enabled, we create a virtual framebuffer # to be used by the emulator when running with GPU acceleration. if [ "$GPU_ACCELERATED" == "true" ]; then - echo "Running with GPU acceleration enabled"; - export DISPLAY=":0.0"; - export GPU_MODE="host"; - Xvfb "$DISPLAY" -screen 0 1920x1080x16 -nolisten tcp &; + echo "Running with GPU acceleration enabled" + export DISPLAY=":0.0" + export GPU_MODE="host" + Xvfb "$DISPLAY" -screen 0 1920x1080x16 -nolisten tcp & else export GPU_MODE="auto" fi From 7d177f65d6136eb590540daa2581a4b46a3053c8 Mon Sep 17 00:00:00 2001 From: Halim Qarroum Date: Mon, 13 Mar 2023 21:07:22 +0000 Subject: [PATCH 10/10] Update the build process to build CUDA images --- .github/workflows/docker-image.yml | 22 +++++++- Dockerfile | 41 +++++++------- Dockerfile.gpu | 88 ++++++++++++++++++++++++++++++ README.md | 2 +- scripts/start-emulator.sh | 12 +++- 5 files changed, 136 insertions(+), 29 deletions(-) create mode 100644 Dockerfile.gpu diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index c4b7a68..27bdbb8 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -5,6 +5,7 @@ on: branches: [ "main" ] paths: - 'Dockerfile' + - 'Dockerfile.gpu' - '.github/workflows/*' - 'deps/*' - 'keys/*' @@ -17,7 +18,14 @@ jobs: strategy: matrix: version: [33, 32, 31, 30, 29, 28] - image: [{ name: 'google_apis', suffix: '' }, { name: 'google_apis_playstore', suffix: '-playstore' }] + image: [ + { name: 'google_apis', suffix: '' }, + { name: 'google_apis_playstore', suffix: '-playstore' } + ] + gpu: [ + { file: 'Dockerfile', suffix: '' }, + { file: 'Dockerfile.gpu', suffix: '-cuda' } + ] runs-on: ubuntu-latest steps: - name: Login to Docker Hub @@ -29,13 +37,20 @@ jobs: uses: docker/build-push-action@v4 with: push: true - tags: halimqarroum/docker-android:api-${{ matrix.version }}${{ matrix.image.suffix }} + file: ${{ matrix.gpu.file }} + tags: halimqarroum/docker-android:api-${{ matrix.version }}${{ matrix.image.suffix }}${{ matrix.gpu.suffix }} build-args: | API_LEVEL=${{ matrix.version }} IMG_TYPE=${{ matrix.image.name }} # Builds a minimal image without any Android SDK images. build-minimal: + strategy: + matrix: + gpu: [ + { file: 'Dockerfile', suffix: '' }, + { file: 'Dockerfile.gpu', suffix: '-cuda' } + ] runs-on: ubuntu-latest steps: - name: Login to Docker Hub @@ -47,5 +62,6 @@ jobs: uses: docker/build-push-action@v4 with: push: true - tags: halimqarroum/docker-android:minimal + file: ${{ matrix.gpu.file }} + tags: halimqarroum/docker-android:minimal${{ matrix.gpu.suffix }} build-args: INSTALL_ANDROID_SDK=0 diff --git a/Dockerfile b/Dockerfile index ad0ef32..2497c73 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM nvidia/cuda:12.1.0-runtime-ubuntu22.04 +FROM adoptopenjdk/openjdk11:alpine-jre # Docker labels. LABEL maintainer "Halim Qarroum " @@ -10,26 +10,23 @@ LABEL version "1.0.0" COPY deps/redir/redir.c /usr/src/redir.c # Installing required packages. -RUN apt update -y && \ - apt install -y --no-install-recommends \ - build-essential \ - bash \ - unzip \ - wget \ - libvirt-daemon \ - dbus \ - openjdk-11-jdk \ - ca-certificates-java \ - virt-manager \ - libvulkan1 \ - xvfb \ - libgl1-mesa-glx \ - libgl1-mesa-dri \ - iproute2 && \ - # Compile `redir`. - gcc /usr/src/redir.c -o /usr/bin/redir && \ - strip /usr/bin/redir && \ - rm -rf /var/lib/apt/lists/* +RUN apk update && \ + apk upgrade && \ + apk add --no-cache \ + alpine-sdk \ + bash \ + unzip \ + wget \ + libvirt-daemon \ + dbus \ + polkit \ + virt-manager && \ + # Compile `redir`. + gcc /usr/src/redir.c -o /usr/bin/redir && \ + strip /usr/bin/redir && \ + # Cleanup APK. + apk del alpine-sdk && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/cache/apk/* # Arguments that can be overriden at build-time. ARG INSTALL_ANDROID_SDK=1 @@ -38,7 +35,7 @@ ARG IMG_TYPE=google_apis ARG ARCHITECTURE=x86_64 ARG CMD_LINE_VERSION=9477386_latest ARG DEVICE_ID=pixel -ARG GPU_ACCELERATED=true +ARG GPU_ACCELERATED=false # Environment variables. ENV ANDROID_SDK_ROOT=/opt/android \ diff --git a/Dockerfile.gpu b/Dockerfile.gpu new file mode 100644 index 0000000..0be8255 --- /dev/null +++ b/Dockerfile.gpu @@ -0,0 +1,88 @@ +FROM nvidia/cuda:12.1.0-runtime-ubuntu22.04 + +# Docker labels. +LABEL maintainer "Halim Qarroum " +LABEL description "A Docker image allowing to run an Android emulator" +LABEL version "1.1.0" + +# `redir.c` will be used to redirect +# localhost ADB ports to the container interface. +COPY deps/redir/redir.c /usr/src/redir.c + +# Installing required packages. +RUN apt update -y && \ + apt install -y --no-install-recommends \ + build-essential \ + bash \ + unzip \ + wget \ + libvirt-daemon \ + dbus \ + openjdk-11-jdk \ + ca-certificates-java \ + virt-manager \ + libvulkan1 \ + xvfb \ + libgl1-mesa-glx \ + libgl1-mesa-dri \ + iproute2 && \ + # Compile `redir`. + gcc /usr/src/redir.c -o /usr/bin/redir && \ + strip /usr/bin/redir && \ + rm -rf /var/lib/apt/lists/* + +# Arguments that can be overriden at build-time. +ARG INSTALL_ANDROID_SDK=1 +ARG API_LEVEL=33 +ARG IMG_TYPE=google_apis +ARG ARCHITECTURE=x86_64 +ARG CMD_LINE_VERSION=9477386_latest +ARG DEVICE_ID=pixel +ARG GPU_ACCELERATED=true + +# Environment variables. +ENV ANDROID_SDK_ROOT=/opt/android \ + ANDROID_PLATFORM_VERSION="platforms;android-$API_LEVEL" \ + PACKAGE_PATH="system-images;android-${API_LEVEL};${IMG_TYPE};${ARCHITECTURE}" \ + API_LEVEL=$API_LEVEL \ + DEVICE_ID=$DEVICE_ID \ + ARCHITECTURE=$ARCHITECTURE \ + ABI=${IMG_TYPE}/${ARCHITECTURE} \ + GPU_ACCELERATED=$GPU_ACCELERATED \ + QTWEBENGINE_DISABLE_SANDBOX=1 \ + ANDROID_EMULATOR_WAIT_TIME_BEFORE_KILL=10 + +# Exporting environment variables for keeping in the path +# Android SDK binaries and shared libraries. +ENV PATH "${PATH}:${ANDROID_SDK_ROOT}/platform-tools" +ENV PATH "${PATH}:${ANDROID_SDK_ROOT}/emulator" +ENV PATH "${PATH}:${ANDROID_SDK_ROOT}/cmdline-tools/tools/bin" +ENV LD_LIBRARY_PATH "$ANDROID_SDK_ROOT/emulator/lib64:$ANDROID_SDK_ROOT/emulator/lib64/qt/lib" + +# Set the working directory to /opt +WORKDIR /opt + +# Exposing the Android emulator console port +# and the ADB port. +EXPOSE 5554 5555 + +# Initializing the required directories. +RUN mkdir /root/.android/ && \ + touch /root/.android/repositories.cfg + +# Exporting ADB keys. +COPY keys/* /root/.android/ + +# Copy the startup scripts. +COPY scripts/* /opt/ + +# Make the scripts executable. +RUN chmod +x /opt/*.sh + +# This layer will download the Android command-line tools +# to install the Android SDK, emulator and system images. +# It will then install the Android SDK and emulator. +RUN /opt/install-sdk.sh + +# Set the entrypoint +ENTRYPOINT ["/opt/start-emulator.sh"] \ No newline at end of file diff --git a/README.md b/README.md index 4e31eeb..8ebc0e8 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ [![DeepSource](https://deepsource.io/gh/HQarroum/docker-android.svg/?label=active+issues&show_trend=true&token=JTfGSHolIiMj0WNfv2ES0I6X)](https://deepsource.io/gh/HQarroum/docker-android/?ref=repository-badge) ![Docker Pulls](https://img.shields.io/docker/pulls/halimqarroum/docker-android) -Current version: **1.0.0** +Current version: **1.1.0** ## 📋 Table of contents diff --git a/scripts/start-emulator.sh b/scripts/start-emulator.sh index 8e1058c..833bcac 100644 --- a/scripts/start-emulator.sh +++ b/scripts/start-emulator.sh @@ -23,12 +23,18 @@ export USER=root # Creating the Android Virtual Emulator. echo "Creating the Android Virtual Emulator ..." echo "Using package '$PACKAGE_PATH', ABI '$ABI' and device '$DEVICE_ID' for creating the emulator" -echo no | avdmanager create avd -f -n android --abi "$ABI" -k "$PACKAGE_PATH" --device "$DEVICE_ID" +echo no | avdmanager create avd \ + --force \ + --name android \ + --abi "$ABI" \ + --package "$PACKAGE_PATH" \ + --device "$DEVICE_ID" # If GPU acceleration is enabled, we create a virtual framebuffer # to be used by the emulator when running with GPU acceleration. +# We also set the GPU mode to `host` to force the emulator to use +# GPU acceleration. if [ "$GPU_ACCELERATED" == "true" ]; then - echo "Running with GPU acceleration enabled" export DISPLAY=":0.0" export GPU_MODE="host" Xvfb "$DISPLAY" -screen 0 1920x1080x16 -nolisten tcp & @@ -48,4 +54,4 @@ emulator \ -gpu "$GPU_MODE" \ -no-boot-anim \ -no-window \ - -no-snapshot-save || update_state "ANDROID_STOPPED" + -no-snapshot || update_state "ANDROID_STOPPED"