Docker run实际上发生了什么? 用Go模拟这个过程

本文探讨了Docker run背后的实际操作,主要涉及Linux /proc文件系统、主函数、容器进程的创建与启动。通过Go语言模拟Docker run过程,详细解释了如何创建namespace隔离的容器进程,如何通过syscall.Exec方法执行用户指定的程序作为容器内初始进程,最终实现容器的启动和运行。
摘要由CSDN通过智能技术生成

Linux Proc

Linux下的/proc文件系统是由内核提供的.它不是一个真正的文件系统,只包含了系统运行时候的消息.(如系统内存/mount设备信息/硬件配置)

它只存在于内存中,不占用外存空间.

本质是以文件系统的方式,为访问内核数据的操作提供接口

main函数

main.go

  • 启动的主函数
  • 现在定义了两个命令initCommand与runCommand
package main

import (
	log "github.com/Sirupsen/logrus"
	"github.com/urfave/cli"
	"os"
)

const usage = `mydocker is a simple container runtime implementation.
			   The purpose of this project is to learn how docker works and how to write a docker by ourselves
			   Enjoy it, just for fun.`

func main() {
   
	app := cli.NewApp()
	app.Name = "mydocker"
	app.Usage = usage

	app.Commands = []cli.Command{
   
		initCommand,
		runCommand,
	}

	//初始化日志配置
	app.Before = func(context *cli.Context) error {
   
		// Log as JSON instead of the default ASCII formatter.
		log.SetFormatter(&log.JSONFormatter{
   })

		log.SetOutput(os.Stdout)
		return nil
	}

	if err := app.Run(os.Args); err != nil {
   
		log.Fatal(err)
	}
}

main_command.go

  • runCommand

调用Run函数

package main

import (
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值