This article analyzes the boot process of the Docker daemon; in the Daemon/daemon.go file;
Func newdaemon (config *config, Registryservice *registry. Service) (daemon *daemon, err error) {
Setdefaultmtu (config)//set Default MTU, here the default value is 1500
Ensure we have compatible configuration options
If err: = checkconfigoptions (config); Err! = Nil {
return nil, err
}//Check for conflicting configurations, mainly (1) config. Bridge.iface and CONFIG. Bridge.ip these two configurations can not all have, set one can;
(2) config. Bridge.enableiptables and CONFIG. Bridge.intercontainercommunication, which is the ICC, indicates whether Docker containers can communicate with each other,
ICC is achieved through iptables, that is, in the Iptables forward chain to add rules, so can not be false at the same time, but not clear icc=true, but the entableiptables is false when it will be;
(3) when config. When Bridge.enableiptables is false, the CONFIG.BRIDGE.ENABLEIPMASQ,IP camouflage function cannot be true, as with (2), because IP spoofing is achieved through iptables;
Do we have a disabled network?
Config. Disablebridge = isbridgenetworkdisabled (config)//judgment CONFIG. Bridge.iface and Disablenetworkbridge are equal, hesitate Iface default value is empty, disablenetworkbridge default value is None, so this config. Disablebridge to False
Verify the platform is supported as a daemon
If!platformsupported {
return Nil, errsystemnotsupported
}//In non-linux,freebsd,windows three OS, this is taken as false;
Validate platform-specific Requirements
If err: = Checksystem (); Err! = Nil {
return nil, err
//checksystem primarily verifies that the process running Docker is root, requires root privileges, and includes a version that verifies the Linux kernel;
Set up SIGUSR1 handler on unix-like systems, or a WIN32 global event
On the Windows to dump Go routine stacks
Setupdumpstacktrap ()//is used to receive the SIGUSR1 signal, after receiving the signal, the stack information is printed out, through the standard library signal. Notify () function to implement;
Get the canonical path to the Docker root directory
var realroot string
If _, Err: = OS. Stat (config. Root); Err! = Nil && os. Isnotexist (Err) {
realroot = config. Root
} else {
Realroot, err = fileutils. Readsymlinkeddirectory (config. Root)
If err! = Nil {
return nil, FMT. Errorf ("Unable to get the full path to root (%s):%s", config. Root, Err)
}
}//Gets the root path of the Docker runtime, and subsequent processes create various subdirectories under the root directory, by default in the "/var/lib/docker" path;
Config. Root = Realroot
Create the root directory if it doesn ' t exists
If err: = System. Mkdirall (config. Root, 0700); Err! = Nil {
return nil, err
}//If the root path does not exist, then create one;
Set up the TmpDir to use a canonical path
TMP, ERR: = TempDir (config. Root)
If err! = Nil {
return nil, FMT. Errorf ("Unable to get the TempDir under%s:%s", config. Root, Err)
}
Realtmp, err: = Fileutils. Readsymlinkeddirectory (TMP)
If err! = Nil {
return nil, FMT. Errorf ("Unable to get ' full path to the TempDir (%s):%s", TMP, ERR)
}
Os. Setenv ("TMPDIR", realtmp)//establish the storage path of the temp file, read the environment variable Docker_tmpdir value, if not the TMP subdirectory is established directly under the root path, that is/var/lib/docker/tmp
Set the default driver
Graphdriver. defaultdriver = config. Graphdriver//Set up graph Driver,graphdriver is primarily to manage mirroring, and the implementation of the relationship between mirroring and mirroring.
Load Storage Driver
Driver, err: = Graphdriver. New (config. Root, CONFIG. Graphoptions)
If err! = Nil {
return nil, FMT. Errorf ("Error initializing Graphdriver:%v", err)
}
Logrus. DEBUGF ("Using graph driver%s", driver)
The establishment of graph Driver,graphdriver is primarily to manage the mirroring and the implementation of the relationship between mirroring and mirroring. Because of CONFIG. The default value for Graphdriver is NULL, so the main processing flow is graphdriver. New ();
The order of precedence for loads is priority = []string{"Aufs", "Btrfs", "ZFS", "Devicemapper", "overlay", "VFS"},
D: = &daemon{}
D.driver = Driver
Ensure the graph driver is shutdown at a later point
Defer func () {
If err! = Nil {
If err: = D.shutdown (); Err! = Nil {
Logrus. Error (ERR)
}
}
}()
Verify Logging Driver Type
If config. Logconfig.type! = "None" {
If _, Err: = Logger. Getlogdriver (config. Logconfig.type); Err! = Nil {
return nil, FMT. Errorf ("Error finding the logging driver:%v", err)
}
}//Log driver configuration, the default log driver is Json-file, in the Deamon/logger directory is a variety of log driver, including: Fluentd,syslogd,journald, G Elf etc (first three kinds of understanding, gelf not quite know https://www.graylog.org/resources/gelf/)
Logrus. DEBUGF ("Using Default logging driver%s", CONFIG. Logconfig.type)
Configure and validate the kernels security support
If err: = Configurekernelsecuritysupport (config, d.driver.string ()); Err! = Nil {
return nil, err
}//Set whether the system uses selinux,selinux there is a problem that can not be used with btrfs graphdriver;
Daemonrepo: = filepath. Join (config. Root, "containers")
If err: = System. Mkdirall (Daemonrepo, 0700); Err! = Nil {
return nil, err//Create the container's storage directory, create the container subdirectory under the root directory,/var/log/docker/container
Migrate the container if it is Aufs and AUFS is enabled
If err: = Migrateifdownlevel (d.driver, config. Root); Err! = Nil {
return nil, err
}//Migrate the storage form of image and container to the new version of Docker in the old version of Docker that may exist;
Logrus. Debug ("Creating images graph")
G, err: = Graph. Newgraph (filepath. Join (config. Root, "graph"), D.driver)
If err! = Nil {
return nil, err
}//Use driver to instantiate a Newgraph object that manages the image of the filesystem and the relationship between the file system;
Configure the volumes driver
Volstore, err: = configurevolumes (config)
If err! = Nil {
return nil, err
}//Set data volume driver, a data volume is a means of sharing data between Docker containers; a data volume can be a native command (mounted in a directory to a container by the-V identity)
The data volume can also be used as a data volume container to be mounted to a container via--volumes-from;
Trustkey, Err: = API. Loadorcreatetrustkey (config. Trustkeypath)
If err! = Nil {
return nil, err
}//Create key, path in/etc/docker/key.json
Trustdir: = filepath. Join (config. Root, "trust")
If err: = System. Mkdirall (Trustdir, 0700); Err! = Nil {
return nil, err
}
Trustservice, err: = Trust. Newstore (Trustdir)
If err! = Nil {
return nil, FMT. Errorf ("Could not create trust store:%s", err)
}//Create Trustservice to provide authentication services;
Eventsservice: = events. New ()
Logrus. Debug ("Creating repository list")
Tagcfg: = &graph. tagstoreconfig{
Graph:g,
Key:trustkey,
Registry:registryservice,
Events:eventsservice,
Trust:trustservice,
}
Repositories, err: = Graph. Newtagstore (filepath. Join (config. Root, "repositories-" +d.driver.string ()), tagcfg)
If err! = Nil {
return nil, FMT. Errorf ("couldn ' t create Tag store repositories-%s:%s", d.driver.string (), err)
//tagstore is a list of warehouses used to store images,
If restorer, OK: = D.driver. (Graphdriver. Imagerestorer); OK {
If _, Err: = restorer. Restorecustomimages (repositories, g); Err! = Nil {
return nil, FMT. Errorf ("couldn ' t restore Custom images:%s", err)
}
}
D.netcontroller, err = initnetworkcontroller (config)
If err! = Nil {
return nil, FMT. Errorf ("Error Initializing Network controller:%v", err)
}//Initialize Docker network environment;
Graphdbpath: = filepath. Join (config. Root, "linkgraph.db")
Graph, err: = Graphdb. Newsqliteconn (Graphdbpath)
If err! = Nil {
return nil, err
}//graphdb is a graph data that records the relationship between mirroring and mirroring;
D.containergraph = Graph
var Sysinitpath string
If config. Execdriver = = "Lxc" {
Initpath, err: = configuresysinit (config)
If err! = Nil {
return nil, err
}
Sysinitpath = Initpath
}
SysInfo: = SysInfo. New (False)
Check if Devices cgroup is mounted, it's hard requirement for container security,
On Linux/freebsd.
If runtime. GOOS! = "Windows" &&!sysinfo.cgroupdevicesenabled {
return nil, FMT. Errorf ("Devices cgroup isn ' t mounted")
}
ed, err: = Execdrivers. Newdriver (config. Execdriver, CONFIG. execoptions, CONFIG. Execroot, CONFIG. Root, Sysinitpath, SysInfo)
If err! = Nil {
return nil, err
}//Execdriver is implemented using Docker container execution driver, execution of container creation and operation by Execdriver, etc., by calling Cgroup,usernamespace and so on;
D.id = Trustkey.publickey (). KeyID ()
D.repository = Daemonrepo
D.containers = &contstore{s:make (Map[string]*container)}
D.execcommands = Newexecstore ()
D.graph = g
D.repositories = repositories
D.idindex = Truncindex. Newtruncindex ([]string{})
d.config = Config
D.sysinitpath = Sysinitpath
D.execdriver = Ed
D.statscollector = Newstatscollector (1 * time. Second)
d.defaultlogconfig = config. LogConfig
D.registryservice = Registryservice
D.eventsservice = Eventsservice
D.volumes = Volstore
d.root = config. Root
Go D.EXECCOMMANDGC ()
If err: = D.restore (); Err! = Nil {
return nil, err
}
Return D, Nil
}
Docker Source Analysis III (based on 1.8.2 version), Newdaemon boot