Skip to content

Instantly share code, notes, and snippets.

@Mhnramin
Forked from oofnikj/answerfile
Last active August 17, 2022 06:10
Show Gist options
  • Save Mhnramin/41ed38759c628a9ae941a7f59199d006 to your computer and use it in GitHub Desktop.
Save Mhnramin/41ed38759c628a9ae941a7f59199d006 to your computer and use it in GitHub Desktop.
Install Docker on Termux
KEYMAPOPTS="us us"
HOSTNAMEOPTS="-n alpine"
INTERFACESOPTS="auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
hostname alpine
"
TIMEZONEOPTS="-z UTC"
PROXYOPTS="none"
APKREPOSOPTS="http://dl-cdn.alpinelinux.org/alpine/v3.16/main http://dl-cdn.alpinelinux.org/alpine/v3.16/community"
SSHDOPTS="-c openssh"
NTPOPTS="-c busybox"
DISKOPTS="-v -m sys -s 0 /dev/sda"

Docker on Termux [in a VM]

Create a Linux VM and install Docker in it so you can (slowly) run x86 Docker containers on your Android device.

Recommended to use SSH or external keyboard to execute the following commands unless you want sore thumbs. See https://wiki.termux.com/wiki/Remote_Access#SSH

  • Install QEMU

     pkg install qemu-utils qemu-common qemu-system-x86_64-headless
    
  • Download Alpine Linux 3.12 (virt optimized) ISO

     mkdir alpine && cd $_
     wget https://dl-cdn.alpinelinux.org/alpine/v3.16/releases/x86_64/alpine-virt-3.16.2-x86_64.iso
    
  • Create disk (note it won't actually take 64GB of space, more like 500MB)

     qemu-img create -f qcow2 alpine.img 64G
    
  • Boot it up

    qemu-system-x86_64 -machine q35 -m 2048 -smp cpus=4 -cpu qemu64 -drive if=pflash,format=raw,read-only=on,file=$PREFIX/share/qemu/edk2-x86_64-code.fd -netdev user,id=n1,hostfwd=tcp::2222-:22,hostfwd=tcp::2022-:22,hostfwd=tcp::9000-:9000 -device virtio-net,netdev=n1 -cdrom alpine-virt-3.16.2-x86_64.iso -nographic alpine.img
    

If you have low cpu please change cpus=4 into cpus=2 and if you have high-end devices you can try with -smp 8,sockets=2,cores=2,threads=2,maxcpus=8

  • Login with user root (no password)
  • Setup network (press Enter to use defaults):

     setup-interfaces
    
     Available interfaces are: eth0.
     Enter '?' for help on bridges, bonding and vlans.
     Which one do you want to initialize? (or '?' or 'done') [eth0] 
     Ip address for eth0? (or 'dhcp', 'none', '?') [dhcp] 
     Do you want to do any manual network configuration? [no] 
    
  • After setup network run command below to connect network:

     ifup eth0
    
  • Create an answerfile to speed up installation:

    wget https://gist.githubusercontent.com/Mhnramin/41ed38759c628a9ae941a7f59199d006/raw/84b37a68e0e8ec7775a8e88f8fa0464e7b2a3456/answerfile
    
  • Patch setup-disk to enable serial console output on boot

    sed -i -E 's/(local kernel_opts)=.*/\1="console=ttyS0"/' /sbin/setup-disk
    
  • Run setup to install to disk

    setup-alpine -f answerfile
    
  • Make a password for root login (press Enter to use defaults)

    Setup a user ? (enter a lower-case loginname, or 'no')[no]
    
  • Once installation is complete, power off the VM (command poweroff) and boot again without cdrom:

    qemu-system-x86_64 -machine q35 -m 2048 -smp cpus=4 -cpu qemu64 -drive if=pflash,format=raw,read-only=on,file=$PREFIX/share/qemu/edk2-x86_64-code.fd -netdev user,id=n1,hostfwd=tcp::2222-:22,hostfwd=tcp::2022-:22,hostfwd=tcp::9000-:9000 -device virtio-net,netdev=n1 -nographic alpine.img
    

    make it same with first boot but without -cdrom alpine-virt-3.16.2-x86_64.iso just like above instruction

  • Install docker and enable on boot:

    alpine:~# apk update && apk add docker
    alpine:~# service docker start
    alpine:~# rc-update add docker
    
  • Useful keys:

    • Ctrl+a x: quit emulation
    • Ctrl+a h: toggle QEMU console
  • BIG THANKS TO:

@Mhnramin
Copy link
Author

Mhnramin commented Aug 16, 2022

#TERMUX-SSH
If you want to get access ssh to termux you have find your user with whoami and adding a password into it with passwd and install ssh with pkg install openssh after installation completed then you have to set up the config with `nano $PREFIX/etc/ssh/sshd_config' and add this command

PasswordAuthentication yes
AllowTcpForwarding yes
GatewayPorts yes
ChallangeResponseAuthentication no 
PubkeyAuthentication yes  

Reboot sshd if needed sshd
Then just run ssh your-termux-user@your-termux-ip -p 8022

#ALPINEVM-SSH
Anybody that wants to expose alpine/docker ports to android you can either update the alpine boot script to manually expose ports or just use SSH port forwarding from termux to alpine vm.
Just make sure to have the following with nano /etc/ssh/sshd_config

Find '#PermitRootLogin prohibit-password' and change it to 'PermitRootLogin yes'.(Remove # and replace prohibit-password with yes.)
PasswordAuthentication yes
PubkeyAuthentication yes
AllowAgentForwarding yes
AllowTcpForwarding yes

Reboot sshd if needed service sshd restart
Then just run ssh root@your-termux-ip -p 2022

@Mhnramin
Copy link
Author

Mhnramin commented Aug 16, 2022

#DOCKER-SOCK-ERROR
If you find some problem when running docker like this error Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
you have to run some commands before running docker with

setsid dockerd #wait until finished with message "containerd successfully booted in ~~~~.s"  and press Ctrl+C
setsid containerd #wait until finished with message "API listen on /var/run/docker.sock" and press Ctrl+C

You always have to run this command if you power-off the qemu or reboot it

@Mhnramin
Copy link
Author

#RUN-PONTAINER-DOCKER
If you wanna running pontainer from docker you have to run

docker run -d -p 9000:9000 --name=my-portainer -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer

and access pontainer from your browser with http://your-ip-termux:9000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment