Running Android Emulator in a Docker Container

Amr Salem
Innovies Club
Published in
6 min readJan 25, 2023

Are you looking for a reliable and efficient solution for running an Android emulator for testing, debugging, and continuous integration/continuous deployment (CI/CD) in your development pipeline?

In this blog post, we will not only explore the benefits of using a Docker container to run your Android emulator but also demonstrate a step-by-step guide on how to set it up, providing a stable and reproducible environment without relying on external services or device farms.

Benefits and Features

  • Isolation of the Android emulator in its own environment, preventing conflicts with other software on the host machine, working in headed/headless mode.
  • Reproducibility of testing environments through the use of portable Docker containers.
  • Scalability of resources to meet the needs of the project.
  • Easy switching between different versions of the Android emulator for testing on various Android OS versions.
  • Simplified setup and management of the Android emulator through the use of Docker’s user-friendly interface/ docker-compose file.
  • Streamlined integration of the Android emulator into the CI/CD pipeline.
  • Ability to run the Android emulator on cloud infrastructure such as AWS, GCP, and Azure using Docker.
  • No need to rely on external services or device farms (for android).
  • Relief from the time-consuming and complex setup process of the Android emulator on your host machine.

Now that we’ve seen the numerous benefits of building the android emulator inside a Docker container, I’m sure you’re feeling tempted to get started. Let’s begin by breaking down our “Dockerfile” and examining the steps needed to build and run our emulator within a container.

A diagram illustrating the use of images in automation testing and scaling

Building Dockerfile

When building a Docker image, it’s important to consider the end goal and the steps required to achieve it. In the case of building an android emulator, the following steps should be taken:

  1. Selecting a suitable base image to build the final image on top of.
  2. Downloading and installing the android command line tools, which will allow us to use the android SDK manager to acquire the necessary SDKs for building the emulator.
  3. Setting the necessary environment variables for the android SDK.
  4. Using the avdmanager to create the desired emulator.
  5. Optionally, downloading NodeJs and Appium for running automation tests.
  6. Creating shell script files to aid in managing the emulator’s booting and operating mode.
  7. Configuring the VNC server in order to run the emulator in headed mode

Steps

  • To build the Dockerfile, we need to first specify the base image, in this example, “openjdk:18-jdk-slim” is used as it is lightweight and includes the necessary JDK. It is important to disable any user interaction during the building process of the Docker image. Additionally, we need to specify our working directory.
  • Download the following libraries, typically used for building and running graphical applications on a Linux system, for example xvfb is a virtual framebuffer X server, useful for running graphical applications on headless systems, x11vnc is a VNC server that can share an X11 session,fluxbox and wmctrl are Window Manager and a command line tool for interacting with an X Window Manager respectively,libpulse-dev for handling sound input and output (if needed),libxshmfence-dev for handling shared memory synchronization, libdbus-glib-1-2 is a library for D-Bus, a message bus system used for inter-process communication.
  • Download “commandlinetools-linux-8092744_latest.zip” and extract it to the desired location for the ANDROID_SDK_ROOT, you can use the wget command with the endpoint provided in the code snippet shown below.
  • Create a new directory inside “$ANDROID_SDK_ROOT/cmdline-tools/” called “tools”.
  • It is important to move all of the contents located in “$ANDROID_SDK_ROOT/cmdline-tools/” to the tools directory in order to launch the sdkmanager.
  • Then we can update our PATH variable with the new binaries.
  • It is now time to review and accept the sdkmanager licenses, and then download the necessary Android SDK packages required for the emulator, including :
    system image, platform, and version build tools, architecture, and Android target.
    It is important to note that all of these values must be compatible with each other in order to successfully build the emulator. To view the full list of compatible versions, you can run the command shown in the screenshot provided.
  • To construct the emulator, the following command should be executed. I have included EMULATOR_NAME/DEVICE as ARG, so it could be easily replaced with another emulator device if needed during the building of the image process.
  • If you plan to run automation tests on the Android emulator, it may be necessary to install Node.js and Appium. Afterward, it will be important to perform some cleanup by removing any unnecessary files.
  • we will require a shell script file to assist us in starting the emulator in either headed or headless mode, creating an Appium session for automation, or initiating the VNC server for a GUI experience when using the emulator in headed mode on in headless mode in case automation test will be performed

start_appium.sh
start_emu_headless.sh
start_emu.sh → use only with VNC
start_vnc.sh
I have included them in the GitHub repository, which you can access via the link provided below.👇🏾

  • We now need to copy all of the shell scripts into our image and provide execute permissions for all users. Next, we will set the entry point as “/bin/bash”.
  • The Dockerfile is ready and we can now build the image

Note:
Port 5900 is required to connect to the VNC server at localhost:5900. Additionally, VNC_PASSWORD is an environment variable used for the VNC login password.

Let’s give it a try

  • To use the VNC server and launch the emulator in headed mode, first, start the docker container and the VNC server using the commands provided above or the docker-compose file from the GitHub repository Then, connect to the remote server using “Remmina”, and on the workplace: right-click → select Application → then Shell → and dash. Finally, run “./start_emu.sh” and wait a few seconds for the emulator to start.
Running the emulator in headed mode
  • With the command “docker compose up”, one or multiple instances of Appium can be launched efficiently using Docker Compose in conjunction with the emulator.
docker-compose file sample

Executed successfully🎉

Booting up the emulator in headless mode and launching appium instance

Links

https://github.com/amrsa1/Android-Emulator
https://hub.docker.com/repository/docker/amrka/android-emulator/
https://developer.android.com/studio?gclsrc=ds&gclsrc=ds
https://linux.die.net/man/1/xvfb

Thank you for taking the time to read my blog — I hope it provided some insight and value into your project. Stay tuned for the new upcoming interesting blogs…

--

--