Docker 101

Docker for Developers

More precisely I should call this section "Docker for Fly-By Developers," because everybody likes a spot of coding, right?

One would presume developing for mobile devices would have become democratized by now. It is true that there are plenty of frameworks that let you use your favorite programming language to create apps for the likes of Android; however, setting up the SDK, NDK, libraries, dependencies, toolchains, and so on is a bit complicated … . No! Scratch that. More like: "Setting up the Android SDK, NDK, and so on is an absolute hell-hole of broken dependencies that will drive the hardiest among us to total and irredeemable insanity." There, much better.

What is the lazy and cavalier programmer to do? Use Docker, of course.

Turns out there are Docker images that provide all the hooks and doodads for Cordova (see the "Cordova" box). They also provide a correct and working installation of the Android tools you need to build and deploy your apps.

Cordova

Cordova [8] is a framework that allows you to create Android, iOS, and Windows apps using HTML, JavaScript, and CSS. Its aim is to democratize the creation of apps for mobile devices, especially Android. However, the complexity of installing the Android bits and pieces and making it work with a real phone dampens this lofty goal.

To get started, check what the Docker repository has to offer,

docker search cordova

and grab the image with the highest score. At the moment of writing, that was beevelop/cordova:

docker pull beevelop/cordova

Cordova provides a way of generating a skeleton app that you can run to test things, and later you can expand it to include your own features.

To build it, move to the directory to the location in which you want to store your app and run:

docker run --rm -i -v /$PWD:/workspace -w /workspace --privileged beevelop/cordovacordova create hello come.example.helloHelloWorld

Don't panic: It isn't as hard as it looks. The first option, --rm, makes sure that Docker deletes the container after it has run the command you pass to it. Because you don't want the container hanging around after every step of the process, this is a good idea.

You already know what -i does: It gives you interactivity with the container. If there are any questions to answer or fields to fill in, -i makes sure you will be able to do that.

The -v option is also familiar: It mounts the directory from the host entered before the colon into the container at the directory indicated after the colon. In this case, it will mount your current directory ($PWD) into a directory called workspace/ within the container.

The option -w is new, but not difficult to grasp: It tells Docker which directory it should use as the working directory. In this case, all the files Cordova generates will go into the workspace/ directory you created with the -v option.

The --privileged option gives superuser-like privileges to the container and allows Cordova to read from and write to the directories you mounted.

The cordova create hello come.example.hello HelloWorld chain is the Cordova command line to run. The cordova create part creates a new project, and hello is the directory where all the projects files will live. The come.example.hello part provides the basic building template for the HelloWorld project (you can call it something else, by the way) and is part of the standard Cordova package.

After running the instruction, a hello/ subdirectory will pop up in your current directory that contains a basic framework for an app. I will not go into each of the bits and pieces, because I will be looking at Cordova-based mobile applications in a future article, but you should now change into your hello/ directory, because the rest of the instructions in this tutorial require that you execute them from within a Cordova-generated folder.

So far, Cordova has generated a platform-neutral application. Because Cordova allows you to build for more than one platform, the next step is to download all the stuff you need for Cordova to adapt the app to a specific platform.

To see what platforms are available, you can enter:

docker run --rm -i -v /$PWD:/workspace -w /workspace --privileged beevelop/cordovacordova platform list

The only new thing here is cordova platform list, which is self-explanatory (Figure 5).

Figure 5: Cordova tells you for which platforms you can build your app.

To add a platform (e.g., Android), you use cordova platform add:

docker run --rm -i -v /$PWD:/workspace -w /workspace --privileged beevelop/cordovacordova platform add android

You can add as many platforms as you like.

The next step is building the code for the platform(s) you just added:

docker run --rm -i -v /$PWD:/workspace -w /workspace --privileged beevelop/cordovacordova build

After some rather profuse output, Cordova informs you it has built an Android Package Kit (APK) and placed it into the platforms/android/app/build/outputs/apk/debug/ subdirectory.

You could now copy the APK to your device and install it by hand, or you can have Cordova do that for you and run the app as a test.

The first matter of business is to make sure Cordova can talk to your device. First, make sure your phone is ready and in development mode. Follow the instructions in the "Prepare Your Phone" box. Second, run the instruction:

Prepare Your Phone

To make your phone ready for development, go to Settings | About phone and scroll down until you see the Build number section. Tap on that several times until your phone tells you that you have become a developer.

Connect your phone to your computer using a USB cable and move back to Settings. Now, you will see a new submenu called Developer options. Tap on that and scroll down until you see the USB debugging option. Activate it.

Your phone is ready.

docker run --rm -i --privileged -v /dev/bus/usb:/dev/bus/usbbeevelop/cordova adb devices

In this command line, you are sharing your /dev/bus/usb/ directory with your container, since that is where Cordova will find your phone. Cordova itself is using the Android Debug Bridge (adb) to try and locate your phone. The devices option shows a list of connected devices.

The first time around, your device may show up as unauthorized. This is normal. Go into Settings | Developer options and make sure you have enabled USB debugging. While you are there, again run

docker run --rm -i --privileged -v /dev/bus/usb:/dev/bus/usbbeevelop/cordova adb devices

and a dialog will pop up on your phone asking you to authorize your computer (Figure 6). Give your computer permission, and try listing your devices again. Your phone should now appear as available. Now you can push your app to your phone:

docker run --rm -i -v /$PWD:/workspace -w /workspace --privileged-v /dev/bus/usb/:/dev/bus/usb/beevelop/cordova cordova run android
Figure 6: You need to authorize your computer before you can transfer your app to your phone.

Cordova will automatically install and run the app, so you can check that everything is okay (Figure 7).

Figure 7: Cordova's Hello World app running on a phone.

Conclusion

Docker is being marketed as a solution for professional sys admins who manage dozens of services on busy server farms. True, Docker and all the other technologies built up around it are super-useful for those guys.

However, it is also useful for the rest of us: the casual home admin or amateur developer who wants to tinker or build stuff for personal use. That is why I will be incorporating Docker into my toolbox in future installments of this series.

In the meantime, have fun playing with Docker!

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • An open source, federated video sharing platform

    With PeerTube you can self-host your videos without the limitations embedded in YouTube and similar platforms.

  • Tutorials – Cordova Sensor

    Frameworks like Cordova make creating simple mobile apps quite easy. Making apps that use your phone's sensor is slightly trickier, but, thanks to a new universal standard, things are not as hard as you may think.

  • Cordova

    Roll out an app elegantly and quickly for up to eight operating systems using the Cordova framework. According to the Apache Foundation, the only requirements are knowledge of HTML, CSS, and JavaScript.

  • Docker Open Source Developer Tools

    Docker provides the open source tools and resources for compiling, building, and testing containerized applications.

  • Tube Archivist

    Tube Archivist indexes videos or entire channels from YouTube in order to download them with the help of the yt-dlp tool.

comments powered by Disqus