Skip to content

Instantly share code, notes, and snippets.

@rezamarzban
Last active October 3, 2025 12:21
Show Gist options
  • Select an option

  • Save rezamarzban/d2eb4817eb0314ea1a4142dc2dfd8716 to your computer and use it in GitHub Desktop.

Select an option

Save rezamarzban/d2eb4817eb0314ea1a4142dc2dfd8716 to your computer and use it in GitHub Desktop.

Installing and Using udocker in Termux

To run a small Docker image like Alpine in Termux using udocker (a user-space Docker alternative without root or a daemon), execute the following commands sequentially in your Termux shell. This setup handles installation, pulling the lightweight Alpine image (~5MB), and running a simple container. Note that you'll need to set an environment variable each session for proot support.

Step 1: Update packages and install dependencies

pkg update && pkg upgrade -y
pkg install -y python-pip proot

Step 2: Install udocker via pip

pip install udocker

Step 3: Initialize udocker

udocker --debug install --force

Step 4: Set proot executable path (run this each new Termux session)

export UDOCKER_USE_PROOT_EXECUTABLE=$(command -v proot)

Step 5: Pull the Alpine image

udocker pull alpine

This downloads the alpine:latest image to your user space (stored in ~/.udocker).

Step 6: Run the Alpine container

udocker run --rm alpine echo "Hello from Alpine container!"

This creates and runs a temporary container, printing the message. The --rm flag removes it after execution. You can replace echo "Hello from Alpine container!" with other commands, like ls or cat /etc/os-release to inspect the image.

Notes

  • udocker uses proot for emulation in Termux, so it's suitable for lightweight tasks but may have performance limitations on mobile hardware.
  • If you want to create a named container for reuse: udocker create --name=myalpine alpine followed by udocker run --rm myalpine.
  • To make the export persistent, add it to ~/.bashrc or ~/.profile: echo 'export UDOCKER_USE_PROOT_EXECUTABLE=$(command -v proot)' >> ~/.bashrc and then source ~/.bashrc.
  • For more details, refer to the udocker user manual.
@rezamarzban
Copy link
Author

Comprehensive udocker Commands for Termux (Alpine Image)

Below is a comprehensive list of practical udocker commands for managing and using the Alpine image in Termux. These assume udocker is installed, the proot executable is set (export UDOCKER_USE_PROOT_EXECUTABLE=$(command -v proot)), and the Alpine image is pulled (udocker pull alpine). All commands are run in the Termux shell. This list includes previously provided commands (corrected where needed) and additional ones for a complete reference, tailored to Alpine where applicable, based on official udocker documentation.

1. Run a temporary container

udocker run --rm alpine echo "Hello from Alpine!"

Executes a command in a temporary Alpine container and removes it after.

2. Create a named container

udocker create --name=myalpine alpine

Creates a persistent container named myalpine for reuse.

3. Run a command in a named container

udocker run myalpine ls -l /etc

Runs ls -l /etc in the myalpine container, listing files in /etc.

4. Start an interactive shell

udocker run --rm -it alpine /bin/sh

Opens an interactive shell in a temporary Alpine container (/bin/sh is Alpine’s default shell).

5. List all containers

udocker ps

Shows all created containers, including names and statuses.

6. List available images

udocker images

Displays downloaded images, including alpine:latest.

7. Inspect container details

udocker inspect myalpine

Shows configuration and metadata for the myalpine container.

8. Remove a container

udocker rm myalpine

Deletes the myalpine container.

9. Remove an image

udocker rmi alpine

Deletes the Alpine image from ~/.udocker.

10. Copy files into a container

udocker cp myfile.txt myalpine:/tmp/myfile.txt

Copies myfile.txt from the host to /tmp/myfile.txt in the myalpine container.

11. Copy files from a container

udocker cp myalpine:/etc/os-release ./os-release

Copies /etc/os-release from the myalpine container to the host’s current directory.

12. Run a container with a volume mount

udocker run --rm -v $(pwd)/local:/mnt alpine ls /mnt

Mounts the current directory’s local subdirectory as /mnt in a temporary container and lists its contents.

13. Check Alpine version

udocker run --rm alpine cat /etc/os-release

Displays the Alpine version by reading /etc/os-release.

14. Install a package in a container

udocker run --rm -it --user=root alpine /bin/sh -c "apk add nano"

Installs the nano editor in a temporary Alpine container (changes are not persistent).

15. Export a container to a tar file

udocker export -o myalpine.tar myalpine

Saves the myalpine container’s filesystem to myalpine.tar.

16. Search for images on Docker Hub

udocker search alpine

Searches Docker Hub for Alpine-related images and lists matching repositories.

17. List tags for an image

udocker search --list-tags alpine

Displays available tags (versions) for the Alpine image, like latest, 3.18, etc.

18. Pull an image for a specific platform

udocker pull --platform=linux/arm64 alpine

Pulls the Alpine image for ARM64 architecture (useful on ARM-based Android devices).

19. List images with long format

udocker images -l

Lists downloaded images in long format, showing details like image ID, creation date, and size.

20. Create a container with a custom name

udocker create --name=myalpine2 alpine

Creates a new persistent container named myalpine2 from the Alpine image.

21. Run with an empty entrypoint

udocker run --rm --entrypoint="" alpine /bin/sh -c "echo 'Custom entry' && ls /"

Overrides the default entrypoint to run a custom shell command, listing the root directory.

22. Run as a specific user

udocker run --rm --user=root alpine whoami

Runs the container as the root user and prints the current user identity.

23. List containers with details

udocker ps -m -s

Lists all containers with mount points (-m) and sizes (-s) for more verbose output.

24. Assign a name to a container ID

udocker name <container_id> mynamedalpine

Assigns the name mynamedalpine to an existing container by its ID (get ID from udocker ps).

25. Rename a container

udocker rename myalpine myalpine-renamed

Changes the name of the myalpine container to myalpine-renamed.

26. Clone a container

udocker clone myalpine

Creates a duplicate of the myalpine container with a new ID (name it afterward if needed).

27. Tag an image

udocker tag alpine mycustom:alpine-v1

Creates a new tag mycustom:alpine-v1 pointing to the existing Alpine image.

28. Import an image from a tar file

udocker import -o alpine-imported.tar alpine:imported

Imports a tar file (alpine-imported.tar) as a new image tagged alpine:imported (create the tar via export first).

29. Commit changes to a new image

udocker commit myalpine alpine:modified

Saves changes made to the myalpine container (e.g., after installing packages) as a new image alpine:modified.

30. Run with environment variables

udocker run --rm -e MY_VAR="hello" alpine env | grep MY_VAR

Sets an environment variable MY_VAR in the container and prints it using env.

31. Verify an image

udocker verify alpine

Checks the integrity of the Alpine image’s metadata and files.

32. Run with a specific working directory

udocker run --rm -w /tmp alpine pwd

Sets the working directory to /tmp and prints it, confirming the container’s current directory.

33. Update an image

udocker pull --force alpine

Forces a re-pull of the Alpine image to update it to the latest version.

34. Execute a command in an existing container

udocker exec myalpine ls /tmp

Executes ls /tmp in the running or stopped myalpine container (if it exists).

35. Save an image to a tar file

udocker save -o alpine-image.tar alpine

Exports the Alpine image (not a container) to alpine-image.tar for backup or transfer.

36. Load an image from a tar file

udocker load -i alpine-image.tar

Imports an image from alpine-image.tar into udocker’s repository.

37. Run with a specific hostname

udocker run --rm --hostenv --hostname=myhost alpine hostname

Sets the container’s hostname to myhost and prints it (--hostenv allows host environment passthrough).

38. Display udocker version

udocker version

Shows the installed udocker version.

39. Check container logs

udocker logs myalpine

Displays logs for the myalpine container (useful if commands produce output).

40. Run with a bind mount

udocker run --rm --bindhome alpine ls /root

Binds the host’s home directory to the container’s /root and lists its contents.

Notes

  • Use --rm for temporary containers to save space; omit for persistent containers unless cleaning up.
  • The -it flag enables interactive mode with a TTY (essential for shells like /bin/sh).
  • Volume mounts (-v) and bind mounts (--bindhome) allow file sharing but may have path resolution issues in proot mode; use absolute paths if errors occur.
  • Use --user=root for privileged operations like apk add in Alpine.
  • Persistent changes require committing (udocker commit) or exporting/importing.
  • Run udocker --help or consult the udocker user manual for advanced options.
  • Performance in Termux is limited by proot emulation; stick to lightweight images like Alpine.
  • For architecture-specific pulls (e.g., --platform=linux/arm64), verify your device’s architecture with uname -m.

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