Header Image

How to Publish an Ionic Android App with Docker

For the sake of brevity, we’re going to operate under the assumption that you already have docker and docker-compose running on your Mac.

If you don’t, here are some instructions: Installing Docker for Mac

Docker for Mac is one of the new kids on the Docker block. It uses a lightweight VM instead of relying on Virtualbox or Vagrant. I find this approach appealing, since it’s a one button install, and self-updating.

Sadly, like all new tech, it’s not all roses. There is a speed issue when dealing with the Filesystem. It’s resulted in me running a hybrid Mac host/Docker setup for my development environment. I run all filesystem intensive apps on my Mac, then dockerize all external services that it depends on (like MySQL and ElasticSearch)

Getting Dockerized

We’re going to use the ionic-framework base image as a jumping off point and following along the Ionic docs for publishing your ionic app.

Let’s start by making a local directory for gradle, so you don’t have to install it each time you run your ionic docker container.

mkdir ~/.gradle

I like to use docker and docker-compose so I don’t need to include a long string of commands each time I’m working with the build process. Docker compose isn’t a hard requirement, but more of a personal preference. I like to start containers with docker-compose […] instead of docker […9 million commands].

Example Dockerfile

You could probably get away with just using the docker image instead of having a local Dockerfile, but I felt more comfortable with it, in case I need to customize it down the road.

FROM agileek/ionic-framework:2.0.0
Docker Compose
version: '2'
services:
  app:
    container_name: karma2_ionic
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - .:/myApp
      - ~/.gradle:/root/.gradle

Ok so we have it all setup. Let’s get it built!

> docker-compose build
> docker-compose run app ionic build android –release

You should see output similar to this:

BUILD SUCCESSFUL
Total time: 34.45 secs
Built the following apk(s):/myApp/platforms/android/build/outputs/apk/android-release-unsigned.apk

If you haven’t already done this, you’ll need to create a private key. You’ll only need to do this one time.

> keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

Sign the apk with Jarsigner.

docker-compose run app jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore platforms/android/build/outputs/apk/android-release-unsigned.apk alias_name

Now we need to run Zipalign

In this example we’re creating our 2.1.4 release (android-2.1.4-release.apk). In yours, you’ll replace the output at the end with the name of your own release.

docker-compose run app /opt/android-sdk-linux/build-tools/23.0.2/zipalign -v 4 /myApp/platforms/android/build/outputs/apk/android-release-unsigned.apk /myApp/platforms/android/build/outputs/apk/android-2.1.4-release.apk

There you go! Now you can upload the APK to the Google Play store and start getting downloads.

/myApp/platforms/android/build/outputs/apk/android-2.1.4-release.apk

I find the docker approach so much cleaner than installing a million android build dependencies. I haven’t done much emulation or development with it yet. Will do that next.

One last thing to keep in mind, you can’t use the docker approach for IOS since Apple development is gated and is required to run on your Mac. If someone knows a docker way to do this let me know. I still find Dockerizing at least the Android part saves you a bit of kludge, even if you still have to run ionic locally to publish on IOS.

A few helpful docker commands

Not really related to ionic, but I find myself using these commands a lot.

> docker stop $(docker ps -a -q) # stop all containers
> docker rm $(docker ps -a -q) # remove all containers
> docker rmi -f $(docker images -q) # remove all images