Build an Android app with Docker: build, push and pull docker image.

Check out this tutorial to get started with Docker:

The main file on Docker is Dockerfile. Docker is like a virtual machine running locally, similar to python virtual environments. Tutorial below shows how to build an Android application with Docker.

This is useful in CI process. Before I used to build with a Mac Machine used only for building, the problem with this is that you need to spend time doing maintenance to update: MacOS, android studio +, emulator, .., virus update, etc. Docker fixes all those problems since it takes care of the middleware. Using an Android Dockerfile installs Android components, sdk, gradle, and after that, we can build our Android App, test, jacoco report, lint, etc. Docker runs on EC2 AWS.

Build Android image

  1. Create a new directory. $mkdir LearningDocker
  2. $cd LearningDocker. Create new file name: Dockerfile
  3. $vi Dockerfile. Copy and paste below snippet
FROM gradle:4.10.0-jdk8USER rootENV SDK_URL="https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip" \
ANDROID_HOME="/usr/local/android-sdk" \
ANDROID_VERSION=28 \
ANDROID_BUILD_TOOLS_VERSION=27.0.3
# Download Android SDK
RUN mkdir "$ANDROID_HOME" .android \
&& cd "$ANDROID_HOME" \
&& curl -o sdk.zip $SDK_URL \
&& unzip sdk.zip \
&& rm sdk.zip \
&& mkdir "$ANDROID_HOME/licenses" || true \
&& echo "24333f8a63b6825ea9c5514f83c2829b004d1fee" > "$ANDROID_HOME/licenses/android-sdk-license"
# && yes | $ANDROID_HOME/tools/bin/sdkmanager --licenses
# Install Android Build Tool and Libraries
RUN $ANDROID_HOME/tools/bin/sdkmanager --update
RUN $ANDROID_HOME/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS_VERSION}" \
"platforms;android-${ANDROID_VERSION}" \
"platform-tools"
# Install Build EssentialsRUN apt-get update && apt-get install build-essential -y && apt-get install file -y && apt-get install apt-utils -y

4. The command below is very important because it creates/builds a docker image to build Android projects:

$docker build -t android-build:android-gradle .

The command will download all the Android components to build an Android app. When done you will see below:

Now $cd (to an Android project) and run below command

Clean:

$docker run --rm -v "$PWD":/home/gradle/ -w /home/gradle/MyApp android-build:android-gradle gradle -PdisablePreDex clean

Lint:

$docker run --rm -v "$PWD":/home/gradle/ -w /home/gradle/MyApp android-build:android-gradle gradle -PdisablePreDex lint

Now you can change the end of the gradle command to any gradle task that you want to run.

Push docker image to dockerHub

After building the Android docker image locally. We can push it to dockerhub and make it available for everyone. For exampleOpen source your Docker image, you can push your dockerImage to dockerHub.

Step 1: Create new Dockerhub repo.

  1. Go to Dockerhub, loging and select Create -> Create Repository -> Create
  2. Now you have docker repo on Docker hub

Step 2: Push your docker image to dockerhub with command

  1. Use same Dockerfile.

2. Build your docker image.

$ docker build -t android-build:android-gradle .

3. Run command “$docker images -a” and see your image was created

4. Add tag to image.

$ docker tag android-build:android-gradle andres2014/android-build:part1

5. Now the image has a tag, push it to hubdocker with below command:

$docker login (sign-out from organization)

$ docker push andres2014/android-build:part1

Your image it’s now available to the world on hub.Docker.com users can do:

$docker pull andres2014/android-build:part1

Pull docker image and build your Android app

In the above tutorials we learned how to Create and Push a docker image to hubdocker. Next we are going to build android application using this image that we pulled.

  1. Pull image from hubdocker. $docker pull <imageName>
$docker pull andres2014/android-build:part1

2. Now that we pull the image we are going to run locally. Go to your Android App directory and run below commands:

Run $gradle clean with below command:

$docker run --rm -v "$PWD":/home/gradle/ -w /home/gradle/MyApp andres2014/android-build:part1 gradle -PdisablePreDex clean

Run $gradle lint with below command:

$docker run --rm -v "$PWD":/home/gradle/ -w /home/gradle/MyApp andres2014/android-build:part1 gradle -PdisablePreDex lint

Run $gradle build with below command:

$docker run --rm -v "$PWD":/home/gradle/ -w /home/gradle/MyApp andres2014/android-build:part1 gradle -PdisablePreDex assembleGoogleFlavorMyAppNameFreeArmv7aDebug

You can change the end of the command and add the Gradle tasks that you want to run.

Search official/public Docker images on hub.docker.com

  1. Go to hub.docker.com
  2. Example: search for “Gradle” image.
  3. We can see the official Gradle image.

Docker Notes:

+-------------------+---------------------------+
| Useful Docker commands |
+-------------------+---------------------------+
| Show all commands | $docker |
| Container list | $docker container ls |
| List images | $docker images |
| Remove image | $docker rmi MyImage |
| Containers process| $docker ps -a |
| Remove container | $docker rm -f process# |
| Remove stop | $docker stop process# |
| Stop all containers | $docker stop $(docker ps -a -q)| | Delete all containers | $docker rm $(docker ps -a -q) |
+-------------------+---------------------------+

Remove image: $docker rmi MyImage

Containers list: $docker container ls

More docker commands: $docker

software engineer, read, swim, travel. apps: https://goo.gl/rDuuQw https://goo.gl/15GvfE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store