├── .gitignore ├── README.md ├── cloudinit-meta-data ├── cloudinit-user-data ├── cloudinit.img ├── create-cloudinit-img.sh ├── install-docker ├── install-vm ├── start-vm └── vars /.gitignore: -------------------------------------------------------------------------------- 1 | ubuntu.img 2 | bind/ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Docker in Android (via Termux) 2 | 3 | Run Docker on your phone using Termux and QEMU 4 | 5 | ## Getting Started 6 | 7 | ### In Termux (in Android) 8 | 9 | ```shell 10 | # Install QEMU and set up ubuntu VM 11 | ./install-vm 12 | 13 | # Add ssh key to bind/authorized_keys 14 | gpg --export-ssh-key keyid > bind/authorized_keys 15 | 16 | # wait for the vm to fully start. It takes a few minutes 17 | 18 | # ** open another shell in termux ** 19 | # run some containers! 20 | 21 | docker run node 22 | 23 | ``` 24 | 25 | ## Troubleshooting 26 | 27 | `Host key verification failed` 28 | Connect once to the VM using SSH so the host is added to ~/.ssh/known_hosts. 29 | 30 | Other SSH auth related errors. Be sure the host bound files in /mnt/host are owned by `ubuntu:ubuntu` and have proper permissions (600 for authorized_keys). 31 | -------------------------------------------------------------------------------- /cloudinit-meta-data: -------------------------------------------------------------------------------- 1 | instance-id: 4afee4f2-9236-4ad9-980e-19e625a01ac5 2 | -------------------------------------------------------------------------------- /cloudinit-user-data: -------------------------------------------------------------------------------- 1 | #cloud-config 2 | password: passw0rd 3 | chpasswd: { expire: False } 4 | ssh_pwauth: True 5 | 6 | package_update: true 7 | #package_upgrade: true 8 | 9 | packages: 10 | - docker.io 11 | 12 | mounts: 13 | - [ host0, /mnt/host, 9p, "trans=virtio,version=9p2000.L" ] 14 | 15 | runcmd: 16 | - chown ubuntu:ubuntu /mnt/host 17 | - mkdir -p /home/ubuntu/.ssh 18 | - chmod 700 /home/ubuntu/.ssh 19 | - su - ubuntu -c 'ln -sf /mnt/host/authorized_keys /home/ubuntu/.ssh/' 20 | - chown -R ubuntu:ubuntu /home/ubuntu/.ssh 21 | - usermod -aG docker ubuntu 22 | -------------------------------------------------------------------------------- /cloudinit.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nuttingd/docker-in-android/1ad7d91c323b8ee0243c011ea2dc3a7648efea72/cloudinit.img -------------------------------------------------------------------------------- /create-cloudinit-img.sh: -------------------------------------------------------------------------------- 1 | set -e 2 | 3 | if ! command -v uuidgen > /dev/null 4 | then 5 | apt update && apt install uuid-runtime 6 | fi 7 | 8 | if ! command -v cloud-localds > /dev/null 9 | then 10 | apt update && apt install cloud-image-utils 11 | fi 12 | 13 | # next: mount authorized_keys from host 14 | # https://superuser.com/questions/628169/how-to-share-a-directory-with-the-host-without-networking-in-qemu 15 | 16 | echo "instance-id: $(uuidgen || echo i-abcdefg)" > cloudinit-meta-data 17 | 18 | cloud-localds cloudinit.img cloudinit-user-data cloudinit-meta-data 19 | 20 | #kvm -net nic -net user,hostfwd=tcp::2222-:22 \ 21 | # -drive file=disk1.img,if=virtio -drive file=my-seed.img,if=virtio 22 | #ssh -p 2222 ubuntu@localhost 23 | -------------------------------------------------------------------------------- /install-docker: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | script_dir=`dirname $0` 3 | source $script_dir/vars 4 | 5 | # enable root-repo for docker CLI package 6 | echo Enabling root-repo... 7 | pkg install root-repo 8 | pkg update 9 | 10 | echo Installing docker... 11 | pkg install docker 12 | 13 | echo Installing docker-compose... 14 | pkg install docker-compose 15 | 16 | echo Creating docker context docker-in-android for SSH access 17 | docker context create \ 18 | --docker host=ssh://ubuntu@localhost:$host_ssh_port \ 19 | --description="Docker-in-Android Remote" \ 20 | docker-in-android 21 | 22 | echo Switching to docker-in-android context... 23 | docker context use docker-in-android 24 | -------------------------------------------------------------------------------- /install-vm: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | script_dir=`dirname $0` 3 | source $script_dir/vars 4 | 5 | img_path=$script_dir/ubuntu.img 6 | 7 | echo Installing dependencies... 8 | pkg update 9 | pkg install qemu-utils qemu-common qemu-system-x86_64-headless wget 10 | 11 | if [[ ! -f $img_path ]] 12 | then 13 | echo Downloading ubuntu cloud image... 14 | # https://cloud-images.ubuntu.com/minimal/releases/focal/release/ 15 | # https://cloudinit.readthedocs.io/en/latest/topics/datasources/nocloud.html 16 | # https://stackoverflow.com/questions/29137679/login-credentials-of-ubuntu-cloud-server-image 17 | wget -O $img_path $ubuntu_img_url 18 | qemu-img resize $img_path $img_size 19 | fi 20 | 21 | $script_dir/start-vm 22 | -------------------------------------------------------------------------------- /start-vm: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | script_dir=`dirname $0` 3 | source $script_dir/vars 4 | 5 | echo Starting VM... 6 | echo 7 | echo mem: $mem 8 | echo cpus: $cpus 9 | echo 10 | qemu-system-x86_64 -machine q35 -m $mem -smp cpus=$cpus -cpu qemu64 \ 11 | -drive if=pflash,format=raw,read-only=on,file=$PREFIX/share/qemu/edk2-x86_64-code.fd \ 12 | -drive file=cloudinit.img,if=virtio,format=raw,read-only=on \ 13 | -netdev user,id=n1,hostfwd=tcp::$host_ssh_port-:22 -device virtio-net,netdev=n1 \ 14 | -nographic \ 15 | -virtfs local,path=$script_dir/bind,mount_tag=host0,security_model=mapped,id=host0 \ 16 | $script_dir/ubuntu.img 17 | 18 | -------------------------------------------------------------------------------- /vars: -------------------------------------------------------------------------------- 1 | img_size=5G 2 | host_ssh_port=2222 3 | mem=1024 4 | cpus=4 5 | ubuntu_img_url='https://cloud-images.ubuntu.com/minimal/releases/focal/release/ubuntu-20.04-minimal-cloudimg-amd64.img' 6 | --------------------------------------------------------------------------------