11# basic-docker-engine
22
3- Basic docker engine implementation from scratch
3+ A minimal, educational Docker-like container runtime implementation from scratch, focused on demonstrating container internals, monitoring capabilities, and Kubernetes integration.
44
5+ ## Project Scope
6+
7+ This is a ** teaching/runtime prototype** designed for:
8+ - Understanding container isolation mechanisms
9+ - Demonstrating the Docker monitoring problem
10+ - Exploring Kubernetes custom resource integration
11+ - Learning container runtime internals
12+
13+ ** Not intended for production use.** This is a reference implementation for education and research.
14+
15+ ## Core Features
16+
17+ ### Container Runtime
18+ - Container lifecycle management with state tracking (` created ` , ` running ` , ` exited ` , ` failed ` )
19+ - Automatic cgroup v1/v2 detection and graceful degradation
20+ - Filesystem isolation with layered image support
21+ - Process and network namespace isolation (when available)
22+ - Container logs and metadata persistence
23+
24+ ### System Monitoring
25+ - Multi-level monitoring (process, container, host)
26+ - Gap analysis between isolation levels
27+ - Correlation tracking across monitoring layers
28+ - See [ MONITORING.md] ( MONITORING.md ) for details
29+
30+ ### Kubernetes Integration
31+ - ResourceCapsule CRD for enhanced resource modeling
32+ - Operator for CRD reconciliation
33+ - GitOps-compatible resource management
34+ - See [ KUBERNETES_INTEGRATION.md] ( KUBERNETES_INTEGRATION.md ) for details
35+
36+ ## Prerequisites
37+
38+ - Linux system (tested on Ubuntu 20.04+)
39+ - Go 1.24+ for building
40+ - Root privileges for namespace operations
41+ - Optional: Kubernetes cluster for CRD features
542
643## Build steps
744
845### build go code
946
10- ``` basic
11- @j143 ➜ /workspaces/basic-docker-engine (main) $ go build -o basic-docker main.go
12- @j143 ➜ /workspaces/basic-docker-engine (main) $ ./basic-docker
13- Environment detected: inContainer=true, hasNamespacePrivileges=true, hasCgroupAccess=false
14- Usage:
15- basic-docker run <command> [args...] - Run a command in a container
16- basic-docker ps - List running containers
17- basic-docker images - List available images
18- basic-docker info - Show system information
47+ ``` bash
48+ go build -o basic-docker .
49+ ./basic-docker info
50+ ```
51+
52+ Expected output:
53+ ```
54+ Environment detected: inContainer=false, hasNamespacePrivileges=true, hasCgroupAccess=true, cgroupVersion=2
55+ Lean Docker Engine - System Information
56+ =======================================
57+ Go version: go1.24.11
58+ OS/Arch: linux/amd64
59+ Running in container: false
60+ Namespace privileges: true
61+ Cgroup access: true
62+ Cgroup version: v2
63+ Cgroup base path: /sys/fs/cgroup
64+ Memory controller: true
65+ CPU controller: true
66+ Available features:
67+ - Process isolation: true
68+ - Network isolation: true
69+ - Resource limits (memory): true
70+ - Resource limits (CPU): true
71+ - Filesystem isolation: true
1972```
2073
2174### create necessary folders
@@ -30,20 +83,83 @@ sudo mkdir -p /sys/fs/cgroup/memory/basic-docker
3083
3184``` bash
3285sudo chmod -R 755 /tmp/basic-docker
33- sudo chmod -R 755 /sys/fs/cgroup/memory/basic-docker
3486```
3587
36- ### Run a simple command in a container
88+ ## Usage
3789
38- > Note: This needs to be run as root due to namespace operations
90+ ### Basic Container Operations
3991
40- sudo ./basic-docker run /bin/sh -c "echo Hello from container"
92+ #### Run a container
93+ ``` bash
94+ # Run a simple command
95+ sudo ./basic-docker run test-image /bin/echo " Hello from container"
4196
97+ # Run an interactive shell (requires image with /bin/sh)
98+ sudo ./basic-docker run test-image /bin/sh
99+ ```
42100
43- > $ sudo ./basic-docker run /bin/sh -c "echo Hello from container"
44- > Starting container container-1743306338
45- > Error: failed to set memory limit: open /sys/fs/cgroup/memory/basic-docker/container-1743306338/memory.limit_in_bytes: permission denied
46- >
101+ #### List containers
102+ ``` bash
103+ # Shows all containers with their states
104+ sudo ./basic-docker ps
105+ ```
106+
107+ Example output:
108+ ```
109+ CONTAINER ID STATE COMMAND CREATED
110+ container-1767175530 exited /bin/echo 2025-12-31 10:05:30
111+ container-1767175600 running /bin/sleep 2025-12-31 10:06:00
112+ ```
113+
114+ #### Inspect a container
115+ ``` bash
116+ # Get detailed container information in JSON format
117+ sudo ./basic-docker inspect < container-id>
118+ ```
119+
120+ Example output:
121+ ``` json
122+ {
123+ "id" : " container-1767175530" ,
124+ "state" : " exited" ,
125+ "image" : " test-image" ,
126+ "command" : " /bin/echo" ,
127+ "args" : [" Hello from container" ],
128+ "created_at" : " 2025-12-31T10:05:30Z" ,
129+ "started_at" : " 2025-12-31T10:05:30Z" ,
130+ "finished_at" : " 2025-12-31T10:05:30Z" ,
131+ "exit_code" : 0 ,
132+ "pid" : 7505 ,
133+ "rootfs_path" : " /tmp/basic-docker/containers/container-1767175530/rootfs"
134+ }
135+ ```
136+
137+ #### View container logs
138+ ``` bash
139+ # Display stdout/stderr from a container
140+ sudo ./basic-docker logs < container-id>
141+ ```
142+
143+ #### Remove a stopped container
144+ ``` bash
145+ # Clean up container directories and resources
146+ sudo ./basic-docker rm < container-id>
147+ ```
148+
149+ ** Note:** Cannot remove running containers. Stop them first.
150+
151+ ### System Information
152+
153+ ``` bash
154+ # Display system capabilities and cgroup information
155+ ./basic-docker info
156+ ```
157+
158+ This command shows:
159+ - Cgroup version (v1 or v2) and availability
160+ - Memory and CPU controller support
161+ - Namespace privileges
162+ - Available isolation features
47163
48164## Architecture
49165
@@ -134,6 +250,67 @@ flowchart TB
134250 class DETECT,PRIV,CGROUP highlight
135251```
136252
253+ ## Container Lifecycle & State Management
254+
255+ Containers follow a well-defined state model:
256+
257+ ```
258+ created → running → exited
259+ ↓
260+ failed
261+ ```
262+
263+ ### States
264+
265+ - ** created** : Container directory structure created, metadata initialized
266+ - ** running** : Container process is executing
267+ - ** exited** : Container completed successfully (exit code 0)
268+ - ** failed** : Container terminated with error (non-zero exit code)
269+
270+ ### State Persistence
271+
272+ Container state is stored in ` /tmp/basic-docker/containers/<id>/state.json ` :
273+
274+ ``` json
275+ {
276+ "id" : " container-123" ,
277+ "state" : " exited" ,
278+ "image" : " alpine" ,
279+ "command" : " /bin/echo" ,
280+ "args" : [" hello" ],
281+ "created_at" : " 2025-12-31T10:00:00Z" ,
282+ "started_at" : " 2025-12-31T10:00:01Z" ,
283+ "finished_at" : " 2025-12-31T10:00:02Z" ,
284+ "exit_code" : 0 ,
285+ "pid" : 12345 ,
286+ "rootfs_path" : " /tmp/basic-docker/containers/container-123/rootfs"
287+ }
288+ ```
289+
290+ ## Cgroup Support & Resource Limits
291+
292+ The runtime automatically detects and adapts to the available cgroup version:
293+
294+ ### Cgroup v2 (Unified Hierarchy)
295+ - Detected via ` /sys/fs/cgroup/cgroup.controllers `
296+ - Uses ` memory.max ` for memory limits
297+ - Controllers enabled via ` cgroup.subtree_control `
298+
299+ ### Cgroup v1 (Legacy)
300+ - Detected via ` /sys/fs/cgroup/memory ` subsystem
301+ - Uses ` memory.limit_in_bytes ` for memory limits
302+ - Separate hierarchy per resource type
303+
304+ ### Graceful Degradation
305+
306+ When cgroup access is not available or permissions are insufficient:
307+ - Container still runs without resource limits
308+ - Warning logged but execution continues
309+ - ` info ` command shows cgroup availability status
310+ - No fatal errors from missing cgroup support
311+
312+ This ensures the runtime works in various environments (containers, VMs, bare metal) without requiring cgroup permissions.
313+
137314## Image Build Logic
138315
139316``` mermaid
0 commit comments