RustBox
A Docker-like container runtime written in Rust with daemon architecture, supporting multi-container orchestration, persistent state management, and comprehensive CLI commands.
Overview
RustBox is a container runtime that isn’t competing with (Docker or Kubernetes), we return to the core and build a simplest “Sandbox/isolated runtime environment” from the lowest level Linux kernel mechanisms (namespaces, cgroups, OverlayFS, etc.), provides Docker-like functionality using:
- Daemon Architecture with Unix domain socket communication
- Multi-container Management with persistent state
- OverlayFS for isolated container filesystems
- Cgroups v2 for resource limits (memory, CPU)
- Linux namespaces for complete process isolation
- Comprehensive CLI with run, stop, list, inspect, remove, logs, and attach commands
This tool is designed for container orchestration, testing environments, and secure code execution.
Architecture
Daemon-Client Model
1 | ┌──────────────────────────────────────────────────────────────────────────────┐ |
Container Isolation
RustBox employs a double fork pattern for each container to ensure proper isolation:
Process Hierarchy (Per Container)
1 | [Daemon Process] |
Container Lifecycle States
1 | Created ──(start)──> Running ──(exit)──────> Exited |
Persistent State Management
- Container metadata:
/var/lib/rustbox/containers/<container-id>.json - Container logs:
/var/lib/rustbox/logs/<container-id>/ - Overlay filesystems:
/var/lib/rustbox/overlay/<container-id>/ - State recovery: Daemon recovers container state on restart
Features
- Daemon Architecture with background process and client-server communication
- Multi-container Management supporting concurrent container execution
- Persistent State Management with automatic recovery across daemon restarts
- Complete Container Lifecycle (create, start, stop, remove, inspect)
- Interactive Attach Support with TTY allocation and real-time I/O streaming
- Real-time Logging with per-container stdout/stderr files
- Resource Isolation using cgroups v2 (memory, CPU limits)
- Filesystem Isolation using overlayfs with automatic cleanup
- Full Namespace Isolation (PID, UTS, NET, USER, IPC)
- Docker-like CLI with familiar commands (run, ps, logs, inspect, rm, attach)
- Graceful Shutdown with proper signal handling and resource cleanup
- Security with proper privilege separation and input validation
Requirements
- Linux kernel 5.x or higher (with overlayfs and cgroups v2 support)
- Rust (1.70+ recommended)
- Root privileges (for daemon operations, mounting, and namespace creation)
Installation
Build from Source
1 | git clone https://github.com/isdaniel/RustBox.git |
Binaries
After building, you’ll have two binaries:
rustbox- Client CLI tooldaemon_rs- Background daemon process
Usage
Start the Daemon
1 | # Start the daemon in background (requires root) |
The daemon will:
- Listen on Unix socket
/tmp/rustbox-daemon.sock - Create system directories under
/var/lib/rustbox/ - Recover existing container state from disk
- Handle graceful shutdown on SIGTERM/SIGINT
Container Management
Create and Run Containers
1 | # Run a container in background with TTY support (allows interactive attach) |
Note: The --tty flag is required if you want to attach to the container later.
List Containers
1 | # List running containers |
Attach to Running Containers
1 | # Attach to a running container (container must have been created with --tty flag) |
Interactive Controls:
- Press
Ctrl+Pfollowed byCtrl+Qto detach from container (leaves it running) - Press
Ctrl+Cto send interrupt signal and exit
Requirements:
- Container must have been started with
--ttyflag - Container must be in
Runningstate
Stop, Remove, and Inspect Containers
1 | # Stop a running container |
Available Options:
--name- Custom container name (auto-generated if not provided)--memory- Memory limit (e.g., “256M”, “1G”, “512000”)--cpu- CPU limit as fraction of one core (e.g., “0.5”, “1.0”)--workdir- Working directory inside container (default: “/“)--rootfs- Path to rootfs directory (default: “./rootfs”)--tty- Allocate a pseudo-TTY for interactive use (required for attach)
Directory Structure
Runtime Directories (created by daemon)
1 | /var/lib/rustbox/ |
Source Code Structure
1 | src/ |
Technical Details
IPC Protocol
Communication between client and daemon uses length-prefixed JSON messages over Unix domain sockets:
1 | [4-byte length (u32, big-endian)][JSON payload] |
Example:
1 | 0x0000001E {"type":"ListRequest","all":true} |
Container ID Format
- 12-character hexadecimal identifiers (e.g.,
a1b2c3d4e5f6) - Auto-generated names follow
adjective-nounpattern (e.g.,happy-elephant) - CLI commands accept either ID or name
Resource Limits
- Memory: Supports units like
100M,1G,512000(bytes) - CPU: Fraction of one core, e.g.,
0.5for 50% CPU limit - Enforced via cgroups v2 at
/sys/fs/cgroup/rustbox/<container-id>/
Security Model
- Daemon runs as root for privileged operations
- Client commands run as user, connect via Unix socket
- Containers run in isolated namespaces (PID, NET, UTS, IPC, USER)
- Input validation prevents directory traversal and injection attack
Final Thoughts
RustBox is not a full container system, and that’s by design — it’s transparent, hackable, and educational. Whether you’re looking to secure untrusted code, explore low-level Linux features, or just love writing systems code in Rust, RustBox is a fantastic playground.
Give it a ⭐ on GitHub and explore the source!
__此文作者__:Daniel Shih(石頭)
__此文地址__: https://isdaniel.github.io/rustbox-introduce/
__版權聲明__:本博客所有文章除特別聲明外,均採用 CC BY-NC-SA 3.0 TW 許可協議。轉載請註明出處!