Dockerfile for Building Ionic Apps

October 16, 2018

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

You can choose your own name for the image.

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

Step 2: Run Container

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

docker run -t -d <image_name>

Step 3: Connect to Container

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

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

Step 5: Build For Android

ionic cordova build --release android

Step 6: Clean-Up

Move your APK to local machine (it should be in /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!