Leanpub Header

Skip to main content

Building Containers from Scratch in Rust

Internals, Linux Kernel Primitives and Container Runtime Engineering

Building Containers from Scratch in Rust
This book is 100% completeLast updated on 2026-10-29

What really happens when a container starts? Go beyond Docker and explore the Linux kernel mechanisms that make containers work. Using Rust, you’ll build a container runtime from scratch, learning how namespaces, cgroups and other kernel primitives fit together to create a working system you can run, test and inspect yourself.

Minimum price

$25.00

$35.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
248
Pages
About

About

About the Book

This book teaches you how to build a functional container runtime from the ground up using Rust. You will learn how Linux namespaces, cgroups and kernel subsystems actually work under the hood, how they combine to create the isolation and resource control that containers rely on and how to wire it all together into a working system. Every chapter builds on the last, moving from kernel-level concepts through subsystem implementation to a complete, end-to-end runtime that you can run and inspect yourself.

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience spanning software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. His work focuses on understanding complex systems, uncovering security weaknesses and developing practical solutions that make technology more secure, reliable and effective.

Through Steve Publications, he collaborates with experienced researchers, engineers and industry experts to create practical, technically rigorous books on various topics not limited to self-improvement, software development, cybersecurity, cloud computing, DevOps, artificial intelligence and enterprise technologies. His mission is to make complex technical subjects accessible through clear explanations, real-world examples and hands-on exercises that help IT professionals, students and technology enthusiasts build valuable, practical skills.

Steve Publications embraces AI as a supporting tool for translation, proofreading and editorial refinement, not as a substitute for original research, technical expertise, or hands-on experience. Some contributors choose to publish under pseudonyms to protect their privacy or maintain professional boundaries.

Behind every book is a commitment to technical accuracy, practical relevance and continuous improvement. Our publications are written, reviewed and maintained by experienced professionals, with support from a private community of more than 420 engineers and researchers. Rather than prioritizing volume or mass-produced AI-generated content, we focus on delivering reliable, useful resources that readers can apply in real-world environments.

We also welcome experienced professionals who want to share their knowledge by writing or reviewing books, particularly in specialized and emerging technology fields. If you have expertise to contribute, we invite you to get in touch and explore opportunities to collaborate.

Our commitment is simple: To deliver books that respect our readers' time, support their professional growth and provide lasting practical value. If a book does not meet your expectations, Leanpub offers a 60-day money-back guarantee.

Contents

Table of Contents

Internals, Linux Kernel Primitives and Container Runtime Engineering

Introduction

Chapter 1: What Are Containers Really

  1. The Process as the Fundamental Unit
  2. Chroot and the Birth of Isolation Ideas
  3. From Zones to Linux Containers to Docker
  4. How a Container Runtime Actually Works
  5. Containers vs Virtual Machines vs Sandboxes
  6. What This Book Will Build

Chapter 2: Rust for Systems Programming

  1. Why Rust for Container Runtimes
  2. Tooling: Cargo, rustup, Cross-compilation, musl
  3. Error Handling Patterns for Systems Code
  4. The unsafe Boundary: When and How
  5. Linux FFI with libc and bindgen
  6. Async vs Sync in Runtime Code
  7. Structuring a Systems Library in Rust

Chapter 3: Linux Kernel Prerequisites

  1. Minimum and Recommended Kernel Versions
  2. Required Kernel Configuration Options
  3. Namespace Support and Detection
  4. cgroup v1 vs v2: Distribution Defaults
  5. Essential Tooling: strace, nsenter, lsns, bpftrace
  6. Building and Testing on Different Distributions

Chapter 4: Linux Namespaces Overview

  1. The Namespace Abstraction in the Kernel
  2. How Namespaces Are Structured in Kernel Memory
  3. The clone and unshare System Calls
  4. Joining and Leaving Namespaces with setns
  5. Inspecting Namespaces: /proc, nsenter, lsns
  6. Namespace Hierarchy and Inheritance

Chapter 5: PID Namespaces and Process Isolation

  1. How clone Works: Flags, Stack and Child Setup
  2. fork, vfork, execve: The Process Creation Family
  3. Nested PID Namespaces and PID Translation
  4. PID 1: Signal Handling, Zombie Reaping, init Responsibilities
  5. Signals Across Namespace Boundaries
  6. Process Tracing and ptrace Interaction
  7. Implementing Our First PID Namespace Container

Chapter 6: Mount Namespaces and Filesystem Isolation

  1. The Mount Namespace Data Structures
  2. chroot: History, Limitations and Security Gaps
  3. pivot_root: How It Actually Works
  4. Bind Mounts and Recursive Mounts
  5. Mount Propagation: Shared, Private, Slave, Unbindable
  6. Creating a Minimal Container Root Filesystem
  7. /proc, /sys and Pseudo-filesystem Mounting
  8. Integrating Mount Namespaces into Our Runtime

Chapter 7: User Namespaces and Privilege

  1. The User Namespace Concept
  2. UID and GID Mapping Mechanics
  3. /proc/self/uid_map, gid_map and setgroups
  4. Root Inside, Nobody Outside
  5. The no_new_privs Flag and Privilege Escalation
  6. Rootless Container Limitations and Workarounds
  7. Multi-Level User Namespaces
  8. Rust Implementation of User Namespace Setup

Chapter 8: Network Namespaces and Container Networking

  1. Network Namespace Isolation
  2. Virtual Ethernet (veth) Pairs
  3. Linux Bridges and Layer 2 Forwarding
  4. IP Addressing and Routing Inside Containers
  5. NAT and Masquerading with iptables/nftables
  6. DNS Resolution Configuration
  7. Loopback Interface Requirements
  8. Common Networking Topologies
  9. Rust Implementation of Container Networking

Chapter 9: Completing the Namespace Set

  1. IPC Namespaces: System V and POSIX IPC
  2. UTS Namespace: Hostname and Domainname Isolation
  3. Time Namespaces (kernel 5.6+): Clock Manipulation
  4. cgroup Namespaces
  5. When Each Namespace Is Necessary
  6. Namespace Setup Order and Interactions
  7. Rust Implementation of Full Namespace Setup

Chapter 10: Control Groups: Architecture and Evolution

  1. The Control Group Concept
  2. cgroup v1: Hierarchies, Controllers and Complexity
  3. Why cgroup v2 Was Designed
  4. The Unified Hierarchy
  5. Controller Types and Delegation
  6. Distribution Defaults and Detection

Chapter 11: cgroup v2 Deep Dive

  1. Hierarchy Operations: Creating, Moving, Cleaning Up
  2. Process Placement in cgroups
  3. CPU Controller: weights, quotas, periods, scheduling
  4. Memory Controller: limits, accounting, high thresholds
  5. Swap Control and Memory+Swap Limits
  6. OOM Killer Behavior and oom.kill_disable
  7. I/O Controller: throttling, weights, cgroup2 io.stat
  8. PID Controller: limiting process counts
  9. Pressure Stall Information (PSI)
  10. Device Controller: blocking and allowing devices
  11. Rust Implementation of cgroup v2 Setup

Chapter 12: Linux Capabilities

  1. The Problem with UID 0
  2. Capability Sets: Permitted, Effective, Inheritable, etc.
  3. Capability Inheritance Across execve and clone
  4. prctl Controls: no_new_privs, dumpable, etc.
  5. Ambient Capabilities (kernel 4.3+)
  6. Common Capabilities Used in Containers
  7. Implementing Capability Dropping

Chapter 13: Mandatory Access Control and System Call Filtering

  1. System Call Filtering with seccomp
  2. seccomp Modes: Strict, Filter, BPF
  3. Writing seccomp Filters in Rust
  4. The LSM Framework Architecture
  5. AppArmor vs SELinux for Containers
  6. Integrating MAC Policies into Our Runtime
  7. seccomp Default Profiles for Containers

Chapter 14: Container Filesystems and OverlayFS

  1. The Layered Image Concept
  2. OverlayFS: How Copy-on-Write Works
  3. Upperdir, Lowerdir, Workdir
  4. Whiteouts and Opaque Directories
  5. File Ownership and UID/GID in Images
  6. Mounting an OverlayFS Root
  7. Performance Characteristics and Limitations
  8. Rust OverlayFS Integration

Chapter 15: Container Images, Manifests and Rootfs Assembly

  1. Docker Image Format History
  2. OCI Image Specification
  3. Image Manifests and Config Blobs
  4. Content-Addressable Storage
  5. Image Extraction and Rootfs Assembly
  6. Building a Minimal Image from Scratch
  7. Integration with Our Runtime

Chapter 16: Project Architecture and Design

  1. Runtime Requirements and Goals
  2. Project Structure and Cargo Configuration
  3. The Container Config Data Model
  4. Component Responsibilities
  5. Communication Between Parent and Child
  6. Error Handling and Failure Recovery

Chapter 17: Implementing Namespace Setup

  1. Namespace Config and Validation
  2. unshare Implementation in Rust
  3. setns and Namespace File Descriptors
  4. Ordering of Namespace Operations
  5. The Initial Process Entry Point
  6. Testing Namespace Setup

Chapter 18: Implementing Filesystem Configuration

  1. Mount Specification Parsing
  2. pivot_root Implementation
  3. Bind Mount Creation
  4. Pseudo-filesystem Mounts
  5. Mount Propagation Configuration
  6. Cleanup on Failure

Chapter 19: Implementing Resource Control

  1. cgroup v2 Setup: Hierarchy Creation and Process Placement
  2. Controller Configuration from Spec
  3. Capability Dropping Implementation
  4. seccomp Filter Construction
  5. Integration into the Startup Sequence

Chapter 20: Process Lifecycle Management

  1. The Parent-Child Handshake Protocol
  2. clone/fork + execve Sequence
  3. File Descriptor Management
  4. Signal Forwarding from Host to Container
  5. Process State Monitoring
  6. Container Shutdown and Cleanup
  7. Zombie Handling and PID 1 Interaction

Chapter 21: Networking Integration

  1. Network Config Specification
  2. veth Pair Creation
  3. Bridge Attachment and Routing
  4. IP Address and DNS Configuration
  5. Integration with Namespace Setup
  6. Rootless Networking Options
  7. DNS Configuration

Chapter 22: Rootless Containers

  1. What Rootless Really Means
  2. Required User Namespace Configuration
  3. Rootless cgroup Delegation
  4. Rootless Mount Namespaces and Privileged Mounts
  5. Rootless Networking Limitations
  6. Testing Rootless Execution

Chapter 23: OCI Runtime Specification Compliance

  1. OCI Runtime Spec Overview
  2. Bundle Layout and config.json
  3. Where We Follow the Spec
  4. Where We Simplify
  5. Comparison with runc Architecture
  6. Comparison with crun Implementation Choices
  7. Path to Full Spec Compliance

Chapter 24: Security Analysis and Threat Modeling

  1. The Container Security Promise and Its Limits
  2. Threat Model: What Are We Defending Against
  3. Namespace Escape Vulnerabilities
  4. cgroup and Capability Abuse
  5. seccomp and Kernel Exploits
  6. Common Misconfigurations That Break Isolation
  7. Defense-in-Depth Implementation
  8. Safe Testing of Container Isolation

Chapter 25: Debugging, Observability and Production Readiness

  1. Debugging with strace: Practical Examples
  2. eBPF and bpftrace for Container Observability
  3. Understanding Kernel Logs and dmesg
  4. Performance Profiling with perf
  5. Common Failure Modes and How to Fix Them
  6. Hardening for Production Use
  7. What We Did Not Implement (and Why)

Conclusion

References

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub