Docker源码分析二:Docker Client

本文深入Docker 18.02.0-ce源码,探讨Docker Client的创建过程,包括在`docker-cli/cli/command/cli.go`中的`NewDockerCli`方法和`newDockerCommand`,以及Docker Client如何执行命令,以`pull image`为例,展示了从`NewImageCommand`到`ImagePull`和`tryImageCreate`的方法调用,揭示了客户端如何将命令转换为API请求发送给Docker Server。
摘要由CSDN通过智能技术生成

Docker Client是Docker架构中用户与Docker Daemon建立通信的客户端。本文基于Docker 18.02.0-ce的源码分析Docker Client的内容。主要包括两个部分,分别是:

  • Docker Client的创建
  • Docker Client对命令的执行

1、Docker Client的创建

对于Docker这样一个C/S架构,客户端的存在意味着Docker相应任务的发起。用户首先要创建一个Docker Client,然后将特定的请求类型与参数传递至Docker Client,最终由Docker Client转义成Docker Server能识别的形式,并发送至Docker Server。
Docker Client的main函数位于docker-ce/components/cli/cmd/docker/docker.go,代码如下。

func main() {
    // Set terminal emulation based on platform as required.
    stdin, stdout, stderr := term.StdStreams()
    logrus.SetOutput(stderr)

    // 创建Cli,返回一个DockerCli实例
    dockerCli := command.NewDockerCli(stdin, stdout, stderr)
    // 创建cmd,配置一定的参数并添加所有子命令
    cmd := newDockerCommand(dockerCli)

    // 执行cmd
    if err := cmd.Execute(); err != nil {
        if sterr, ok := err.(cli.StatusError); ok {
            if sterr.Status != "" {
                fmt.Fprintln(stderr, sterr.Status)
            }
            // StatusError should only be used for errors, and all errors should
            // have a non-zero exit status, so never exit with 0
            if sterr.StatusCode == 0 {
                os.Exit(1)
            }
            os.Exit(sterr.StatusCode)
        }
        fmt.Fprintln(stderr, err)
        os.Exit(1)
    }
}

1)NewDockerCli方法位于docker-ce/components/cli/cli/command/cli.go文件,它返回一个DockerCli类型的对象实例。代码如下:

// DockerCli is an instance the docker command line client.
// Instances of the client can be returned from NewDockerCli.
type DockerCli struct {   
    configFile *configfile.ConfigFile
    in         *InStream
    out        *OutStream
    err        io.Writer
    client     client.APIClient
    serverInfo ServerInfo
    clientInfo ClientInfo  
}

// ServerInfo stores details about the supported features and platform of the server
type ServerInfo struct {
    HasExperimental bool
    OSType          string
}

// ClientInfo stores details about the supported features of the client
type ClientInfo struct {
    HasExperimental bool
    DefaultVersion  string   //api.defaultVersion or DOCKER_API_VERSION
    Orchestrator    Orchestrator
}

// NewDockerCli returns a DockerCli instance with IO output and error streams set by in, out and err.
func NewDockerCli(in io.ReadCloser, out, err io.Writer) *DockerCli {
    return &DockerCli{in: NewInStream(in), out: NewOutStream(out), err: err}   // 创建并初始化DockerCli类型的对象实例
}

2)newDockerCommand与main函数同在docker-ce/components/cli/cmd/docker/docker.go文件中,它根据DockerCli类型的对象实例,返回cobra.Command对象实例。
先了解几个库:

    "github.com/sirupsen/logrus"  //结构话的log库
    "github.com/spf13/cobra"    //命令行的库
    "github.com/spf13/pflag"    //用于替代go自身的flag包

newDockerCommand的代码如下:

// DockerCli实现了Command.Cli接口
func newDockerCommand(dockerCli *command.DockerCli) *cobra.Command {  
    opts := cliflags.NewClientOptions()   //配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值