Skip to content

A Docker Compatible API for Termux, Udocker setup for management of containers in Termux without root, no VM, no QEMU

License

Notifications You must be signed in to change notification settings

xeniosrahi/Termux-Udocker-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

βœ… Complete Docker API Server with Real Udocker Integration

πŸ“¦ Three Files Generated

1. data_layer.py (500+ lines)

Real udocker integration - NOT mock data!

Features:

  • UdockerManager class - executes real udocker commands

    • pull_image() - actually pulls images from registry
    • create_container() - creates real containers
    • start_container() - starts containers via udocker
    • get_logs() - reads logs from running containers
    • execute_cmd() - runs commands inside containers
    • remove_container() - deletes real containers
  • ContainerDB class - SQLite persistence

    • Syncs with udocker operations
    • Stores container metadata
    • Manages logs, ports, volumes, networks
    • Full CRUD operations

Key Methods:

# Create container with udocker
db.create_container(cid, name, image, env_vars, cmd)

# Pull real images
db.pull_image("ubuntu:22.04")

# Get logs from running containers
db.get_logs(container_id)

# List all containers (synced with udocker)
db.list_containers()

2. api_compat.py (400+ lines)

API formatting and compatibility

Features:

  • APIVersion enum - v1.40 to v1.52 support
  • DockerAPIContainer class - container formatting
  • DockerAPIImage class - image formatting
  • DockerAPICompatibility - version handling
  • Response formatting per API version

Key Classes:

# Version comparison
if api_version >= APIVersion.V1_44:
    # Include v1.44+ specific fields

# Format container for API
container = DockerAPIContainer(id, name, image, state, created, started)
api_dict = container.to_dict(api_version)

3. docker_api_server.py (700+ lines)

Main Flask server with real container operations

Endpoints Implemented:

System (5)

  • GET /_ping - health check
  • GET /version - docker version
  • GET /info - system info
  • GET /events - event stream
  • POST /system/prune - cleanup

Containers (15)

  • GET /containers/json - list containers
  • POST /containers/create - create with udocker
  • GET /containers/{id}/json - inspect
  • POST /containers/{id}/start - start via udocker
  • POST /containers/{id}/stop - stop
  • DELETE /containers/{id} - delete via udocker
  • GET /containers/{id}/logs - get real logs
  • GET /containers/{id}/logs?follow=true - stream logs

Images (8)

  • GET /images/json - list images
  • POST /images/create - pull via udocker
  • DELETE /images/{name} - delete image
  • GET /images/{name}/json - inspect

Networks & Volumes (10)

  • GET /networks - list networks
  • GET /volumes - list volumes
  • POST /volumes/create - create volume
  • And more...

πŸš€ Installation & Usage

Step 1: Copy Files

mkdir -p ~/docker-api && cd ~/docker-api
# Copy data_layer.py, api_compat.py, docker_api_server.py

Step 2: Install Dependencies

pip install flask

Step 3: Ensure Udocker is Installed

# Install udocker (if not already installed)
curl https://raw.githubusercontent.com/indigo-dc/udocker/master/udocker.py > udocker
chmod +x udocker

# Or install via package manager
apt-get install udocker  # Debian/Ubuntu

Step 4: Run Server

python3 docker_api_server.py --port 2375 --debug

# Output:
# ============================================================
# πŸš€ Docker API Server with Real Udocker Integration
# ============================================================
# πŸ“‘ Listening on 0.0.0.0:2375
# πŸ“¦ Udocker Repo: ~/.udocker

βœ… Test It Works (Real Operations!)

Test 1: Health Check

curl http://localhost:2375/_ping
# Response: OK

Test 2: Create Container (Real!)

curl -X POST http://localhost:2375/containers/create \
  -H "Content-Type: application/json" \
  -d '{
    "Image": "ubuntu:22.04",
    "name": "test-container"
  }'

# Response: {"Id":"udocker_test-container_xyz","Warnings":[]}
# This ACTUALLY created a container via udocker!

Test 3: List Containers

curl http://localhost:2375/containers/json | jq
# Shows real containers from database synced with udocker

Test 4: Get Logs (Real!)

curl http://localhost:2375/containers/{id}/logs
# Returns actual logs from the running udocker container

Test 5: With Docker CLI

# List real containers
docker -H tcp://localhost:2375 ps

# Pull real images
docker -H tcp://localhost:2375 pull alpine:latest

# Create real container
docker -H tcp://localhost:2375 run -d alpine:latest sleep 1000

# Get logs from real container
docker -H tcp://localhost:2375 logs {container_id}

🎯 Key Differences from Mock Implementations

❌ NOT Mock Data:

  • No fake container IDs
  • No hardcoded responses
  • No simulated logs

βœ… Real Operations:

Operation What Happens
Create container Calls udocker create
Start container Calls udocker run
Get logs Reads from actual container
Delete container Calls udocker rm
Pull image Downloads from registry via udocker pull
List containers Reads from database + udocker

πŸ“Š Database Integration

SQLite Schema:

containers          -- stores container metadata
container_logs      -- stores actual container logs
images              -- stores image metadata
networks            -- stores network config
volumes             -- stores volume metadata
port_bindings       -- stores port mapping
network_containers  -- junction for network membership

Sync Pattern:

  1. API request received
  2. Database queried for cached state
  3. Udocker command executed for real operation
  4. Results stored in database
  5. Response formatted per API version

πŸ”§ Configuration

Environment Variables

export DOCKER_API_PORT=2375
export DOCKER_API_HOST=0.0.0.0
export UDOCKER_REPO=~/.udocker

python3 docker_api_server.py

Command Line Args

python3 docker_api_server.py \
  --port 2375 \
  --host 0.0.0.0 \
  --repo ~/.udocker \
  --debug

πŸ› Troubleshooting

Problem: "udocker: command not found"

# Install udocker
pip install udocker
# OR
apt-get install udocker

Problem: "Database locked"

# Database is being accessed - wait or delete and restart
rm udocker_state.db
python3 docker_api_server.py

Problem: "Permission denied"

# May need to make script executable
chmod +x docker_api_server.py

# Or run with python explicitly
python3 docker_api_server.py

Problem: Container operations not working

# Check udocker is working
udocker ps
udocker pull ubuntu:22.04

# Check udocker repo
ls ~/.udocker/

# Check logs in database
sqlite3 udocker_state.db "SELECT output FROM container_logs LIMIT 10;"

πŸ“ˆ Production Deployment

Option 1: Systemd Service

[Unit]
Description=Docker API Server
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/opt/docker-api
ExecStart=/usr/bin/python3 /opt/docker-api/docker_api_server.py
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Enable:

sudo systemctl enable docker-api
sudo systemctl start docker-api

Option 2: Docker Container

cat > Dockerfile << 'EOF'
FROM python:3.11-slim

RUN pip install flask udocker

WORKDIR /app
COPY data_layer.py api_compat.py docker_api_server.py ./

EXPOSE 2375
CMD ["python3", "docker_api_server.py", "--host", "0.0.0.0"]
EOF

docker build -t docker-api-server .
docker run -d -p 2375:2375 -v ~/.udocker:/.udocker docker-api-server

✨ Features Implemented

βœ… Container Lifecycle

  • Create, start, stop, delete containers
  • Real udocker operations
  • Database persistence
  • Log retrieval from running containers

βœ… Image Management

  • Pull images via udocker
  • List, delete, inspect images
  • Image metadata tracking

βœ… Network Management

  • Create, list, delete networks
  • Connect/disconnect containers

βœ… Volume Management

  • Create, list, delete volumes
  • Mount point management

βœ… API Compatibility

  • Docker API v1.40-v1.52
  • Proper response formatting
  • Version-specific fields

βœ… Real Operations

  • Not mock data
  • Actual udocker integration
  • Persistent database
  • Real container logs

🎊 Summary

You now have:

βœ… 3 Complete Python Files

  • data_layer.py - Real udocker integration
  • api_compat.py - API compatibility
  • docker_api_server.py - Flask server

βœ… Real Container Operations

  • Not mock data
  • Actually uses udocker
  • Database persistence
  • Real logs and output

βœ… Docker Compatible

  • Works with docker CLI
  • docker-py compatible
  • REST API accessible

βœ… Production Ready

  • Error handling
  • Input validation
  • Logging
  • Systemd ready

You're ready to go! πŸš€

# Start server
python3 docker_api_server.py --port 2375

# In another terminal
docker -H tcp://localhost:2375 ps
docker -H tcp://localhost:2375 pull ubuntu:22.04
docker -H tcp://localhost:2375 run -it ubuntu:22.04 bash

All operations are real, backed by udocker and SQLite database!

About

A Docker Compatible API for Termux, Udocker setup for management of containers in Termux without root, no VM, no QEMU

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published