Run Docker and Nomad on Android - truly serverless mobile compute
I run a lot of servers and many workloads around the world on my own mini-cloud. It's distributed amongst Hetzner, Azure and AWS. Some of these workloads are better located on the edge, especially when there are sensitive credentials or cost concerns or whatever. Remember, the cloud is just someone else's computer.
I have been contacted by a friend who's running a pretty cool use case and I suggested to use Docker on many mobile phones to do what he wants to do. The whole mobile farm is running and is managed by Nomad just like a regular cluster, only that it's really mobile. More on that probably in another article.
I really believe that mobile compute like this is the future. Just imagine how much compute power can be accumulated easily and quickly: everyone has a mobile phone right? But first things first, you certainly came here for a guide and not my technical ruminations.
Note: Raspberry pi's could also be an option. But they involve too many cables, they don't come with a screen, storage or battery and aren't compact or optimized like a mobile. Besides, a Raspberry pi looks sketchy to people who don't know what it is, really. Mobile terminals is where it's at.
Get an Android phone
I'm not particularly a fan of any brand of mobile phones, they're just machines, the condition for something like this to work though is to get something easy and forgiving to tinker with, hence Android.
It should be possible to do something like this on an iPhone as well, I'm sure, although with a bit more effort and I don't want to be the one that publicly documents something like that. The ultimate goal is to get a configurable compute running on the phone by avoiding the "app" way, which 10 times out of 10 sent me yak shaving for months without end (Kotlin? Flutter? Java? React Native?). No, I'm a man of simple tastes.
There is a very handy utility to debug any android phone you have physical access to. It's called adb and it will be a great help, I paid for the device and I am in Europe/Germany, you better believe I'll be making it do what I want it to do (without Java too, thank you)
To get setup, install adb, fastboot and throw in scrcpy in the mix too. I'm doing all these operations from a vanilla Ubuntu.
Once setup, enable developer mode, USB debugging and OEM Unlock on your Android phone. There are enough guides for this online.
Note: Enable OEM Unlock only for Test 1. It isn't necessary for Test 2.
Test 1 - Difficult : Root the Android phone
In it's pristine state, the phone is like a computer you'd get at your #day-job, it doesn't allow you to install anything outside of a specific catalog of apps, you don't get sudo rights (aka footgun) and if you try to do anything sketchy, you're at the risk of breaking (bricking) the device or locking yourself out from the app stores. This is of course to prevent hackers from getting access to your phone and syphoning your private data. That's an activity reserved for other, more honest entities.
Bad things tend to happen though when you're expected to program or do anything on a device that was intended for content consumption in the first place: little documentation, a lot of trial and error, wrong documentation... Solitude.
One common way to get those sudo rights on Android is to root the device, meaning removing any safeguards and locks.
Rooting the device (aka jailbreaking it), means installing some "stuff" on it to remove the shackles keeping us from being 10x mobile developers and doing fun things like installing a custom kernel or removing pre-installed applications. This is a risky operation and depends on the device, if you don't know what you're doing you can completely destroy your phone. Only do it on spare, expendable phones and always think before running a command or clicking a shiny download button.
I suggest you check other articles by people smarter than me on how to root your specific phone. I had success with a tool called Magisk. The high level steps are to get some file, copy it on the phone, put your phone in recovery mode and then flash the file using Magisk/Odin.
One huge warning: Take your own images and run any software you download on a separate computer. You've been warned.
I got a Samsung A22 which was discounted for 120€, your mileage may vary. The only grief I have with this device is that the battery cannot be removed easily. But thankfully, the EU has my back for future devices. Removable batteries will definitely extend the lifespan of the devices.
Once that is done, you're officially in the Wild West with a rooted phone, and the real fun can start. (still no Java in sight btw) Basically just install stuff on it, you're good to go!
It is utterly confusing though and there are no recipes, just mashed up text and guides, and most of the guides are not correct. They just replace the phone model with the model you're searching for (an attempt at SEO) and call it a day. Also, it is weird that you get redirected to sketchy download sites where you have to download 4 GB firmware files to only use a 32MB Boot.img file. Ultimately, I got it to work, but it felt dirty so I reset the phone to its initial state.
There really has to be a better way.
Useful links:
- Magisk Flashable zip: https://magiskapp.com/zip/#download-now
- How to Root: https://www.getdroidtips.com/root-samsung-a22-magisk/
- Odin Flash Tool: https://odinflashtool.com/download/odin3-v3-14-4/
- Extract image: https://www.youtube.com/watch?v=DyUainEJwLM
- TWRP Recovery https://www.getdroidtips.com/twrp-recovery-samsung-galaxy-a22-sm-a225f/#Steps-to-Install-TWRP-Recovery-on-Galaxy-A22-SM-A225F
- Best guide but for Motorola https://www.mobilewithdrivers.com/twrp/samsung-galaxy-a22-5g-128gb-6gb-ram
- Unlock bootloader https://www.youtube.com/watch?v=lmg7En7sjOY
- Best complete guide: https://www.youtube.com/watch?v=l9PJDpJYlt8
- Run command in termux https://github.com/termux/termux-app/issues/924
Trial 2 - Easy: Running a VM on Android
At first this got me thinking. Why would I want to run a VM on Android? Well because I can get root within that VM and essentially run what I want on it directly, keeping the host clean.
There are three stars for this trick: Termux, QEMU and Alpine Linux.
Termux is a terminal emulator and Linux environment, we can use to run different commands and tools. It's fantastic software and I recommend installing it from F-Droid. By the way, it has nothing to do with tmux!
QEMU is an emulator. It's fantastic technology, read a bit about it, a great place to start is their main website.
Alpine Linux is a version of Linux that comes from a secret penguin hideout in the Alps. Obviously, what I said is not true, Alpine is a lightweight Linux distribution, just like Ubuntu, sort of.
Start of by installing Termux. Download it from F-Droid and then install it on your mobile phone using adb. Then start it and play around with the interface a bit. Look up how to install things and run things. I find it's a fantastic piece of software. It'll get a bit clunky to type on the phone with your fingers, so I recommend using a tool to simplify the next steps.
If you have many devices or don't like typing long commands on the phone, I'd recommend going with https://github.com/Genymobile/scrcpy to display the phone screens on your computer and send keystrokes to multiple devices at the same time.
The legacy paste flag allows you to copy paste from your clipboard directly into the phone. Neat? Neat!
Installing the VM
Now you can start installing the real stuff.
You can pipe all these commands through adb, with ssh or scrcpy but you need to run them within Termux.
You can login with the user root and no password, we will set it shortly.
This will ask you to setup your root password and format the disk we created earlier.
The setup should be complete when you see "Installation finished. No error reported". There might be another thing that will display a loading bar with no indication of what is going on, I love those, totally help with my anxiety. All in all this process might take from 10 to 30 minutes depending on the capabilities of the device.
The real last message is "Installation is complete. Please reboot." Läuft! Thank you, next.
You can now shutdown the VM using the poweroff command, don't reboot yet, what's QEMU gonna do anyway?
Installing and running docker
Alright after all that yak shaving, let's get to work.
Now witness the glory of scrolling text and wait for the VM to boot up and login with root and the password you provided (What do you mean you forgot it?)
You can also put the above snippet in a bash file and adapt it to your needs (more memory, other port forwards, whatever) and now we can install docker.
From here on out, it's open bar. You did it! 🎉
But why stop here? You can try installing podman, nomad, LXC, heck even QEMU again. Yes, Kubernetes too. Plug a Sim card in for the lulz and see what happens! (I only tried nomad and docker so far!)
The wonders of the world belong to the explorers and builders. Try stuff out and please write about it.
What's left now? Getting access to the camera, microphone and other hardware stuff within the VM and do funny things. What is that you say? Not possible?
Resources used:
- https://gist.github.com/oofnikj/e79aef095cd08756f7f26ed244355d62 (1 to 1 copy with updates and adaptation, there are great comments on that gist)
- https://www.youtube.com/watch?v=IthUo9zVfmg Great video
- https://github.com/egandro/docker-qemu-arm
- https://ownyourbits.com/2018/06/13/transparently-running-binaries-from-any-architecture-in-linux-with-qemu-and-binfmt_misc/
Closing thoughts
Once all of this is understood, you can automate it with ansible through a hub for an extra fun challenge. I've had loads of fun with these pocket servers frankly.
Is that a Kubernetes cluster in your pocket or are you just happy to see me?
However, we need a way to run Linux on the devices directly. I don't need the apps to be honest.
It's quite inefficient now and I admit it, since what we're doing is Android > Termux > VM but it's okay I guess, it works. I'm pretty sure someone can fine tune this and run a cloud provider completely on top of mobile phones. That would be absolutely hilarious! How would we call this? Serverless-less!
There is an estimate of 15 billion mobile devices in the world (3 billion of them run Java). By pooling their resources we could surely do some fun mind bending stuff. I certainly do with 30.
Without Java!
PS: Did you know this works on some car infotainment systems too? ;-)