This article QQ link: http://user.qzone.qq.com/29185807/blog/1462527343
This article Csdn blog links: http://blog.csdn.net/screscent/article/details/51333371
The previous 5 articles briefly analyzed the next Docker daemon workflow.
So now go into Docker client mode
1. Initial process
code in Docker\docker\flags.go
First, let's look at all the operations that are available.
By Flag.usage, it's the most straightforward.
From the init function above, it can be seen that all the command provided
We enter the main function
Docker\docker\docker.go
func Main () {
At the end of the main function, a dockercli is constructed and the CLI is called. Cmd
2, Dockercli
code in Docker\api\client\cli.go
Some parameter preparation, then constructs the DOCKERCLI structure body
Let's look at the DOCKERCLI structure.
The number of members of the structure is still relatively small, relatively more concise than the daemon.
2.1, CMD
Now, let's see how cmd is converted.
According to the command line parameters, to get, if there is a problem, call Cmdhelp
2.1.1, Cmdhelp
As you can see here, in fact the last call is flag. Usage (), which we have analyzed at the outset.
2.1.2, GetMethod
GetMethod, by adding "cmd" in front of CMD, and then finding the corresponding handler function by reflection.
Here is the handler function for all of the CMD provided.
2.2.cmdcreate
Now let's look at the Create command
code in Docker\api\client\commands.go
The parameters are parsed first, and the Config,hostconfig,cmd is obtained.
Then call the CreateContainer
2.2.1,CreateContainer
The parameters are merged, then call is called, the method is passed in as the path, and the encoding method
Finally, the returned response is decoded and the result is returned
2.2.2, call
code in Docker\api\client\utils.go
The data is encoded, and then the clientrequest is called
2.2.3 Clientrequest
code in Docker\api\client\utils.go
The code here is simple and clear, constructs a httprequest, and then sets some parameters
Then the httpclient is used to initiate the request, and the data obtained is returned to the upper level
3. Summary
Dockerclient, the code volume is less, the function is clear. Read the input parameters, find the corresponding cmdhandler by launching, and do some related operations. Finally, by calling the call---"Clientrequest---" httpclient and daemon connection.
If there is any explanation of the wrong place, please forgive me, look correct.
Hao Haohua
QQ Crescent 29185807
May 6, 2016
(Copyright notice: This article for the author's original, if need to reprint please notify me, and mark the source and author. If reproduced without authorization, the right to pursue its infringement is reserved. )
"Original" Docker Source Analysis (6)---dockerclient