Based on a detailed analysis of the technology stack that Docker relies on, this book uses code examples step by step to allow readers to build a container engine in Go language step by step. Unlike other books that introduce Docker principles or analyze code, this book aims to provide readers with a hands-on route to implement Docker isolation, build Docker images, container lifecycles, and Docker networks step by step. The codes involved in this book are all hosted on GitHub. Readers can learn the construction process from the code level by referring to the steps in the book, so as to master the entire container technology stack. This book also briefly introduces the direction and implementation of the current container technology in the industry to deepen readers\' knowledge and understanding of the container ecosystem. This book is suitable for readers who have used or have some understanding of container technology and hope to have a deeper understanding of the principles and best practices of container technology. Chapter 1 Containers and Development Languages……………………………………………………1 1.1 Docker ……………………………………………………………………………………1 1.1.1 Introduction………………………………………………………………………………1 1.1.2 Comparison between containers and virtual machines…………………………………2 1.1.3 Containers accelerate development efficiency………………………………………3 1.1.4 Collaborative development using containers…………………………………4 1.1.5 Rapid capacity expansion using containers…………………………………4 1.1.6 Installing and using Docker…………………………………………………4 1.2 Go …………………………………………………………………………………………5 1.2.1 Description……………………………………………………………………5 1.2.2 Installing Go ………………………………………………………………6 1.2.3 Configuring GOPATH ……………………………………………………6 1.3 Summary……………………………………………………………………………7 Chapter 2 Basic Technologies………………………………………………………………8 2.1 Introduction to Linux Namespace…………………………………………………8 2.1.1 Concepts………………………………………………………………………8 2.1.2 UTS Namespace …………………………………………………………10 2.1.3 IPC Namespace …………………………………………………………11 2.1.4 PID Namespace ………………………………………………………………13 2.1.5 Mount Namespace ………………………………………………………………14 2.1.6 User Namespace ………………………………………………………………16 XII Write your own Docker 2.1.7 Network Namespace ………………………………………………………… 18 2.2 Introduction to Linux Cgroups………………………………………………………… 20 2.2.1 What is Linux Cgroups ……………………………………………………… 20 2.2.2 How does Docker use Cgroups……………………………………… 24 2.2.3 Using Go to limit container resources through cgroup… 25 2.3 Union File System ……………………………………………………………… 26 2.3.1 What is Union File System ……………………………………………… 26 2.3.2 AUFS …………………………………………………………………… 27 2.3.3 How does Docker use AUFS 2.3.4 Write AUFS by yourself………………………………………… 34 2.4 Summary………………………………………………………………………… 37 Chapter 3 Constructing containers…………………………………………………… 38 3.1 Constructing a container that implements the run command version… 38 3.1.1 Introduction to the Linux proc file system…………………………… 38 3.1.2 Implementing the run command……………………………………… 39 3.2 Adding resource limits to containers………………………………… 45 3.2.1 Defining the data structure of Cgroups……………………………… 45 3.2.2 Adding resource limits when starting a container…………… 51 3.3 Adding Pipeline and Environment Variable Recognition……………………………… 53 3.4 Summary………………………………………………………………………… 58 Chapter 4 Building an Image……………………………………………………………… 59 4.1 Using busybox to create a container……………………………………… 59 4.1.1 busybox ……………………………………………………………………… 59 4.1.2 pivot_root …………………………………………………………………… 60 4.2 Using AUFS to package busybox……………………………………… 63 4.3 Implementing volume data volumes……………………………………………… 67 4.4 Implementing simple image packaging………………………………………… 75 4.5 Summary…………………………………………………………………… 77 Chapter 5 Advanced Container Building……………………………………………………… 78 5.1 Implementing Background Running of Containers………………………………………… 78 5.2 Viewing Running Containers…………………………………………… 82 5.2.1 Preparing Data…………………………………………………………… 82 5.2.2 Implementing mydocker ps………………………………………………… 87 5.3 Viewing Container Logs………………………………………………… 90 5.4 Entering Container Namespace…………………………………………… 93 5.4.1 setns ………………………………………………………………………… 94 5.4.2 Cgo …………………………………………………………………… 94 5.4.3 Implementing commands…………………………………………………… 94 5.5 Implementing stopping containers……………………………………………… 100 5.6 Implementing deleting containers…………………………………………… 104 5.7 Creating an Image Through a Container………………………………………… 105 5.8 Running a Container with Environment Variables Specifyed… 117 5.8.1 Modifying runCommand ……………………………………………………… 117 5.8.2 Modifying the Run Function……………………………………………… 117 5.8.3 Modifying the NewParentProcess Function……………………… 118 5.8.4 Modifying the mydocker exec Command…………………………… 119 5.9 Summary………………………………………………………………………… 121 Chapter 6 Container Network……………………………………………………… 122 6.1 Introduction to Network Virtualization Technology…………………………… 122 6.1.1 Linux 6.1.2 Linux routing table…………………………………………………… 124 6.1.3 Linux iptables ………………………………………………………………… 126 6.1.4 Introduction to Go language network library……………………………… 127 6.2 Building container network model………………………………………… 128 6.2.1 Model…………………………………………………………………… 128 6.2.2 Call relationship…………………………………………………… 130 6.3 Container address allocation……………………………………………… 137 6.3.1 Introduction to bitmap algorithm………………………………………… 138 6.3.2 6.3.3 Implementation of Address Allocation…………………………………… 140 6.3.4 Implementation of Address Release……………………………………… 142 6.3.5 Test…………………………………………………………………… 142 6.4 Creating a Bridge Network…………………………………………………… 144 6.4.1 Bridge Driver Create Implementation…………………………………… 144 6.4.2 Bridge Driver Initializes Linux Bridge Process…………… 144 6.4.3 Bridge Driver Delete Implementation……………………………… 148 6.4.4 Test…………………………………………………………………… 148 6.5 6.5.1 The process of mounting container endpoints…………………………………… 150 6.5.2 Testing…………………………………………………………………… 156 6.6 Container cross-host network……………………………………………… 159 6.6.1 IPAM for cross-host container network…………………………… 160 6.6.2 Common implementation methods of cross-host container network communication… 161 6.7 Summary………………………………………………………………………… 163 Chapter 7 Advanced Practice……………………………………………………………… 164 7.1 Use mydocker to create an accessible nginx container…………… 164 7.1.1 Get nginx tar package………………………………………………………… 164 7.1.2 Build your own nginx image…………………………………………… 165 7.1.3 Run mynginx container……………………………………………… 167 7.2 Use mydocker to create a flask + redis counter…………………………… 169 7.2.1 Create a redis container…………………………………………………… 169 7.2.2 Make a flask image……………………………………………… 173 7.2.3 Create a myflask container………………………………………… 176 7.3 runC ………………………………………………………………………………… 177 7.3.1 Introduction…………………………………………………………………… 177 7.3.2 OCI standard bundle …………………………………………………… 177 Contents XV 7.3.3 config.json …………………………………………………………………… 178 7.3.4 mounts ………………………………………………………………………… 178 7.3.5 process ………………………………………………………………………… 179 7.3.6 user ………………………………………………………………………… 179 7.3.7 hostname ……………………………………………………………… 180 7.3.8 platform …………………………………………………………………… 180 7.3.9 Hook …………………………………………………………………… 181 7.4 runC creates a container process……………………………………………… 182 7.5 Introduction to the Docker containerd project…………………………………… 186 7.5.1 Architecture……………………………………………………………… 187 7.5.2 Features and roadmap……………………………………………… 188 7.5.3 Relationship between containerd and Docker……………………… 188 7.5.4 Relationship between containerd, OCI and runC………………… 188 7.5.5 Relationship between containerd and container orchestration systems……… 189 7.6 Kubernetes CRI container engine…………………………………… 189 7.6.1 What is CRI………………………………………………………… 190 7.6.2 Why do we need CRI…………………………………………………… 193 7.6.3 Why CRI is an interface and is container-based rather than Pod-based ………… 193 7.6.4 How to use CRI ………………………………………………………… 193 7.6.5 Objectives of CRI…………………………………………………… 194 7.6.6 Known Issues…………………………………………………… 194 7.7 Summary…………………………………………………………………… 195
You Might Like
Recommended ContentMore
Open source project More
Popular Components
Searched by Users
Just Take a LookMore
Trending Downloads
Trending ArticlesMore