dkr
is a minimalistic container runtime written in Rust, demonstrating core containerization concepts using Linux namespaces and system calls. It's intended for educational purposes, showcasing how containers work at a low level. This is not a production-ready solution.
- UTS Namespace Isolation: Provides a separate hostname for the container (
nix::unistd::sethostname
). - PID Namespace Isolation: Creates an isolated process tree for the container (
nix::sched::unshare(CloneFlags::CLONE_NEWPID)
). - Root Filesystem Change: Uses
chroot
to change the container's root directory to/alpine-root
. You must create and populate this directory beforehand (see below). - Command Execution: Executes a user-provided command within the container, inheriting stdin, stdout, and stderr.
- Basic Error Handling: Provides informative error messages.
-
Rust: Install Rust: https://www.rust-lang.org/tools/install.
-
Root Privileges: Running
dkr
requires root privileges (or appropriate capabilities) for namespace creation (usesudo
). -
Alpine Root Filesystem (
/alpine-root
): Create and populate this directory with a minimal Linux filesystem:sudo mkdir /alpine-root sudo docker create --name alpine-temp alpine sudo docker export alpine-temp | sudo tar -xf - -C /alpine-root sudo docker rm alpine-temp
-
direnv (optional): Install
direnv
and rundirenv allow
in the project directory (for.envrc
).
git clone <repository_url> # Replace with your repository URL
cd rust-docker
cargo build
sudo ./target/debug/dkr /bin/sh
This creates a container, sets its hostname to "container", changes its root to /alpine-root
, and executes /bin/sh
. You'll get a shell prompt (#
). Available commands depend on what's in /alpine-root
.
Example:
# (After running the above command)
# hostname
container
# ps # (May not be available)
# exit
- No Network Isolation: The container shares the host's network.
- No User Namespace Isolation: The container's root user is the host's root user (though
chroot
provides some isolation). - No Mount Namespace Isolation (beyond
chroot
): Limited filesystem isolation. - No Resource Limits (cgroups): No CPU, memory, or other resource limits.
- Minimal Filesystem: You must manually create
/alpine-root
. - Security: This is a demonstration and is not secure for production use.
- Basic Signal Handling.
println!
Restriction: Useswrite_stdout
instead ofprintln!
in the child process for safety afterfork
.
- Add network namespaces and virtual network interfaces.
- Implement mount and user namespaces.
- Use cgroups for resource limiting.
- Explore image management.
This project provides hands-on experience with the core components of containers. Experiment and learn!