Skip to content

Instantly share code, notes, and snippets.

@oofnikj
Last active June 2, 2023 01:46
Embed
What would you like to do?
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.12/main http://dl-cdn.alpinelinux.org/alpine/v3.12/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 http://dl-cdn.alpinelinux.org/alpine/v3.12/releases/x86_64/alpine-virt-3.12.3-x86_64.iso
    
  • Create disk (note it won't actually take 4GB of space, more like 500MB)

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

    qemu-system-x86_64 -machine q35 -m 1024 -smp cpus=2 -cpu qemu64 \
      -drive if=pflash,format=raw,read-only,file=$PREFIX/share/qemu/edk2-x86_64-code.fd \
      -netdev user,id=n1,hostfwd=tcp::2222-:22 -device virtio-net,netdev=n1 \
      -cdrom alpine-virt-3.12.3-x86_64.iso \
      -nographic alpine.img
    
  • Login with user root (no password)

  • Setup network (press Enter to use defaults):

     localhost:~# 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] 
     localhost:~# ifup eth0
    
  • Create an answerfile to speed up installation:

    localhost:~# wget https://gist.githubusercontent.com/oofnikj/e79aef095cd08756f7f26ed244355d62/raw/answerfile
    
  • Patch setup-disk to enable serial console output on boot

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

    localhost:~# setup-alpine -f answerfile
    
  • Once installation is complete, power off the VM (command poweroff) and boot again without cdrom:

    qemu-system-x86_64 -machine q35 -m 1024 -smp cpus=2 -cpu qemu64 \
      -drive if=pflash,format=raw,read-only,file=$PREFIX/share/qemu/edk2-x86_64-code.fd \
      -netdev user,id=n1,hostfwd=tcp::2222-:22 -device virtio-net,netdev=n1 \
      -nographic alpine.img
    
  • 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
@alick97
Copy link

alick97 commented Mar 25, 2023

when setup-disk error and not installed to alpine.img, you can run qemu with -hda alpine.img and export ERASE_DISKS=/dev/sda before setup-alpine

@directentis1
Copy link

directentis1 commented Apr 1, 2023

Is it works on proot (fake root) on some interactive shells?

Everything is okay until the boot qemu step, i got this error: qemu-system-x86_64: -drive if=pflash,format=raw,read-only,file=/usr/share/qemu/edk2-x86_64-code.fd: Could not open '/usr/share/qemu/edk2-x86_64-code.fd': No such file or directory

btw, I can't install qemu-common qemu-system-x86_64-headless with proot on my debian machine.

@sdshan8
Copy link

sdshan8 commented Apr 1, 2023

Is it works on proot (fake root) on some interactive shells?

Everything is okay until the boot qemu step, i got this error: qemu-system-x86_64: -drive if=pflash,format=raw,read-only,file=/usr/share/qemu/edk2-x86_64-code.fd: Could not open '/usr/share/qemu/edk2-x86_64-code.fd': No such file or directory

btw, I can't install qemu-common qemu-system-x86_64-headless with proot on my debian machine.

Try Installing qemu-system-x86_64-headless in termux then using /data/data/com.termux/files/usr/share/qemu/edk2-x86_64-code.fd instead of /usr/share/qemu/edk2-x86_64-code.fd

@directentis1
Copy link

Is it works on proot (fake root) on some interactive shells?
Everything is okay until the boot qemu step, i got this error: qemu-system-x86_64: -drive if=pflash,format=raw,read-only,file=/usr/share/qemu/edk2-x86_64-code.fd: Could not open '/usr/share/qemu/edk2-x86_64-code.fd': No such file or directory
btw, I can't install qemu-common qemu-system-x86_64-headless with proot on my debian machine.

Try Installing qemu-system-x86_64-headless in termux then using /data/data/com.termux/files/usr/share/qemu/edk2-x86_64-code.fd instead of /usr/share/qemu/edk2-x86_64-code.fd

Umm, but I mean I'm using Interactive Shells from Amazon Cloud9, I don't have root privileges on it so wondering is it possible to install Docker via proot?

@Joanvoo
Copy link

Joanvoo commented Apr 4, 2023

i have no internet why?

localhost:# ifup eth0
udhcpc: started, v1.31.1
udhcpc: sending discover
udhcpc: sending select for 10.0.2.15
udhcpc: lease of 10.0.2.15 obtained, lease time 86400
localhost:
# wget https://gist.githubusercontent.com/oofnikj/e79aef095cd08756f7f
26ed244355d62/raw/answerfile
wget: bad address 'gist.githubusercontent.com'
edit:
This network setting work for me:
localhost: # echo -e "auto lo\niface lo inet loopback\nauto eth0\niface eth0 inet dhcp" > /etc/network/interfaces
localhost: # /etc/init.d/networking restart
localhost:~# echo nameserver 1.1.1.1 > /etc/resolv.conf
nameserver 8.8.8.8 does not work for me

@sdshan8
Copy link

sdshan8 commented Apr 5, 2023

Is it works on proot (fake root) on some interactive shells?
Everything is okay until the boot qemu step, i got this error: qemu-system-x86_64: -drive if=pflash,format=raw,read-only,file=/usr/share/qemu/edk2-x86_64-code.fd: Could not open '/usr/share/qemu/edk2-x86_64-code.fd': No such file or directory
btw, I can't install qemu-common qemu-system-x86_64-headless with proot on my debian machine.

Try Installing qemu-system-x86_64-headless in termux then using /data/data/com.termux/files/usr/share/qemu/edk2-x86_64-code.fd instead of /usr/share/qemu/edk2-x86_64-code.fd

Umm, but I mean I'm using Interactive Shells from Amazon Cloud9, I don't have root privileges on it so wondering is it possible to install Docker via proot?

i cant help with that, try copying the edk2 file to the interactive shell somehow

@RedstoneWizard08
Copy link

i have no internet why?

localhost:# ifup eth0 udhcpc: started, v1.31.1 udhcpc: sending discover udhcpc: sending select for 10.0.2.15 udhcpc: lease of 10.0.2.15 obtained, lease time 86400 localhost:# wget https://gist.githubusercontent.com/oofnikj/e79aef095cd08756f7f 26ed244355d62/raw/answerfile wget: bad address 'gist.githubusercontent.com'

Yea same here

localhost:~# ifup -a
udhcpc: started, v1.35.0
udhcpc: broadcasting discover
udhcpc: broadcasting select for 10.0.2.15, server 10.0.2.2
udhcpc: lease of 10.0.2.15 obtained from 10.0.2.2, lease time 86400
localhost:~# wget google.com
wget: bad address 'google.com'

@reski-rukmantiyo
Copy link

i have no internet why?
localhost:# ifup eth0 udhcpc: started, v1.31.1 udhcpc: sending discover udhcpc: sending select for 10.0.2.15 udhcpc: lease of 10.0.2.15 obtained, lease time 86400 localhost:# wget https://gist.githubusercontent.com/oofnikj/e79aef095cd08756f7f 26ed244355d62/raw/answerfile wget: bad address 'gist.githubusercontent.com'

Yea same here

localhost:~# ifup -a
udhcpc: started, v1.35.0
udhcpc: broadcasting discover
udhcpc: broadcasting select for 10.0.2.15, server 10.0.2.2
udhcpc: lease of 10.0.2.15 obtained from 10.0.2.2, lease time 86400
localhost:~# wget google.com
wget: bad address 'google.com'

change your dns into your own. scroll up

@rahuls360
Copy link

To debug/resolve the DNS issues follow the below steps. Hope it saves someone else few hours

ping 8.8.8.8 -> Worked for me
ping google.com -> Failed for me
Edit DNS file -> vm etc/resolv.conf -> Add google's DNS on 2nd line nameserver 8.8.8.8 (it doesn't work, try other DNS)
ping google.com will work now
ping gist.githubusercontent.com will also work
Continue with next steps

@chenoi
Copy link

chenoi commented May 15, 2023

Hi...after reboot all the setup answerfile, interface eth0 setup ....everything gone and I have to gone thru the whole process and once again after 'Once installation is complete, power off the VM (command poweroff) and boot again without cdrom:' everything gone....need some advise here to make it persistent. Thanks

@bassem-saeed
Copy link

After following these instructions I will be able to use docker and access device folder at the same time?
(device folders like /storage/emulated/0/

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