Docker [7]

Docker 源码分析

Posted by ZYT on April 16, 2019

一、源代码

$ git clone https://github.com/moby/moby.git

二、代码分析

1. 启动 daemon ,配置参数,监听端口

/github.com/docker/docker/cmd/dockerd/docker.go

// 启动 daemon
func newDaemonCommand() (*cobra.Command, error) {
    // 命令配置参数
	opts := newDaemonOptions(config.New())

	cmd := &cobra.Command{
		Use:           "dockerd [OPTIONS]",
		Short:         "A self-sufficient runtime for containers.",
		SilenceUsage:  true,
		SilenceErrors: true,
		Args:          cli.NoArgs,
		RunE: func(cmd *cobra.Command, args []string) error {
            opts.flags = cmd.Flags()
            // 运行 daemon 端
			return runDaemon(opts)  // daemonCli.start(opts)
		},
		DisableFlagsInUseLine: true,
		Version:               fmt.Sprintf("%s, build %s", dockerversion.Version, dockerversion.GitCommit),
	}
	cli.SetupRootCommand(cmd)

    flags := cmd.Flags()
	flags.BoolP("version", "v", false, "Print version information and quit")
	defaultDaemonConfigFile, err := getDefaultDaemonConfigFile()
	if err != nil {
		return nil, err
	}
	flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "Daemon configuration file")
	opts.InstallFlags(flags)
	if err := installConfigFlags(opts.daemonConfig, flags); err != nil {
		return nil, err
	}
	installServiceFlags(flags)

	return cmd, nil
}

func init() {
	if dockerversion.ProductName != "" {
		apicaps.ExportedProduct = dockerversion.ProductName
	}
}

// 程序入口
func main() {
    // 判断是否有初始化的函数,如果有,退出程序
	if reexec.Init() {
		return
	}

    // initial log formatting; this setting is updated after the daemon configuration is loaded.
    // 初始化 logrus 的时间戳参数配置
	logrus.SetFormatter(&logrus.TextFormatter{
		TimestampFormat: jsonmessage.RFC3339NanoFixed,
		FullTimestamp:   true,
	})

    // Set terminal emulation based on platform as required.
    // 获取标准数据流
	_, stdout, stderr := term.StdStreams()

    // 初始化标准输出和标准错误的 log
	initLogging(stdout, stderr)

	// 如果程序有误,输出错误信息并且退出程序
    onError := func(err error) {
		fmt.Fprintf(stderr, "%s\n", err)
		os.Exit(1)
	}

	// 启动 daemon
    cmd, err := newDaemonCommand()
	if err != nil {
		onError(err)
    }
    // 执行命令
	cmd.SetOutput(stdout)
	if err := cmd.Execute(); err != nil {
		onError(err)
	}
}