Building apps using Ionic is useful for the ability to build for both IOS and Android from the same codebase. However, mostly because Ionic is build on top of the Apache Cordova framework, it can be difficult to make sure your environment is configured with the required version of each build component.

This is where Docker can be helpful. You can create a container that has a consistent environment and run it locally to build your project.

The Dockerfile here creates an environment suitable for building Android applications. The file that is created on build for Android is referred to as an APK. To generate your APK without modifying the Dockerfile at all, follow the directions below:

Step 1: Build Image Link to heading

You can choose your own name for the image.

docker build -t <image_name> . --force-rm

Step 2: Run Container Link to heading

Use the same image name as above to run container as background process.

docker run -t -d <image_name>

Step 3: Connect to Container Link to heading

Find the name of the running container (Docker will generate one).

docker container ls

Get a bash shell in the container.

docker exec -i -t <container_name> /bin/bash

Step 4: Copy Project To Container Link to heading

docker cp <local_path> <container_name>:<container_path>

Step 5: Build For Android Link to heading

ionic cordova build --release android

Step 6: Clean-Up Link to heading

Move your APK to local machine (it should be in <ionic_proj_name>/platforms/android/build/outputs/apk).

docker cp <container_name>:<ionic_proj_name>/platforms/android/build/outputs/apk <local_path>

Kill container.

docker kill <container_name>

As always, send me a message @HashedDan on Twitter for any questions or comments!