Minimal container runtime from scratch. Namespaces, cgroups v2, overlayfs — educational but functional. Logs its own decay and self-destructs gracefully.
Language: Nim
Constraint: Make something that dies
Stack: pure Nim (posix wrappers, linux headers)
- Phase 1: Namespace isolation (
clone,unshare) —pid,net,mount,uts,ipc,user - Phase 2: Root filesystem setup (
pivot_root,overlayfs) - Phase 3: cgroups v2 resource limiting (
cpu,memory,pids) - Phase 4: Process lifecycle management (init, reap)
- Phase 5: Self-monitoring (memory fragmentation, inode exhaustion)
- Phase 6: Graceful self-destruction at resource limits
- Phase 7: CLI (
run,exec,kill,ps) - Phase 8: OCI runtime spec compatibility (partial)
- Nim >= 2.0.0
- Linux (kernel 5.x+ for full cgroups v2 support)
- Root or
CAP_SYS_ADMINfor namespace operations
# Compile the runtime
nimble build
# Run tests
nimble testCreate an isolated process with its own namespaces:
sudo ./ferrite run -- /bin/shThis creates a new process in separate pid, net, mount, uts, ipc, and user namespaces.
Run a command inside an isolated root filesystem using overlayfs:
# Provide a directory as the base image layer
sudo ./ferrite run --root /var/lib/ferrite/images/alpine -- /bin/shferrite will:
- Mount an overlayfs with the provided directory as the read-only lower layer
pivot_rootinto the merged view- Execute the command
The overlay layout is created automatically under /tmp/ferrite-<pid>/.
Limit CPU, memory, and process count for containers:
# Limit CPU to 50% and memory to 128 MiB
sudo ./ferrite run --cpu 50 --mem 134217728 -- /bin/sh
# Combined with rootfs
sudo ./ferrite run --root /var/lib/ferrite/images/alpine \
--cpu 25 --mem 67108864 --pids 100 -- /bin/shWhen resource limits are specified, ferrite:
- Creates a cgroup under
/sys/fs/cgroup/ferrite-<pid>-<timestamp>/ - Writes the requested limits (
cpu.max,memory.max,pids.max) - Moves the container process into the cgroup
- Cleans up the cgroup after the container exits
ferrite now supports the full container lifecycle:
# Run a new container
sudo ./ferrite run --root /var/lib/ferrite/images/alpine -- /bin/sh
# Execute a command in a running container
sudo ./ferrite exec ferrite-1234 -- /bin/hostname
# Send a signal to a container
sudo ./ferrite kill ferrite-1234
sudo ./ferrite kill -s SIGKILL ferrite-1234
# List running containers
sudo ./ferrite psContainers are tracked in /run/ferrite/ (or /tmp/ferrite/ as fallback) with JSON state files. Container IDs can be specified with --id or auto-generated. The exec command uses setns(2) to enter the target container's namespaces.
ferrite now supports partial OCI runtime spec compatibility:
# Create a container from an OCI bundle
sudo ./ferrite create mycontainer --bundle /path/to/bundle
# Show OCI state
sudo ./ferrite state mycontainer
# Start the container
sudo ./ferrite start mycontainer
# Delete the container
sudo ./ferrite delete mycontainerAn OCI bundle is a directory containing config.json (OCI runtime configuration) and rootfs/ (container root filesystem). ferrite parses namespaces, resources, process args, and hostname from the OCI config.
See PLAN.md, docs/phase1.md, docs/phase2.md, and docs/phase3.md for detailed architecture decisions.
Apache-2.0, see LICENSE.
If this project saved you time, solved a problem, or just made your day a little more neon, you can fuel the next one:

