๐ I'm Building My Own Container Runtime!
This is part of a complete series where I'm building Conti - a container runtime from scratch. Check it out on GitHub!
About This Series:
- I'm sharing everything I learn while building my own container runtime
- Most concepts come from videos, documentation, and LLM-assisted learning (for educational purposes)
- Focus: Understanding through practice - raw Linux commands and practical implementation
- Important: When building your own container, DON'T copy code from sources - it kills the fun! Write it yourself, break things, debug, and learn.
Why Build Your Own?
- Deep understanding of how containers really work
- Master low-level Linux concepts
- Learn by doing, not just reading
- It's incredibly fun when things finally click!
Network Namespaces
What are Network Namespaces?
Network namespaces provide isolation of network resources, giving each application its own networking stack.
Three Key Network Resources
1. Network Devices
- Ethernet interfaces (eth0, eth1)
- Loopback device (lo)
- Virtual interfaces
2. IP Tables (Firewall Rules)
- Packet filtering rules
- NAT rules
- Security policies
3. Routing Tables
- Route decisions
- Gateway configurations
- Network paths
Visual Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Host System โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Default Namespace โ โ App Namespace โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ Devices: โ โ Devices: โ โ
โ โ โข lo (127.0.0.1) โ โ โข lo (isolated) โ โ
โ โ โข eth0 โ โ โ โ
โ โ โข docker0 โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ IPTables: โ โ IPTables: โ โ
โ โ โข Complex rules โ โ โข Default/empty โ โ
โ โ โข Firewall config โ โ โข Customizable โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ Routing: โ โ Routing: โ โ
โ โ โข Internet routes โ โ โข Empty initially โ โ
โ โ โข Local routes โ โ โข Isolated โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Practical Commands
View Network Resources on Host:
# List network devices
ip link list
# View IP tables rules (requires sudo)
sudo iptables --list
# Show routing table
ip route
Create Network Namespace:
# Create isolated namespace with network isolation
sudo unshare --pid --net --fork --mount-proc /bin/bash
# Inside namespace, check resources:
ip link list # Only loopback device
iptables --list # Different rules
ip route # Empty routing table
Use Cases for Network Namespaces
- Security Isolation: Prevent applications from accessing the internet
- Network Testing: Simulate different network conditions
- Container Networking: Foundation for Docker/Kubernetes networking
- Multi-tenancy: Separate network resources for different applications
UTS Namespaces
What are UTS Namespaces?
UTS (UNIX Time-Sharing) namespaces provide isolation of hostname and domain name.
Visual Concept
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Host: "production-server" โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ
โ โ Web App 1 โ โ Web App 2 โ โ
โ โ โ โ โ โ
โ โ Hostname: โ โ Hostname: โ โ
โ โ "webapp-01" โ โ "webapp-02" โ โ
โ โ โ โ โ โ
โ โ UTS Namespace โ โ UTS Namespace โ โ
โ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Practical Implementation
# Create namespace with UTS isolation
sudo unshare --uts --pid --fork --mount-proc /bin/bash
# Change hostname in namespace
hostname webapp-01
# Reload bash to see change
exec bash
# Verify hostname
hostname
# Output: webapp-01
Benefits of UTS Namespaces
- Application Identity: Each container can have its own hostname
- Configuration Isolation: Applications can use hostname-based configs
- Testing: Simulate different server environments
IPC Namespaces
What are IPC Namespaces?
IPC (Inter-Process Communication) namespaces provide isolation of:
- System V IPC objects: Message queues, semaphores, shared memory
- POSIX message queues
IPC Resources Isolation
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Host System โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ Namespace 1 โ โ Namespace 2 โ โ
โ โโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโค โ
โ โ Message Queue: โ โ Message Queue: โ โ
โ โ Key: 0x12345 โ โ Key: 0x67890 โ โ
โ โ โ โ โ โ
โ โ Processes can โ โ Processes can โ โ
โ โ only see their โ โ only see their โ โ
โ โ own queue โ โ own queue โ โ
โ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ
โ โ โ โ
โ โโโโโโโโโโฌโโโโโโโโโโโโ โ
โ Isolated IPC โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Practical Commands
# Create namespace with IPC isolation
sudo unshare --ipc --pid --fork --mount-proc /bin/bash
# Create a message queue
ipcmk -Q
# View message queues
ipcs -q
# The queue is only visible in this namespace
Commands Reference
Essential Commands Summary
| Command | Purpose | Example |
|---|---|---|
unshare |
Create new namespaces | sudo unshare --net --pid /bin/bash |
ip link |
View network devices | ip link list |
ip route |
View routing table | ip route show |
iptables |
View/modify firewall rules | sudo iptables -L |
hostname |
View/set hostname | hostname NewName |
ipcmk |
Create IPC resources | ipcmk -Q |
ipcs |
View IPC resources | ipcs -q |
Common Namespace Creation Patterns
# Complete isolation (all namespaces)
sudo unshare --uts --ipc --pid --net --fork --mount-proc /bin/bash
# Network testing environment
sudo unshare --net --fork /bin/bash
# Application container simulation
sudo unshare --pid --net --uts --ipc --fork --mount-proc /bin/bash
Summary
Key Takeaways
1. Network Namespaces provide:
- Isolated network devices
- Separate IP tables (firewall rules)
- Independent routing tables
- Foundation for container networking
2. UTS Namespaces enable:
- Hostname isolation
- Domain name isolation
- Per-container identity
3. IPC Namespaces offer:
- Message queue isolation
- Semaphore isolation
- Shared memory isolation
- Secure inter-process communication
Namespace Interaction Diagram
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Linux Kernel โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Containerized Application โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ
โ โ โ โ
โ โ โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ โ โ
โ โ โ PID โ โ NET โ โ UTS โ โ โ
โ โ โNamespaceโ โNamespaceโ โNamespaceโ โ โ
โ โ โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ โ โ
โ โ โ โ
โ โ โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ โ โ
โ โ โ IPC โ โ Mount โ โ User โ โ โ
โ โ โNamespaceโ โNamespaceโ โNamespaceโ โ โ
โ โ โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ โ โ
โ โ โ โ
โ โ Complete Resource Isolation โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Real-World Applications
- Docker: Uses all namespace types for container isolation
- Kubernetes: Builds on namespaces for pod isolation
- systemd: Uses namespaces for service isolation
Top comments (2)
What is the difference between namespaces and linux screens ?
Namespaces create isolated mini-systems; Screen just keeps your terminal programs running even if you disconnect.