2nd. Docker client creation and command execution

Source: Internet
Author: User
Tags docker hub docker registry
This is a creation in Article, where the information may have evolved or changed.

2.1 Introduction

This chapter analyzes the contents of the Docker client based on the source code of the Docker 1.2.0. It consists of two parts, namely the creation of Docker client and the execution of Docker client commands. The specific contents of the two-part analysis are as follows.

The first section analyzes the creation of the Docker client. This part of the analysis can be divided into the following three steps:

    1. Analyzes how the command line flag parameter is resolved through the Docker command, as well as the request parameters in the Docker command.
    2. Analyze how to handle specific flag parameter information and collect the configuration information required by the Docker client.
    3. Analyze how to create a docker Client.

The second part analyzes how to execute the Docker command on the basis of the existing Docker client. This part of the analysis can be divided into the following two steps:

    1. Analyze how to parse the request parameters in the Docker command to get the type of the corresponding request.
    2. Analyzes how the Docker client executes a specific request command and eventually sends the request to the Docker Server.

2.2 Creating a Docker Client

2.2.1 The flag parameter resolution of the Docker command

For the parameters in the Docker request, we can divide it into two categories: the first class is the command-line argument, which is the parameters that the Docker program needs to provide when it runs, such as:-D 、--daemon=true 、--Daemon=false, and so on; the second type is Docker sent to Docker The actual request parameters of the server, such as PS, pull name, and so on.

For the first class, we are accustomed to call it the flag parameter, in the standard library of the Go language, a flag packet is provided for the class parameter, which makes it easy to parse the command line arguments.

Part of the main function code is as follows:

Func Main () {
If Reexec. Init () {
Return
}
Flag. Parse ()
Fixme:validate Daemon Flags here
}

Above source code realization, first Judge Reexec. The return value of the Init () method, if true, exits the run directly or continues execution. Reexec. The Init () function is defined in./docker/reexec/reexec.go, and it is possible to find that the return value of the code snippet is false because there is no initializer registration before the Docker runs. The reexec exists to coordinate the relationship between Execdriver and dockerinit when the container is created.

Judge Reexec. After Init (), the main function of Docker resolves the flag parameter in the command line by calling the Flag.parse () function. Docker defines multiple flag parameters in./docker/docker/flag.go and initializes some of the flag parameters with the init function. The code is as follows:

var (
Flversion = flag. Bool ([]string{"V", "-version"}, False, "Print version information and quit")
Fldaemon = flag. Bool ([]string{"D", "-daemon"}, False, "Enable daemon mode")
Flsocketgroup = flag. String ([]string{"G", "-group"}, "Docker", "Group to assign the UNIX socket specifby-h when running in Daemon Modeuse" ( The empty string) to disable setting of a group ")
Flenablecors = flag. Bool ([]string{"TLS"}, False, "use TLS; Implied by tls-verify flags ")
FLTLS = flag. Bool ([]string{"-tlsverify"}, False, "use TLS and verify the remote (daemon:verify client, client:verify daemon")
// These is initialized in Init () below since their default valus depend on Dockercertpath which isn ' t fully initialized unt Il init () runs
Flca string
Flcert
string
FlKey *string
flhosts []string
)

Func init () {
FLCA = flag. String ([]string{"-tlscacert"}, filepath. Join (Dockercertpath, Defaultcafile), "Trust only remotes providing a certficate signed by the CA given here")
Flcert = flag. String ([]string{"-tlscert"}, filepath. Join (Dockercertpath, Defaultcertfile), "Path to TLS certificate file")
FlKey = flag. String ([]string{"-tlskey"}, filepath. Join (Dockercertpath, Defaultkeyfile), "Path to TLS key file"
OPTs. Hostlistvar (&flhosts, []string{"H", "-host"}, "the socket (s) to bind to with daemon mode\nspecified using one or more TC P://host:port, Unix:///path/to/socket, fd://* or FD:SOCKETFD. ")
}

For the init function in Golang, the following features can be derived from deep analysis:

    • The init function is used for initialization of the package before the program executes, such as initializing variables, etc.
    • Each package can have more than one init function;
    • Each source file of a package can also have more than one init function;
    • The sequence of execution of the INIT function within the same package is not explicitly defined;
    • The init function of different packages determines the order of initialization according to the dependency relationship of the package import;
    • The init function cannot be called, but is called automatically before the main function is called.

2.2.2 How to create a docker Client

The Docker client is created using the Newdockercli method in the client package to create a Docker client instance CLI with the configuration parameter information already in use.

2.3 Docker Command execution

The main function executes to this stage, and the following needs to be performed for the Docker command: the request in the completed Docker client,docker command (stored in flag after flag parsing). ARG ()). That is, the program needs to use Docker client to parse the request parameters in the Docker command, draw the requested type, escape the request that Docker server can recognize, and eventually send it to the Docker server.

The Docker client performs two main tasks: parsing the request command, drawing the request type, and executing the specific type of request.

2.3.1 Docker Client Parsing request command

The Docker client resolves the work of the request command, which is the first completion of the Docker command execution section.

2.3.2 Docker Client Execution Request command

Different Docker, although the request content is different, but the request execution process is roughly the same, so this section will still use an example to illustrate the process, the example is Docker pull image_name. The purpose of this command is that the Docker client initiates a request to download the image, which is eventually received by the Docker server and is downloaded and stored by the Docker daemon.

When the Docker client executes the Docker pull image_name request command, the Cmdpull function is executed with the incoming parameter args[1:] ..., which is image_name.

If the user does not have a Docker registry address, the Docker default address is Docker hub address https://index.docker.io/v1/.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.