A Docker Image for Apache Cordova

By Silviu, on 2017-04-10

Keep your base OS tidy, and bundle Apache Cordova and Android SDK build tools and libraries inside a Docker Linux image.

Overview

Despite my visceral unease about Docker instances used in production systems (perhaps less justified now, in 2017, than in previous years), I find containers to be invaluably useful on my development machine, where development suites and frameworks create an ever-growing pile of steamy dependencies.

One such use case came up recently, when I had to re-install the paraphernalia of tools associated with the Apache Cordova and Android SDK mobile development ecosystem. Keeping my base operating system as clean as possible was a good motivator.

This article describes the steps that I took to create an ArchLinux-based Docker image that packages together Apache Cordova, Android SDK and the auxiliary build tools. The article does not cover the iOS side of things, but if I get my hands on a Mac, I might add the missing section.

When run, the container has access to a shared folder path containing the Cordova project, and performs the platform-specific compilation and packaging of the project. Deploying to the actual Android phone is done outside of the container.

The Frankenstein of Arch Dockerfiles

My favorite, and go-to Docker base image is the ArchLinux-based oddlid/arch-cli-minimal. It gives us a minimal Arch image, with bash shell, that we can build further on. Its locale is set to Swedish but we can easily change it to English.

Let's start with the easy stuff: we're going to get the Arch image up to date, and re-install the certificate utils package, which is prone to provoke the occasional hiccup. In addition to that we install the baseline requirements for Apache Cordova (NodeJs, Npm, and so forth).

FROM oddlid/arch-cli-minimal:latest

# Temporarily uninstall ca-certificates-utils to get rid of the 
# ca-certificates-utils: /etc/ssl/certs/ca-certificates.crt exists in filesystem error
# See: https://bbs.archlinux.org/viewtopic.php?id=223895
# It will be reinstalled later on
RUN  pacman -Rdd --noconfirm ca-certificates-utils

# Install packages for both ease of use and 
# structurally needed for PhantomJS (fontconfig) and Node
RUN pacman -Syy
RUN pacman -S --noconfirm --needed ca-certificates-utils harfbuzz fontconfig nodejs npm
RUN pacman -S --noconfirm --needed nano htop wget

Our first predicament is that we need Oracle's Java SDK, not the default OpenJDK. It lies in the the AUR (auxiliary) Arch repositories. To be able to grab the AUR packages easily, we first need obtain a tool named yaourt.

That one is found in the Chinese Arch repo:

# Archlinux CN repo (has yaourt and sometimes other interesting tools)
RUN echo "[archlinuxcn]" >> /etc/pacman.conf && \
echo "SigLevel = Optional TrustAll" >> /etc/pacman.conf && \
echo "Server = http://repo.archlinuxcn.org/\$arch" >> /etc/pacman.conf

The second hurdle is installing Android SDK. Fortunately, the dev group at BBQLinux are providing repositories for it:

# Add BBQLinux repo for Android development: http://bbqlinux.org/
RUN echo "[bbqlinux]" >> /etc/pacman.conf && \
echo "Server = http://packages.bbqlinux.org/\$repo/os/\$arch" >> /etc/pacman.conf && \
pacman-key -r 04C0A941 && \
pacman-key --lsign-key 04C0A941

# Add multilib repo for the Android SDK (it requires 32bit libs)
RUN sed -i '/#\[multilib\]/,/#Include = \/etc\/pacman.d\/mirrorlist/ s/#//' /etc/pacman.conf && \
sed -i '/#\[multilib\]/,/#Include = \/etc\/pacman.d\/mirrorlist/ s/#//' /etc/pacman.conf && \
sed -i 's/#\[multilib\]/\[multilib\]/g' /etc/pacman.conf

I will skip the steps that perform the actual installing of Oracle Java and Android SDK and focus instead on the final bits of the Dockerfile. The default location where Android SDK gets installed by pacman is /opt/android-sdk, so make sure you reference it properly when setting up the platform tools. I chose version 25, you may want to experiment with newer versions.

# Install Android build tools
RUN yes | /opt/android-sdk/tools/bin/sdkmanager \
"platforms;android-25" "build-tools;25.0.2" "extras;google;m2repository" \
"extras;android;m2repository"

# Set up Cordova
RUN npm install -g cordova

The complete Dockerfile can be found on Github. We are now ready to build the Docker image on the host machine:

docker build -t mydockeruser/archcordova .