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
@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?

@McJoe21
Copy link

McJoe21 commented Jul 1, 2022

How do you use the login?

@Saikatsaha1996
Copy link

Saikatsaha1996 commented Jul 3, 2022

@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.

How to access storage?

@N17R0-HACKERS
Copy link

N17R0-HACKERS commented Jul 10, 2022

_PXE-E16: No valid offer received.
BdsDxe: failed to load Boot0003 "UEFI PXEv4 (MAC:525400123456)" from PciRoot(0x0)/Pci(0x2,0x0)/MAC(525400123456,0x1)/IPv4(0.0.0.0,0x0,DHCP,0.0.0.0,0.0.0.0,0.0.0.0): Not Found

@binhake
Copy link

binhake commented Sep 14, 2022

awesome, I did it

@sdshan8
Copy link

sdshan8 commented Sep 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

You need to resize the harddrive file with qemu-img then resize the partition in the vm with something like fdisk

@McJoe21
Copy link

McJoe21 commented Oct 9, 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?

Copy to termux folder

@carlinhoshk
Copy link

carlinhoshk commented Oct 11, 2022

image

i have this error

@ElaXan
Copy link

ElaXan commented Oct 16, 2022

Awesome buddy 😉

@egandro
Copy link

egandro commented Oct 24, 2022

How to access storage?

You need to read the documentation of qemu (Really - this is just non working sample code and I won't read the documentation for you...)

qemu-system-x86_64 \
    -net user,smb=/absolute/path/to/folder \
   ...

Inside of qemu, mount via samba. Good luck.

@subhashisbhowmik
Copy link

subhashisbhowmik commented Oct 29, 2022

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

Any idea on this?

Facing the same issue here. Can someone help?

@subhashisbhowmik
Copy link

subhashisbhowmik commented Oct 30, 2022

I'm getting an illegal instruction error when booting up. I tried @bdwandry's img file as well (removed the cdrom option/tried his .sh script), both result in the same (weirdly, if my qemu command is from a script, the shell doesn't echo back the stdin anymore after the error, I have to reset the shell to fix it, doesn't happen if I type out the qemu command). I am suspecting that this has something to do with the pflash file. Tried installing ovmf, tried upgrading packages, reinstalling qemu. Nothing worked so far. Any help would be appreciated.

Device: S22 Ultra Snapdragon
Android 13 (5.10.81-android12-9-25281987-abS908EXXU2BVJA)
Termux v0.118.0 Github
Supported ABIs: arm64-v8a, armeabi-v7a, armeabi

@vjburic1
Copy link

vjburic1 commented Nov 7, 2022

I followed instructions but I had no internet. The command for creating network interfaces would give me errors.

Then I tried ifup eth0 and got the following message: ifup: can't open '/etc/network/interfaces': No such file or directory

That meant the file didn't exits! Simple fix: just enter touch /etc/network/interfaces

This creates the neccessary file and the command to create interfaces works and ifup eth0 now initializes it!

@Saikatsaha1996
Copy link

Saikatsaha1996 commented Nov 7, 2022

I followed instructions but I had no internet. The command for creating network interfaces would give me errors.

Then I tried ifup eth0 and got the following message: ifup: can't open '/etc/network/interfaces': No such file or directory

That meant the file didn't exits! Simple fix: just enter touch /etc/network/interfaces

This creates the neccessary file and the command to create interfaces works and ifup eth0 now initializes it!

Everyone use this one

https://github.com/sylirre/vmConsole

Very lite and virtual machine

@YashIndane
Copy link

YashIndane commented Nov 11, 2022

# service start docker

Above command works fine and docker daemon also runs, but if I run any docker commands I get Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Also did this -

# /usr/bin/dockerd

It throws error -

WARN[2022-11-11T18:53:34.552116680Z] grpc: addrConn.createTransport failed to connect to {unix:///var/run/docker/containerd/containerd.sock 0 }. Err :connection error: desc = "transport: error while dialing: dial unix:///var/run/docker/containerd/containerd.sock: timeout". Reconnecting... module=grpc

ANY HELP WOULD BE APPRECIATED!

@Saikatsaha1996
Copy link

Saikatsaha1996 commented Nov 11, 2022

# service start docker

Above command works fine and docker daemon also runs, but if I run any docker commands I get Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Also did this -

# /usr/bin/dockerd

It throws error -

WARN[2022-11-11T18:53:34.552116680Z] grpc: addrConn.createTransport failed to connect to {unix:///var/run/docker/containerd/containerd.sock 0 }. Err :connection error: desc = "transport: error while dialing: dial unix:///var/run/docker/containerd/containerd.sock: timeout". Reconnecting... module=grpc

ANY HELP WOULD BE APPRECIATED!

Use this one
https://github.com/sylirre/vmConsole

@YashIndane
Copy link

YashIndane commented Nov 12, 2022

Use this one https://github.com/sylirre/vmConsole

@Saikatsaha1996 Thanks, this works, but In this the VM is not getting ip address like 192.168.x.x so that I can ssh. How to do that?

@Saikatsaha1996
Copy link

Saikatsaha1996 commented Nov 12, 2022

Use this one https://github.com/sylirre/vmConsole

@Saikatsaha1996 Thanks, this works, but In this the VM is not getting ip address like 192.168.x.x so that I can ssh. How to do that?

Please ask here https://github.com/sylirre/vmConsole/issues

@fran6120
Copy link

fran6120 commented Nov 17, 2022

It works fine on a OnePlus 5 with Termux 0.118.0 (118) F-Droid, but it takes a long time to start and the containers feel very slow.

Welcome to Termux!

Community forum: https://termux.com/community
Gitter chat:     https://gitter.im/termux/termux
IRC channel:     #termux on libera.chat

Working with packages:

 * Search packages:   pkg search <query>
 * Install a package: pkg install <package>
 * Upgrade packages:  pkg upgrade

Subscribing to additional repositories:

 * Root:     pkg install root-repo
 * X11:      pkg install x11-repo

Report issues at https://termux.com/issues

~ $ cd alpine
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::8123-:8123,hostfwd=tcp::9000-:9000,hostfwd=tcp::6052-:6052,hostfwd=tcp::49154-:49154,hostfwd=tcp::61208-:61208,hostfwd=tcp::3218-:3218 \
  -device virtio-net,netdev=n1 \
  -nographic alpine_2.qcow2
BdsDxe: failed to load Boot0001 "UEFI QEMU DVD-ROM QM00005 " from PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x2,0xFFFF,0x0): Not Found
BdsDxe: loading Boot0002 "UEFI QEMU HARDDISK QM00001 " from PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x0,0xFFFF,0x0)
BdsDxe: starting Boot0002 "UEFI QEMU HARDDISK QM00001 " from PciRoot(0x0)/Pci(0x1F,0x2)/Sata(0x0,0xFFFF,0x0)
Welcome to GRUB!


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

alpineop5 login: root
Password:
Welcome to Alpine!

The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org/>.

You can setup the system with the command: setup-alpine

You may change this message by editing /etc/motd.

alpineop5:~# docker ps
CONTAINER ID   IMAGE                                          COMMAND                  CREATED          STATUS                             PORTS                                                           NAMES
b302dadea6a2   ghcr.io/home-assistant/home-assistant:latest   "/init"                  44 minutes ago   Up 2 minutes                                                                                       homeassistant
e59c38ef7d00   causticlab/hass-configurator-docker:latest     "/app/run.sh"            51 minutes ago   Up 13 seconds                      0.0.0.0:3218->3218/tcp, :::3218->3218/tcp                       configurador-hass-configurator-1
a37544f8da52   esphome/esphome                                "/entrypoint.sh dash…"   5 hours ago      Up 2 minutes                                                                                       esphome
b7ea2ae49547   filebrowser/filebrowser:latest                 "/filebrowser --noau…"   5 hours ago      Up 13 seconds (health: starting)   0.0.0.0:49154->80/tcp, :::49154->80/tcp                         archivos-filebrowser-1
645d8e7d288e   portainer/portainer-ce:latest                  "/portainer"             5 hours ago      Up 58 seconds                      8000/tcp, 9443/tcp, 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp   portainer
alpineop5:~#

@Itsfadixx
Copy link

Itsfadixx commented Nov 19, 2022

same

@szcharlesji
Copy link

szcharlesji commented Nov 19, 2022

It works for me on OnePlus 5. Is there a way to log into the alpine vm without restarting it each time? Thanks!

@fran6120
Copy link

fran6120 commented Nov 19, 2022

It works for me on OnePlus 5. Is there a way to log into the alpine vm without restarting it each time? Thanks!

If you only want to run docker and not use android apps the best solution is move from android to postmarketos (Alpine Linux distro) and run docker natively.

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