Skip to content

Instantly share code, notes, and snippets.

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
@VPraharsha03
Copy link

VPraharsha03 commented Aug 3, 2021

@egandro I tried assigning eth0's inet address as IP and port 2222 as LISTEN_ADDR in my miniflux configuration as follows LISTEN_ADDR=<eth0's inet addr>:2222 when i try to access miniflux's login page http://x.y.x.y:8080/ it doesn't work. I'm doing something fundamentally wrong here - networking stuff is confusing to me, please guide me

Also is there a reason to use qemu-x86_64 instead of qemu-aarch64 ? Doesn't it complicate things by doing a cross architecture translation?

@egandro
Copy link

egandro commented Aug 3, 2021

@egandro I tried assigning eth0's inet address as IP and port 2222 as LISTEN_ADDR in my miniflux configuration as follows

Silly question. Do you have a USB ethernet pluged into your phone? Where did you get the eth0 device on your phone?
Try using ifconfig on your cellphone in termux:

$ ifconfig -a
  • I have lo, tun0 (from a VPN e.g. Blockada), wlan0.
  • I couldn't manage to get a eth0 with a USB->OTG->USB/Ethernet adapter. So no luck getting eth0. I have no idea if this is possible and what you did to get one.

This qemu param -netdev user,id=n1,hostfwd=tcp::2222-:22 is an internal NAT. If you need more complex szenarios or want to shape the traffic for a specific IP, you have to jump into the documentation. I am no expert on this, but you can do really a lot.

Please keep in mind. The qemu -netdev isn't a full feature blown VMware/VBox nat, bridge, host only network. Why? Stuff that is required from the Android OS (the Linux level below this) hasn't all modules enabled that are required for this - or - you can't even access the required userspace tools on a non rooted phone or without a Custom Rom. If you need that (which I don't - because I see no point in playing with that!) then you have to jump into phone rooting. Good luck with that!

Also is there a reason to use qemu-x86_64 instead of qemu-aarch64 ? Doesn't it complicate things by doing a cross architecture translation?

Yes. This is docker to run x86_64 images on ARM based cell phones. This is done on purpose. We want to run our PC based x86_64 images with this aporach here.

Feel free to adopt it to aarch64 or whatever if you want to run these type of images. This might a good starting point here. The -drive if=pflash,format=raw,read-only,file=$PREFIX/share/qemu/edk2-x86_64-code.fd will be different. You can do that and it's just reading the qemu manual and check how to boot aarm64/Alpine on that. It might be simpler to figure out the parameters for qemu to emulate a supported aarm64 board and to get an Alpine disk image. I see no point playing with that for me, because I need no aarch64 Docker on my cell phone.

@VPraharsha03
Copy link

VPraharsha03 commented Aug 4, 2021

Silly question. Do you have a USB ethernet pluged into your phone? Where did you get the eth0 device on your phone? So no luck getting eth0. I have no idea if this is possible and what you did to get one

I don't see eth0 on termux, but see that inside the alpine VM on the default address (which is something like 10.0.2.0/24). I believe it is due to alpine's default network setup process and i'm using my cellular data.

You can do that and it's just reading the qemu manual and check how to boot aarm64/Alpine on that.

I end up with errors doing that.

@egandro
Copy link

egandro commented Aug 4, 2021

I don't see eth0 on termux, but see that inside the alpine VM on the default address (which is something like 10.0.2.0/24). I believe it is due to alpine's default network setup process and i'm using my cellular data.

But that's not the question.

This is the chain (for ssh):

Android -> 

Qemu (Exposing 2222 to world [lo,wlan0,tun0] from inside the machine 22) -> (done with -netdev user,id=n1,hostfwd=tcp::2222-:22)

Inside of  Alpine "ssh started on port 22"

So if you expose "something" to eth0 inside of Alpine this is of no interesst. Android can't see ports other then this that you added to hostfwd.

For docker it is even more complex. Docker has it's own additional docker network inside of Alpine. You need to start containers with -p foo:bar e.g. -p 8080:80 and then use "-netdev user,id=n1,hostfwd=tcp::2222-:22,hostfwd=tcp::5000:8080" to also expose that port to Android (in this case the 8080 is mapped to 5000).

This isn't a tivial subject. Please read the documentation on qemu / docker.

@FelipeGamer-Dev
Copy link

FelipeGamer-Dev commented Aug 10, 2021

Hi, I tried to install Docker, all ok. but when using docker run hello-world it gave me this error:

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

How to Fix it?

I tried reboot already.

@shixueqiang
Copy link

shixueqiang commented Aug 27, 2021

@Felipe8703 try setsid containerd setsid dockerd

@FelipeGamer-Dev
Copy link

FelipeGamer-Dev commented Aug 27, 2021

@Felipe8703 try setsid containerd setsid dockerd

It gives me all this.
Screenshot_20210827-152613~2

@shixueqiang
Copy link

shixueqiang commented Aug 30, 2021

@Felipe8703 try setsid containerd setsid dockerd

It gives me all this.
Screenshot_20210827-152613~2

maybe you should wait a few minute, in my case, it work

@FelipeGamer-Dev
Copy link

FelipeGamer-Dev commented Aug 30, 2021

@Felipe8703 try setsid containerd setsid dockerd

It gives me all this.
Screenshot_20210827-152613~2

maybe you should wait a few minute, in my case, it work

It gives It:

Screenshot_20210829-205629~2

But re running

docker run hello-world

It Worked, Thanks!

is there a way not to repeat setsid containerd and setsid dockerd every time?

@tuanba-design
Copy link

tuanba-design commented Oct 26, 2021

i try setup and it worked, so good. 🎉🎉 Thanks you.

image

@nmintoh
Copy link

nmintoh commented Nov 16, 2021

Please how do is boot into alpine after powering off without restarting the processes

@hdhnl
Copy link

hdhnl commented Nov 16, 2021

it worked, so good. 🎉🎉

@tuanba-design, Amazing!
How did you apply this background?

@tuanba-design
Copy link

tuanba-design commented Nov 16, 2021

it worked, so good. 🎉🎉

@tuanba-design, Amazing! How did you apply this background?

@hdhnl . i used iTerm2 on Mac & connected termux by ssh. ( so this background apply in iTerm2, and it's not a background in termux )

@ivon852
Copy link

ivon852 commented Dec 14, 2021

Thank you, very useful.

@sebastianmacarescu
Copy link

sebastianmacarescu commented Jan 15, 2022

@egandro this works great, many thanks for the script.
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 in /etc/ssh/sshd_config

AllowAgentForwarding yes
AllowTcpForwarding yes

Reboot sshd if needed service sshd restart
Then just run ssh -L 19999:127.0.0.1:19999 -N -i qemukey root@localhost -p 2222

@Nrjstar
Copy link

Nrjstar commented Feb 9, 2022

Hi how do I access port outside of the VM this is my command docker run --name
myapp_container -p 4000:4000 -d myapp "-netdev user,id=n1,hostfwd=tcp::2222-:22,hostfwd
=tcp::5000:4000". and when enter in the URL . I didn't get any response

@bdwandry
Copy link

bdwandry commented Feb 21, 2022

Hey I don't know if anyone is interested cause I read comments about people having issues with the "answerfile".
But, I do have some backups of a fully setup Alpine instance/.img.

I set it up originally with 256gb of internal storage which is way overkill, but Idc lol... (it doesn't actually take up 256 gb of storage on your device - the VM is allowed to use that much however).

Here is the link to the downloads: https://drive.google.com/drive/folders/1GO86DXXONNvZIDvuKXTbsQ3mkqhyoG50?usp=sharing

You will need: pkg install qemu-utils qemu-common qemu-system-x86_64-headless

Username: 'root'
Password: [No Password - Leave Blank]

There will be two Files:

  1. Fresh install filesystem - (Nothing installed except for updates and changed root password)
  2. Has Wine and XFCE installed (updates and changed root password)

@Nrjstar
Copy link

Nrjstar commented Feb 21, 2022

Thank You!

@2705714131
Copy link

2705714131 commented Mar 5, 2022

~/alpine $ qemu-img create -f qcow2 alpine.img 4G
CANNOT LINK EXECUTABLE "qemu-img": cannot locate symbol "__lshrdi3" referenced by "/data/data/com.termux/files/usr/bin/qemu-img"...
Aborted

how to fix it ?

@s-praba-karan
Copy link

s-praba-karan commented Mar 12, 2022

My local device IP is like 192.168.29.xx but the alpine I am running in termux has the ip like 10.0.2.xx. The VM is able to access the devices with IP 192.168.29.xx but the devices are not able to access the VM. Can someone help me with this?

@bdwandry
Copy link

bdwandry commented Mar 21, 2022

Did you create the answer file? That does some in VM ip configuration.

Since people are having issues downloading the answering file. Just type "vi answerfile"

@vibezjit
Copy link

vibezjit commented Mar 28, 2022

Hey how long do you have to wait for the "starting stream server" to finish. I wait for about 8 minutes and nothing happens and Im not sure if this is just me.

@vibezjit
Copy link

vibezjit commented Mar 28, 2022

Hey I don't know if anyone is interested cause I read comments about people having issues with the "answerfile". But, I do have some backups of a fully setup Alpine instance/.img.

I set it up originally with 256gb of internal storage which is way overkill, but Idc lol... (it doesn't actually take up 256 gb of storage on your device - the VM is allowed to use that much however).

Here is the link to the downloads: https://drive.google.com/drive/folders/1GO86DXXONNvZIDvuKXTbsQ3mkqhyoG50?usp=sharing

You will need: pkg install qemu-utils qemu-common qemu-system-x86_64-headless

Username: 'root' Password: [No Password - Leave Blank]

There will be two Files:

  1. Fresh install filesystem - (Nothing installed except for updates and changed root password)
  2. Has Wine and XFCE installed (updates and changed root password)

how would we import this to termux?

@shreevd2006
Copy link

shreevd2006 commented Apr 2, 2022

I am getting below error while trying to login after reboot

Welcome to Alpine Linux 3.12
Kernel 5.4.188-0-virt on an x86_64 (/dev/ttyS0)

alpine login: root
Login incorrect
alpine login:

@turbomaster95
Copy link

turbomaster95 commented Apr 8, 2022

Hey how do I install portainer in it?

@fgovoni
Copy link

fgovoni commented Apr 25, 2022

alpine:~# docker run -d -p 8888:8888 sandraso/ds-anaconda:1.0 Unable to find image 'sandraso/ds-anaconda:1.0' locally 1.0: Pulling from sandraso/ds-anaconda 58690f9b18fc: Pull complete b51569e7c507: Pull complete da8ef40b9eca: Pull complete fb15d46c38dc: Pull complete f799409a46ed: Pull complete e35ae74b0213: Extracting 1.482GB/1.482GB 512fbe09ca16: Download complete docker: failed to register layer: Error processing tar file(exit status 1): write /opt/conda/lib/libmkl_core.so.1: no space left on device. See 'docker run --help'.

Somebody can help me?
I have more than 100gb free in mysmartphobe

@gyurmogyuri
Copy link

gyurmogyuri commented Apr 27, 2022

Can you try with qemu-system-aarch64 and some aarch64 os? What do you think from speed should be increased?

@ubergeekzone
Copy link

ubergeekzone commented May 6, 2022

when i try to boot it up i get a Illegal instruction error.

@anwarpro
Copy link

anwarpro commented May 23, 2022

Thank you it's working.

@ubergeekzone
Copy link

ubergeekzone commented May 25, 2022

when i try to boot it up i get a Illegal instruction error.

Any idea on this?

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