From 7d177f65d6136eb590540daa2581a4b46a3053c8 Mon Sep 17 00:00:00 2001 From: Halim Qarroum Date: Mon, 13 Mar 2023 21:07:22 +0000 Subject: [PATCH] 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"