There are various ways of running Docker containers on Android. First, Docker will not work in proot. And because Android kernel lacks the features which are required by dockers to run, even with root permission you still cannot run docker in chroot environment.
The non-rooted method of running Dockers on Android is to set up a virtual machine and install docker in it, see oofnikj - Docker on Termux in a VM. However this method is freaking slow.
In order to run Docker containers on Android without virtual machine and chroot (which means native and better performance), we must our Android phone and compile a custom kernel for it.
My Device: Sony Xperia 5 II (pdx206). LineageOS 20 (Android 13). The source code of the kernel is available on my Github repository.
1. Check kernel compatibility
First I rooted my Sony Xperia 5 II. Then I installed LineageOS 20.
Install Termux. Then execute Moby’s script to check kernel’s compatibility o running docker.
|
|
- The missing configs will be displayed. Take notes of these red missing configs (especially configs under
Generally Necessary
), we have to enable them during kernel compliation.
2. Compile custom Android kernel
In 2021, I had built a docker compatible kernel for Xiaomi Redmi Note 5 Pro (whyred). But at that time I built the kernel out of source tree (standalone) and it was hardly to done for other devices. Therefore, this time I decide to build the kernel with the source tree.
There is an offcial LineageOS port of pdx206.
We need a 64-bit Linux PC to compile the kernel. Ubuntu would be a good choice, however I use Arch Linux.
First follow the steps of Build for pdx206 - LineageOS wiki.
After syncing the code of LineageOS, we can build the kernel only. A boot.img
will be generated after compliation.
- Enter the compliation environment.
|
|
- Go to the directory of kernel. Generate
.config
|
|
- Start menu configuration
|
|
A menu will pop up. Find the missing configs which listed in Moby’s script and enable them. Use Arrow keys to move, press Space to enable/disable configs. Don’t forget to hit Save before exit.
We can search the configs. For example, to find
CONFIG_IP_VS
, type/
and type the config name, it shall tell you where it is.According to the
prompts
ofCONFIG_IP_VS
, we know it is located at Networking Support -> Networking options -> Network packet filtering framework (Netfilter) -> IP virtual server support.Some configs such as
CONFIG_CGROUP_HUGETLB
are not available in Android kernel because the kernel of the device is too old.CONFIG_BINFMT_MISC
should also be enabled in order to run x86 apps (or docker images) on ARM.After enabling missing configs, we have to modify the code of
kernel/Makefile
. (usepatch
command or edit the code directly)
|
|
- And modfiy
net/netfilter/xt_qtaguid.c
:
|
|
- After that, replace original defconfig with new config
|
|
- Compile the kernel. The output
boot.img
will be located at~/android/lineage/out/target/product/pdx206/
.
|
|
Docker requires root permission to run, so we send
boot.img
to the phone. Then open Magisk and click “Install” β “Patch boot.img”. Finally transfer themagisk-patched-boot.img
back to PC.There is no need to reflash ROM after flashing a new kernel, but we should backup the original
boot.img
in the first place in the case our phone failed to boot.Power off the device. Enter fastboot mode, flash
boot.img
, that is it.
|
|
3. Running Docker containers
A message “There is an internal problem with your device” will pop up on every boot. Just ignore it.
- Open Termux, mount cgroups
|
|
- Enable binfmt_misc
|
|
Execute Moby’s script again:
sudo ./check-config.sh
. Make sure everything turns green.Install docker and docker-compose.
|
|
- Start docker daemon
|
|
- Swipe from left edge of the screen and open a new session. Run hello-world containers
|
|
We shall see this
To run docker containers with
--init
arguments, installtini
:
|
|
Now try to run more containers!
Note: The docker packages had been patched by Termux developers but still not fully-functional. Currently docker-compose
is broken on Termux. And while running web services, you must add --net=host --dns=8.8.8.8
arguments.
You can run qus containers for eumlating and running x86 images on ARM.
Before exiting Termux, press CTRL+C to terminate docker daemon.
4. Install Flatpak applications
Flatpak package is not available in Termux’s repository, we have to create a chroot environment and install Flatpak in it.
Modfiy the starting script of chroot. Add this on the top to solve
Failed to make / slave: Invalid argument
error
|
|
- Install Flatpak. Logout. Reboot the phone.
|
|
- Now we can install Flatpak applications. For instance
ffmpeg
, which is included in Freedesktop Platform:
|
|
- Start dbus daemon before running a Flatpak application
|
|
- Before running Flatpak applications, add
--devel
argument to view debug info.
|
|