daemon.go 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. // Package daemon exposes the functions that occur on the host server
  2. // that the Docker daemon is running.
  3. //
  4. // In implementing the various functions of the daemon, there is often
  5. // a method-specific struct for configuring the runtime behavior.
  6. package daemon // import "github.com/docker/docker/daemon"
  7. import (
  8. "context"
  9. "fmt"
  10. "io/ioutil"
  11. "math/rand"
  12. "net"
  13. "os"
  14. "path"
  15. "path/filepath"
  16. "runtime"
  17. "strings"
  18. "sync"
  19. "time"
  20. "google.golang.org/grpc"
  21. "github.com/containerd/containerd"
  22. "github.com/containerd/containerd/defaults"
  23. "github.com/containerd/containerd/pkg/dialer"
  24. "github.com/containerd/containerd/remotes/docker"
  25. "github.com/docker/distribution/reference"
  26. "github.com/docker/docker/api/types"
  27. containertypes "github.com/docker/docker/api/types/container"
  28. "github.com/docker/docker/api/types/swarm"
  29. "github.com/docker/docker/builder"
  30. "github.com/docker/docker/container"
  31. "github.com/docker/docker/daemon/config"
  32. "github.com/docker/docker/daemon/discovery"
  33. "github.com/docker/docker/daemon/events"
  34. "github.com/docker/docker/daemon/exec"
  35. "github.com/docker/docker/daemon/images"
  36. "github.com/docker/docker/daemon/logger"
  37. "github.com/docker/docker/daemon/network"
  38. "github.com/docker/docker/errdefs"
  39. "github.com/moby/buildkit/util/resolver"
  40. "github.com/moby/buildkit/util/tracing"
  41. "github.com/sirupsen/logrus"
  42. // register graph drivers
  43. _ "github.com/docker/docker/daemon/graphdriver/register"
  44. "github.com/docker/docker/daemon/stats"
  45. dmetadata "github.com/docker/docker/distribution/metadata"
  46. "github.com/docker/docker/dockerversion"
  47. "github.com/docker/docker/image"
  48. "github.com/docker/docker/layer"
  49. "github.com/docker/docker/libcontainerd"
  50. "github.com/docker/docker/pkg/idtools"
  51. "github.com/docker/docker/pkg/locker"
  52. "github.com/docker/docker/pkg/plugingetter"
  53. "github.com/docker/docker/pkg/sysinfo"
  54. "github.com/docker/docker/pkg/system"
  55. "github.com/docker/docker/pkg/truncindex"
  56. "github.com/docker/docker/plugin"
  57. pluginexec "github.com/docker/docker/plugin/executor/containerd"
  58. refstore "github.com/docker/docker/reference"
  59. "github.com/docker/docker/registry"
  60. "github.com/docker/docker/runconfig"
  61. volumesservice "github.com/docker/docker/volume/service"
  62. "github.com/docker/libnetwork"
  63. "github.com/docker/libnetwork/cluster"
  64. nwconfig "github.com/docker/libnetwork/config"
  65. "github.com/pkg/errors"
  66. "golang.org/x/sync/semaphore"
  67. )
  68. // ContainersNamespace is the name of the namespace used for users containers
  69. const ContainersNamespace = "moby"
  70. var (
  71. errSystemNotSupported = errors.New("the Docker daemon is not supported on this platform")
  72. )
  73. // Daemon holds information about the Docker daemon.
  74. type Daemon struct {
  75. ID string
  76. repository string
  77. containers container.Store
  78. containersReplica container.ViewDB
  79. execCommands *exec.Store
  80. imageService *images.ImageService
  81. idIndex *truncindex.TruncIndex
  82. configStore *config.Config
  83. statsCollector *stats.Collector
  84. defaultLogConfig containertypes.LogConfig
  85. RegistryService registry.Service
  86. EventsService *events.Events
  87. netController libnetwork.NetworkController
  88. volumes *volumesservice.VolumesService
  89. discoveryWatcher discovery.Reloader
  90. root string
  91. seccompEnabled bool
  92. apparmorEnabled bool
  93. shutdown bool
  94. idMapping *idtools.IdentityMapping
  95. // TODO: move graphDrivers field to an InfoService
  96. graphDrivers map[string]string // By operating system
  97. PluginStore *plugin.Store // todo: remove
  98. pluginManager *plugin.Manager
  99. linkIndex *linkIndex
  100. containerdCli *containerd.Client
  101. containerd libcontainerd.Client
  102. defaultIsolation containertypes.Isolation // Default isolation mode on Windows
  103. clusterProvider cluster.Provider
  104. cluster Cluster
  105. genericResources []swarm.GenericResource
  106. metricsPluginListener net.Listener
  107. machineMemory uint64
  108. seccompProfile []byte
  109. seccompProfilePath string
  110. diskUsageRunning int32
  111. pruneRunning int32
  112. hosts map[string]bool // hosts stores the addresses the daemon is listening on
  113. startupDone chan struct{}
  114. attachmentStore network.AttachmentStore
  115. attachableNetworkLock *locker.Locker
  116. }
  117. // StoreHosts stores the addresses the daemon is listening on
  118. func (daemon *Daemon) StoreHosts(hosts []string) {
  119. if daemon.hosts == nil {
  120. daemon.hosts = make(map[string]bool)
  121. }
  122. for _, h := range hosts {
  123. daemon.hosts[h] = true
  124. }
  125. }
  126. // HasExperimental returns whether the experimental features of the daemon are enabled or not
  127. func (daemon *Daemon) HasExperimental() bool {
  128. return daemon.configStore != nil && daemon.configStore.Experimental
  129. }
  130. // Features returns the features map from configStore
  131. func (daemon *Daemon) Features() *map[string]bool {
  132. return &daemon.configStore.Features
  133. }
  134. // NewResolveOptionsFunc returns a call back function to resolve "registry-mirrors" and
  135. // "insecure-registries" for buildkit
  136. func (daemon *Daemon) NewResolveOptionsFunc() resolver.ResolveOptionsFunc {
  137. return func(ref string) docker.ResolverOptions {
  138. var (
  139. registryKey = "docker.io"
  140. mirrors = make([]string, len(daemon.configStore.Mirrors))
  141. m = map[string]resolver.RegistryConf{}
  142. )
  143. // must trim "https://" or "http://" prefix
  144. for i, v := range daemon.configStore.Mirrors {
  145. v = strings.TrimPrefix(v, "https://")
  146. v = strings.TrimPrefix(v, "http://")
  147. mirrors[i] = v
  148. }
  149. // set "registry-mirrors"
  150. m[registryKey] = resolver.RegistryConf{Mirrors: mirrors}
  151. // set "insecure-registries"
  152. for _, v := range daemon.configStore.InsecureRegistries {
  153. v = strings.TrimPrefix(v, "http://")
  154. m[v] = resolver.RegistryConf{
  155. PlainHTTP: true,
  156. }
  157. }
  158. def := docker.ResolverOptions{
  159. Client: tracing.DefaultClient,
  160. }
  161. parsed, err := reference.ParseNormalizedNamed(ref)
  162. if err != nil {
  163. return def
  164. }
  165. host := reference.Domain(parsed)
  166. c, ok := m[host]
  167. if !ok {
  168. return def
  169. }
  170. if len(c.Mirrors) > 0 {
  171. def.Host = func(string) (string, error) {
  172. return c.Mirrors[rand.Intn(len(c.Mirrors))], nil
  173. }
  174. }
  175. def.PlainHTTP = c.PlainHTTP
  176. return def
  177. }
  178. }
  179. func (daemon *Daemon) restore() error {
  180. var mapLock sync.Mutex
  181. containers := make(map[string]*container.Container)
  182. logrus.Info("Loading containers: start.")
  183. dir, err := ioutil.ReadDir(daemon.repository)
  184. if err != nil {
  185. return err
  186. }
  187. // parallelLimit is the maximum number of parallel startup jobs that we
  188. // allow (this is the limited used for all startup semaphores). The multipler
  189. // (128) was chosen after some fairly significant benchmarking -- don't change
  190. // it unless you've tested it significantly (this value is adjusted if
  191. // RLIMIT_NOFILE is small to avoid EMFILE).
  192. parallelLimit := adjustParallelLimit(len(dir), 128*runtime.NumCPU())
  193. // Re-used for all parallel startup jobs.
  194. var group sync.WaitGroup
  195. sem := semaphore.NewWeighted(int64(parallelLimit))
  196. for _, v := range dir {
  197. group.Add(1)
  198. go func(id string) {
  199. defer group.Done()
  200. _ = sem.Acquire(context.Background(), 1)
  201. defer sem.Release(1)
  202. container, err := daemon.load(id)
  203. if err != nil {
  204. logrus.Errorf("Failed to load container %v: %v", id, err)
  205. return
  206. }
  207. if !system.IsOSSupported(container.OS) {
  208. logrus.Errorf("Failed to load container %v: %s (%q)", id, system.ErrNotSupportedOperatingSystem, container.OS)
  209. return
  210. }
  211. // Ignore the container if it does not support the current driver being used by the graph
  212. currentDriverForContainerOS := daemon.graphDrivers[container.OS]
  213. if (container.Driver == "" && currentDriverForContainerOS == "aufs") || container.Driver == currentDriverForContainerOS {
  214. rwlayer, err := daemon.imageService.GetLayerByID(container.ID, container.OS)
  215. if err != nil {
  216. logrus.Errorf("Failed to load container mount %v: %v", id, err)
  217. return
  218. }
  219. container.RWLayer = rwlayer
  220. logrus.Debugf("Loaded container %v, isRunning: %v", container.ID, container.IsRunning())
  221. mapLock.Lock()
  222. containers[container.ID] = container
  223. mapLock.Unlock()
  224. } else {
  225. logrus.Debugf("Cannot load container %s because it was created with another graph driver.", container.ID)
  226. }
  227. }(v.Name())
  228. }
  229. group.Wait()
  230. removeContainers := make(map[string]*container.Container)
  231. restartContainers := make(map[*container.Container]chan struct{})
  232. activeSandboxes := make(map[string]interface{})
  233. for _, c := range containers {
  234. group.Add(1)
  235. go func(c *container.Container) {
  236. defer group.Done()
  237. _ = sem.Acquire(context.Background(), 1)
  238. defer sem.Release(1)
  239. if err := daemon.registerName(c); err != nil {
  240. logrus.Errorf("Failed to register container name %s: %s", c.ID, err)
  241. mapLock.Lock()
  242. delete(containers, c.ID)
  243. mapLock.Unlock()
  244. return
  245. }
  246. if err := daemon.Register(c); err != nil {
  247. logrus.Errorf("Failed to register container %s: %s", c.ID, err)
  248. mapLock.Lock()
  249. delete(containers, c.ID)
  250. mapLock.Unlock()
  251. return
  252. }
  253. // The LogConfig.Type is empty if the container was created before docker 1.12 with default log driver.
  254. // We should rewrite it to use the daemon defaults.
  255. // Fixes https://github.com/docker/docker/issues/22536
  256. if c.HostConfig.LogConfig.Type == "" {
  257. if err := daemon.mergeAndVerifyLogConfig(&c.HostConfig.LogConfig); err != nil {
  258. logrus.Errorf("Failed to verify log config for container %s: %q", c.ID, err)
  259. }
  260. }
  261. }(c)
  262. }
  263. group.Wait()
  264. for _, c := range containers {
  265. group.Add(1)
  266. go func(c *container.Container) {
  267. defer group.Done()
  268. _ = sem.Acquire(context.Background(), 1)
  269. defer sem.Release(1)
  270. daemon.backportMountSpec(c)
  271. if err := daemon.checkpointAndSave(c); err != nil {
  272. logrus.WithError(err).WithField("container", c.ID).Error("error saving backported mountspec to disk")
  273. }
  274. daemon.setStateCounter(c)
  275. logrus.WithFields(logrus.Fields{
  276. "container": c.ID,
  277. "running": c.IsRunning(),
  278. "paused": c.IsPaused(),
  279. }).Debug("restoring container")
  280. var (
  281. err error
  282. alive bool
  283. ec uint32
  284. exitedAt time.Time
  285. )
  286. alive, _, err = daemon.containerd.Restore(context.Background(), c.ID, c.InitializeStdio)
  287. if err != nil && !errdefs.IsNotFound(err) {
  288. logrus.Errorf("Failed to restore container %s with containerd: %s", c.ID, err)
  289. return
  290. }
  291. if !alive {
  292. ec, exitedAt, err = daemon.containerd.DeleteTask(context.Background(), c.ID)
  293. if err != nil && !errdefs.IsNotFound(err) {
  294. logrus.WithError(err).Errorf("Failed to delete container %s from containerd", c.ID)
  295. return
  296. }
  297. } else if !daemon.configStore.LiveRestoreEnabled {
  298. if err := daemon.kill(c, c.StopSignal()); err != nil && !errdefs.IsNotFound(err) {
  299. logrus.WithError(err).WithField("container", c.ID).Error("error shutting down container")
  300. return
  301. }
  302. }
  303. if c.IsRunning() || c.IsPaused() {
  304. c.RestartManager().Cancel() // manually start containers because some need to wait for swarm networking
  305. if c.IsPaused() && alive {
  306. s, err := daemon.containerd.Status(context.Background(), c.ID)
  307. if err != nil {
  308. logrus.WithError(err).WithField("container", c.ID).
  309. Errorf("Failed to get container status")
  310. } else {
  311. logrus.WithField("container", c.ID).WithField("state", s).
  312. Info("restored container paused")
  313. switch s {
  314. case libcontainerd.StatusPaused, libcontainerd.StatusPausing:
  315. // nothing to do
  316. case libcontainerd.StatusStopped:
  317. alive = false
  318. case libcontainerd.StatusUnknown:
  319. logrus.WithField("container", c.ID).
  320. Error("Unknown status for container during restore")
  321. default:
  322. // running
  323. c.Lock()
  324. c.Paused = false
  325. daemon.setStateCounter(c)
  326. if err := c.CheckpointTo(daemon.containersReplica); err != nil {
  327. logrus.WithError(err).WithField("container", c.ID).
  328. Error("Failed to update stopped container state")
  329. }
  330. c.Unlock()
  331. }
  332. }
  333. }
  334. if !alive {
  335. c.Lock()
  336. c.SetStopped(&container.ExitStatus{ExitCode: int(ec), ExitedAt: exitedAt})
  337. daemon.Cleanup(c)
  338. if err := c.CheckpointTo(daemon.containersReplica); err != nil {
  339. logrus.Errorf("Failed to update stopped container %s state: %v", c.ID, err)
  340. }
  341. c.Unlock()
  342. }
  343. // we call Mount and then Unmount to get BaseFs of the container
  344. if err := daemon.Mount(c); err != nil {
  345. // The mount is unlikely to fail. However, in case mount fails
  346. // the container should be allowed to restore here. Some functionalities
  347. // (like docker exec -u user) might be missing but container is able to be
  348. // stopped/restarted/removed.
  349. // See #29365 for related information.
  350. // The error is only logged here.
  351. logrus.Warnf("Failed to mount container on getting BaseFs path %v: %v", c.ID, err)
  352. } else {
  353. if err := daemon.Unmount(c); err != nil {
  354. logrus.Warnf("Failed to umount container on getting BaseFs path %v: %v", c.ID, err)
  355. }
  356. }
  357. c.ResetRestartManager(false)
  358. if !c.HostConfig.NetworkMode.IsContainer() && c.IsRunning() {
  359. options, err := daemon.buildSandboxOptions(c)
  360. if err != nil {
  361. logrus.Warnf("Failed build sandbox option to restore container %s: %v", c.ID, err)
  362. }
  363. mapLock.Lock()
  364. activeSandboxes[c.NetworkSettings.SandboxID] = options
  365. mapLock.Unlock()
  366. }
  367. }
  368. // get list of containers we need to restart
  369. // Do not autostart containers which
  370. // has endpoints in a swarm scope
  371. // network yet since the cluster is
  372. // not initialized yet. We will start
  373. // it after the cluster is
  374. // initialized.
  375. if daemon.configStore.AutoRestart && c.ShouldRestart() && !c.NetworkSettings.HasSwarmEndpoint && c.HasBeenStartedBefore {
  376. mapLock.Lock()
  377. restartContainers[c] = make(chan struct{})
  378. mapLock.Unlock()
  379. } else if c.HostConfig != nil && c.HostConfig.AutoRemove {
  380. mapLock.Lock()
  381. removeContainers[c.ID] = c
  382. mapLock.Unlock()
  383. }
  384. c.Lock()
  385. if c.RemovalInProgress {
  386. // We probably crashed in the middle of a removal, reset
  387. // the flag.
  388. //
  389. // We DO NOT remove the container here as we do not
  390. // know if the user had requested for either the
  391. // associated volumes, network links or both to also
  392. // be removed. So we put the container in the "dead"
  393. // state and leave further processing up to them.
  394. logrus.Debugf("Resetting RemovalInProgress flag from %v", c.ID)
  395. c.RemovalInProgress = false
  396. c.Dead = true
  397. if err := c.CheckpointTo(daemon.containersReplica); err != nil {
  398. logrus.Errorf("Failed to update RemovalInProgress container %s state: %v", c.ID, err)
  399. }
  400. }
  401. c.Unlock()
  402. }(c)
  403. }
  404. group.Wait()
  405. daemon.netController, err = daemon.initNetworkController(daemon.configStore, activeSandboxes)
  406. if err != nil {
  407. return fmt.Errorf("Error initializing network controller: %v", err)
  408. }
  409. // Now that all the containers are registered, register the links
  410. for _, c := range containers {
  411. group.Add(1)
  412. go func(c *container.Container) {
  413. _ = sem.Acquire(context.Background(), 1)
  414. if err := daemon.registerLinks(c, c.HostConfig); err != nil {
  415. logrus.Errorf("failed to register link for container %s: %v", c.ID, err)
  416. }
  417. sem.Release(1)
  418. group.Done()
  419. }(c)
  420. }
  421. group.Wait()
  422. for c, notifier := range restartContainers {
  423. group.Add(1)
  424. go func(c *container.Container, chNotify chan struct{}) {
  425. _ = sem.Acquire(context.Background(), 1)
  426. logrus.Debugf("Starting container %s", c.ID)
  427. // ignore errors here as this is a best effort to wait for children to be
  428. // running before we try to start the container
  429. children := daemon.children(c)
  430. timeout := time.After(5 * time.Second)
  431. for _, child := range children {
  432. if notifier, exists := restartContainers[child]; exists {
  433. select {
  434. case <-notifier:
  435. case <-timeout:
  436. }
  437. }
  438. }
  439. // Make sure networks are available before starting
  440. daemon.waitForNetworks(c)
  441. if err := daemon.containerStart(c, "", "", true); err != nil {
  442. logrus.Errorf("Failed to start container %s: %s", c.ID, err)
  443. }
  444. close(chNotify)
  445. sem.Release(1)
  446. group.Done()
  447. }(c, notifier)
  448. }
  449. group.Wait()
  450. for id := range removeContainers {
  451. group.Add(1)
  452. go func(cid string) {
  453. _ = sem.Acquire(context.Background(), 1)
  454. if err := daemon.ContainerRm(cid, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil {
  455. logrus.Errorf("Failed to remove container %s: %s", cid, err)
  456. }
  457. sem.Release(1)
  458. group.Done()
  459. }(id)
  460. }
  461. group.Wait()
  462. // any containers that were started above would already have had this done,
  463. // however we need to now prepare the mountpoints for the rest of the containers as well.
  464. // This shouldn't cause any issue running on the containers that already had this run.
  465. // This must be run after any containers with a restart policy so that containerized plugins
  466. // can have a chance to be running before we try to initialize them.
  467. for _, c := range containers {
  468. // if the container has restart policy, do not
  469. // prepare the mountpoints since it has been done on restarting.
  470. // This is to speed up the daemon start when a restart container
  471. // has a volume and the volume driver is not available.
  472. if _, ok := restartContainers[c]; ok {
  473. continue
  474. } else if _, ok := removeContainers[c.ID]; ok {
  475. // container is automatically removed, skip it.
  476. continue
  477. }
  478. group.Add(1)
  479. go func(c *container.Container) {
  480. _ = sem.Acquire(context.Background(), 1)
  481. if err := daemon.prepareMountPoints(c); err != nil {
  482. logrus.Error(err)
  483. }
  484. sem.Release(1)
  485. group.Done()
  486. }(c)
  487. }
  488. group.Wait()
  489. logrus.Info("Loading containers: done.")
  490. return nil
  491. }
  492. // RestartSwarmContainers restarts any autostart container which has a
  493. // swarm endpoint.
  494. func (daemon *Daemon) RestartSwarmContainers() {
  495. ctx := context.Background()
  496. // parallelLimit is the maximum number of parallel startup jobs that we
  497. // allow (this is the limited used for all startup semaphores). The multipler
  498. // (128) was chosen after some fairly significant benchmarking -- don't change
  499. // it unless you've tested it significantly (this value is adjusted if
  500. // RLIMIT_NOFILE is small to avoid EMFILE).
  501. parallelLimit := adjustParallelLimit(len(daemon.List()), 128*runtime.NumCPU())
  502. var group sync.WaitGroup
  503. sem := semaphore.NewWeighted(int64(parallelLimit))
  504. for _, c := range daemon.List() {
  505. if !c.IsRunning() && !c.IsPaused() {
  506. // Autostart all the containers which has a
  507. // swarm endpoint now that the cluster is
  508. // initialized.
  509. if daemon.configStore.AutoRestart && c.ShouldRestart() && c.NetworkSettings.HasSwarmEndpoint && c.HasBeenStartedBefore {
  510. group.Add(1)
  511. go func(c *container.Container) {
  512. if err := sem.Acquire(ctx, 1); err != nil {
  513. // ctx is done.
  514. group.Done()
  515. return
  516. }
  517. if err := daemon.containerStart(c, "", "", true); err != nil {
  518. logrus.Error(err)
  519. }
  520. sem.Release(1)
  521. group.Done()
  522. }(c)
  523. }
  524. }
  525. }
  526. group.Wait()
  527. }
  528. // waitForNetworks is used during daemon initialization when starting up containers
  529. // It ensures that all of a container's networks are available before the daemon tries to start the container.
  530. // In practice it just makes sure the discovery service is available for containers which use a network that require discovery.
  531. func (daemon *Daemon) waitForNetworks(c *container.Container) {
  532. if daemon.discoveryWatcher == nil {
  533. return
  534. }
  535. // Make sure if the container has a network that requires discovery that the discovery service is available before starting
  536. for netName := range c.NetworkSettings.Networks {
  537. // If we get `ErrNoSuchNetwork` here, we can assume that it is due to discovery not being ready
  538. // Most likely this is because the K/V store used for discovery is in a container and needs to be started
  539. if _, err := daemon.netController.NetworkByName(netName); err != nil {
  540. if _, ok := err.(libnetwork.ErrNoSuchNetwork); !ok {
  541. continue
  542. }
  543. // use a longish timeout here due to some slowdowns in libnetwork if the k/v store is on anything other than --net=host
  544. // FIXME: why is this slow???
  545. logrus.Debugf("Container %s waiting for network to be ready", c.Name)
  546. select {
  547. case <-daemon.discoveryWatcher.ReadyCh():
  548. case <-time.After(60 * time.Second):
  549. }
  550. return
  551. }
  552. }
  553. }
  554. func (daemon *Daemon) children(c *container.Container) map[string]*container.Container {
  555. return daemon.linkIndex.children(c)
  556. }
  557. // parents returns the names of the parent containers of the container
  558. // with the given name.
  559. func (daemon *Daemon) parents(c *container.Container) map[string]*container.Container {
  560. return daemon.linkIndex.parents(c)
  561. }
  562. func (daemon *Daemon) registerLink(parent, child *container.Container, alias string) error {
  563. fullName := path.Join(parent.Name, alias)
  564. if err := daemon.containersReplica.ReserveName(fullName, child.ID); err != nil {
  565. if err == container.ErrNameReserved {
  566. logrus.Warnf("error registering link for %s, to %s, as alias %s, ignoring: %v", parent.ID, child.ID, alias, err)
  567. return nil
  568. }
  569. return err
  570. }
  571. daemon.linkIndex.link(parent, child, fullName)
  572. return nil
  573. }
  574. // DaemonJoinsCluster informs the daemon has joined the cluster and provides
  575. // the handler to query the cluster component
  576. func (daemon *Daemon) DaemonJoinsCluster(clusterProvider cluster.Provider) {
  577. daemon.setClusterProvider(clusterProvider)
  578. }
  579. // DaemonLeavesCluster informs the daemon has left the cluster
  580. func (daemon *Daemon) DaemonLeavesCluster() {
  581. // Daemon is in charge of removing the attachable networks with
  582. // connected containers when the node leaves the swarm
  583. daemon.clearAttachableNetworks()
  584. // We no longer need the cluster provider, stop it now so that
  585. // the network agent will stop listening to cluster events.
  586. daemon.setClusterProvider(nil)
  587. // Wait for the networking cluster agent to stop
  588. daemon.netController.AgentStopWait()
  589. // Daemon is in charge of removing the ingress network when the
  590. // node leaves the swarm. Wait for job to be done or timeout.
  591. // This is called also on graceful daemon shutdown. We need to
  592. // wait, because the ingress release has to happen before the
  593. // network controller is stopped.
  594. if done, err := daemon.ReleaseIngress(); err == nil {
  595. select {
  596. case <-done:
  597. case <-time.After(5 * time.Second):
  598. logrus.Warn("timeout while waiting for ingress network removal")
  599. }
  600. } else {
  601. logrus.Warnf("failed to initiate ingress network removal: %v", err)
  602. }
  603. daemon.attachmentStore.ClearAttachments()
  604. }
  605. // setClusterProvider sets a component for querying the current cluster state.
  606. func (daemon *Daemon) setClusterProvider(clusterProvider cluster.Provider) {
  607. daemon.clusterProvider = clusterProvider
  608. daemon.netController.SetClusterProvider(clusterProvider)
  609. daemon.attachableNetworkLock = locker.New()
  610. }
  611. // IsSwarmCompatible verifies if the current daemon
  612. // configuration is compatible with the swarm mode
  613. func (daemon *Daemon) IsSwarmCompatible() error {
  614. if daemon.configStore == nil {
  615. return nil
  616. }
  617. return daemon.configStore.IsSwarmCompatible()
  618. }
  619. // NewDaemon sets up everything for the daemon to be able to service
  620. // requests from the webserver.
  621. func NewDaemon(ctx context.Context, config *config.Config, pluginStore *plugin.Store) (daemon *Daemon, err error) {
  622. setDefaultMtu(config)
  623. registryService, err := registry.NewService(config.ServiceOptions)
  624. if err != nil {
  625. return nil, err
  626. }
  627. // Ensure that we have a correct root key limit for launching containers.
  628. if err := ModifyRootKeyLimit(); err != nil {
  629. logrus.Warnf("unable to modify root key limit, number of containers could be limited by this quota: %v", err)
  630. }
  631. // Ensure we have compatible and valid configuration options
  632. if err := verifyDaemonSettings(config); err != nil {
  633. return nil, err
  634. }
  635. // Do we have a disabled network?
  636. config.DisableBridge = isBridgeNetworkDisabled(config)
  637. // Setup the resolv.conf
  638. setupResolvConf(config)
  639. // Verify the platform is supported as a daemon
  640. if !platformSupported {
  641. return nil, errSystemNotSupported
  642. }
  643. // Validate platform-specific requirements
  644. if err := checkSystem(); err != nil {
  645. return nil, err
  646. }
  647. idMapping, err := setupRemappedRoot(config)
  648. if err != nil {
  649. return nil, err
  650. }
  651. rootIDs := idMapping.RootPair()
  652. if err := setupDaemonProcess(config); err != nil {
  653. return nil, err
  654. }
  655. // set up the tmpDir to use a canonical path
  656. tmp, err := prepareTempDir(config.Root, rootIDs)
  657. if err != nil {
  658. return nil, fmt.Errorf("Unable to get the TempDir under %s: %s", config.Root, err)
  659. }
  660. realTmp, err := getRealPath(tmp)
  661. if err != nil {
  662. return nil, fmt.Errorf("Unable to get the full path to the TempDir (%s): %s", tmp, err)
  663. }
  664. if runtime.GOOS == "windows" {
  665. if _, err := os.Stat(realTmp); err != nil && os.IsNotExist(err) {
  666. if err := system.MkdirAll(realTmp, 0700, ""); err != nil {
  667. return nil, fmt.Errorf("Unable to create the TempDir (%s): %s", realTmp, err)
  668. }
  669. }
  670. os.Setenv("TEMP", realTmp)
  671. os.Setenv("TMP", realTmp)
  672. } else {
  673. os.Setenv("TMPDIR", realTmp)
  674. }
  675. d := &Daemon{
  676. configStore: config,
  677. PluginStore: pluginStore,
  678. startupDone: make(chan struct{}),
  679. }
  680. // Ensure the daemon is properly shutdown if there is a failure during
  681. // initialization
  682. defer func() {
  683. if err != nil {
  684. if err := d.Shutdown(); err != nil {
  685. logrus.Error(err)
  686. }
  687. }
  688. }()
  689. if err := d.setGenericResources(config); err != nil {
  690. return nil, err
  691. }
  692. // set up SIGUSR1 handler on Unix-like systems, or a Win32 global event
  693. // on Windows to dump Go routine stacks
  694. stackDumpDir := config.Root
  695. if execRoot := config.GetExecRoot(); execRoot != "" {
  696. stackDumpDir = execRoot
  697. }
  698. d.setupDumpStackTrap(stackDumpDir)
  699. if err := d.setupSeccompProfile(); err != nil {
  700. return nil, err
  701. }
  702. // Set the default isolation mode (only applicable on Windows)
  703. if err := d.setDefaultIsolation(); err != nil {
  704. return nil, fmt.Errorf("error setting default isolation mode: %v", err)
  705. }
  706. if err := configureMaxThreads(config); err != nil {
  707. logrus.Warnf("Failed to configure golang's threads limit: %v", err)
  708. }
  709. // ensureDefaultAppArmorProfile does nothing if apparmor is disabled
  710. if err := ensureDefaultAppArmorProfile(); err != nil {
  711. logrus.Errorf(err.Error())
  712. }
  713. daemonRepo := filepath.Join(config.Root, "containers")
  714. if err := idtools.MkdirAllAndChown(daemonRepo, 0700, rootIDs); err != nil {
  715. return nil, err
  716. }
  717. // Create the directory where we'll store the runtime scripts (i.e. in
  718. // order to support runtimeArgs)
  719. daemonRuntimes := filepath.Join(config.Root, "runtimes")
  720. if err := system.MkdirAll(daemonRuntimes, 0700, ""); err != nil {
  721. return nil, err
  722. }
  723. if err := d.loadRuntimes(); err != nil {
  724. return nil, err
  725. }
  726. if runtime.GOOS == "windows" {
  727. if err := system.MkdirAll(filepath.Join(config.Root, "credentialspecs"), 0, ""); err != nil {
  728. return nil, err
  729. }
  730. }
  731. // On Windows we don't support the environment variable, or a user supplied graphdriver
  732. // as Windows has no choice in terms of which graphdrivers to use. It's a case of
  733. // running Windows containers on Windows - windowsfilter, running Linux containers on Windows,
  734. // lcow. Unix platforms however run a single graphdriver for all containers, and it can
  735. // be set through an environment variable, a daemon start parameter, or chosen through
  736. // initialization of the layerstore through driver priority order for example.
  737. d.graphDrivers = make(map[string]string)
  738. layerStores := make(map[string]layer.Store)
  739. if runtime.GOOS == "windows" {
  740. d.graphDrivers[runtime.GOOS] = "windowsfilter"
  741. if system.LCOWSupported() {
  742. d.graphDrivers["linux"] = "lcow"
  743. }
  744. } else {
  745. driverName := os.Getenv("DOCKER_DRIVER")
  746. if driverName == "" {
  747. driverName = config.GraphDriver
  748. } else {
  749. logrus.Infof("Setting the storage driver from the $DOCKER_DRIVER environment variable (%s)", driverName)
  750. }
  751. d.graphDrivers[runtime.GOOS] = driverName // May still be empty. Layerstore init determines instead.
  752. }
  753. d.RegistryService = registryService
  754. logger.RegisterPluginGetter(d.PluginStore)
  755. metricsSockPath, err := d.listenMetricsSock()
  756. if err != nil {
  757. return nil, err
  758. }
  759. registerMetricsPluginCallback(d.PluginStore, metricsSockPath)
  760. gopts := []grpc.DialOption{
  761. grpc.WithInsecure(),
  762. grpc.WithBackoffMaxDelay(3 * time.Second),
  763. grpc.WithDialer(dialer.Dialer),
  764. // TODO(stevvooe): We may need to allow configuration of this on the client.
  765. grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(defaults.DefaultMaxRecvMsgSize)),
  766. grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(defaults.DefaultMaxSendMsgSize)),
  767. }
  768. if config.ContainerdAddr != "" {
  769. d.containerdCli, err = containerd.New(config.ContainerdAddr, containerd.WithDefaultNamespace(ContainersNamespace), containerd.WithDialOpts(gopts), containerd.WithTimeout(60*time.Second))
  770. if err != nil {
  771. return nil, errors.Wrapf(err, "failed to dial %q", config.ContainerdAddr)
  772. }
  773. }
  774. createPluginExec := func(m *plugin.Manager) (plugin.Executor, error) {
  775. var pluginCli *containerd.Client
  776. // Windows is not currently using containerd, keep the
  777. // client as nil
  778. if config.ContainerdAddr != "" {
  779. pluginCli, err = containerd.New(config.ContainerdAddr, containerd.WithDefaultNamespace(pluginexec.PluginNamespace), containerd.WithDialOpts(gopts), containerd.WithTimeout(60*time.Second))
  780. if err != nil {
  781. return nil, errors.Wrapf(err, "failed to dial %q", config.ContainerdAddr)
  782. }
  783. }
  784. return pluginexec.New(ctx, getPluginExecRoot(config.Root), pluginCli, m)
  785. }
  786. // Plugin system initialization should happen before restore. Do not change order.
  787. d.pluginManager, err = plugin.NewManager(plugin.ManagerConfig{
  788. Root: filepath.Join(config.Root, "plugins"),
  789. ExecRoot: getPluginExecRoot(config.Root),
  790. Store: d.PluginStore,
  791. CreateExecutor: createPluginExec,
  792. RegistryService: registryService,
  793. LiveRestoreEnabled: config.LiveRestoreEnabled,
  794. LogPluginEvent: d.LogPluginEvent, // todo: make private
  795. AuthzMiddleware: config.AuthzMiddleware,
  796. })
  797. if err != nil {
  798. return nil, errors.Wrap(err, "couldn't create plugin manager")
  799. }
  800. if err := d.setupDefaultLogConfig(); err != nil {
  801. return nil, err
  802. }
  803. for operatingSystem, gd := range d.graphDrivers {
  804. layerStores[operatingSystem], err = layer.NewStoreFromOptions(layer.StoreOptions{
  805. Root: config.Root,
  806. MetadataStorePathTemplate: filepath.Join(config.Root, "image", "%s", "layerdb"),
  807. GraphDriver: gd,
  808. GraphDriverOptions: config.GraphOptions,
  809. IDMapping: idMapping,
  810. PluginGetter: d.PluginStore,
  811. ExperimentalEnabled: config.Experimental,
  812. OS: operatingSystem,
  813. })
  814. if err != nil {
  815. return nil, err
  816. }
  817. }
  818. // As layerstore initialization may set the driver
  819. for os := range d.graphDrivers {
  820. d.graphDrivers[os] = layerStores[os].DriverName()
  821. }
  822. // Configure and validate the kernels security support. Note this is a Linux/FreeBSD
  823. // operation only, so it is safe to pass *just* the runtime OS graphdriver.
  824. if err := configureKernelSecuritySupport(config, d.graphDrivers[runtime.GOOS]); err != nil {
  825. return nil, err
  826. }
  827. imageRoot := filepath.Join(config.Root, "image", d.graphDrivers[runtime.GOOS])
  828. ifs, err := image.NewFSStoreBackend(filepath.Join(imageRoot, "imagedb"))
  829. if err != nil {
  830. return nil, err
  831. }
  832. lgrMap := make(map[string]image.LayerGetReleaser)
  833. for os, ls := range layerStores {
  834. lgrMap[os] = ls
  835. }
  836. imageStore, err := image.NewImageStore(ifs, lgrMap)
  837. if err != nil {
  838. return nil, err
  839. }
  840. d.volumes, err = volumesservice.NewVolumeService(config.Root, d.PluginStore, rootIDs, d)
  841. if err != nil {
  842. return nil, err
  843. }
  844. trustKey, err := loadOrCreateTrustKey(config.TrustKeyPath)
  845. if err != nil {
  846. return nil, err
  847. }
  848. trustDir := filepath.Join(config.Root, "trust")
  849. if err := system.MkdirAll(trustDir, 0700, ""); err != nil {
  850. return nil, err
  851. }
  852. // We have a single tag/reference store for the daemon globally. However, it's
  853. // stored under the graphdriver. On host platforms which only support a single
  854. // container OS, but multiple selectable graphdrivers, this means depending on which
  855. // graphdriver is chosen, the global reference store is under there. For
  856. // platforms which support multiple container operating systems, this is slightly
  857. // more problematic as where does the global ref store get located? Fortunately,
  858. // for Windows, which is currently the only daemon supporting multiple container
  859. // operating systems, the list of graphdrivers available isn't user configurable.
  860. // For backwards compatibility, we just put it under the windowsfilter
  861. // directory regardless.
  862. refStoreLocation := filepath.Join(imageRoot, `repositories.json`)
  863. rs, err := refstore.NewReferenceStore(refStoreLocation)
  864. if err != nil {
  865. return nil, fmt.Errorf("Couldn't create reference store repository: %s", err)
  866. }
  867. distributionMetadataStore, err := dmetadata.NewFSMetadataStore(filepath.Join(imageRoot, "distribution"))
  868. if err != nil {
  869. return nil, err
  870. }
  871. // Discovery is only enabled when the daemon is launched with an address to advertise. When
  872. // initialized, the daemon is registered and we can store the discovery backend as it's read-only
  873. if err := d.initDiscovery(config); err != nil {
  874. return nil, err
  875. }
  876. sysInfo := sysinfo.New(false)
  877. // Check if Devices cgroup is mounted, it is hard requirement for container security,
  878. // on Linux.
  879. if runtime.GOOS == "linux" && !sysInfo.CgroupDevicesEnabled {
  880. return nil, errors.New("Devices cgroup isn't mounted")
  881. }
  882. d.ID = trustKey.PublicKey().KeyID()
  883. d.repository = daemonRepo
  884. d.containers = container.NewMemoryStore()
  885. if d.containersReplica, err = container.NewViewDB(); err != nil {
  886. return nil, err
  887. }
  888. d.execCommands = exec.NewStore()
  889. d.idIndex = truncindex.NewTruncIndex([]string{})
  890. d.statsCollector = d.newStatsCollector(1 * time.Second)
  891. d.EventsService = events.New()
  892. d.root = config.Root
  893. d.idMapping = idMapping
  894. d.seccompEnabled = sysInfo.Seccomp
  895. d.apparmorEnabled = sysInfo.AppArmor
  896. d.linkIndex = newLinkIndex()
  897. // TODO: imageStore, distributionMetadataStore, and ReferenceStore are only
  898. // used above to run migration. They could be initialized in ImageService
  899. // if migration is called from daemon/images. layerStore might move as well.
  900. d.imageService = images.NewImageService(images.ImageServiceConfig{
  901. ContainerStore: d.containers,
  902. DistributionMetadataStore: distributionMetadataStore,
  903. EventsService: d.EventsService,
  904. ImageStore: imageStore,
  905. LayerStores: layerStores,
  906. MaxConcurrentDownloads: *config.MaxConcurrentDownloads,
  907. MaxConcurrentUploads: *config.MaxConcurrentUploads,
  908. ReferenceStore: rs,
  909. RegistryService: registryService,
  910. TrustKey: trustKey,
  911. })
  912. go d.execCommandGC()
  913. d.containerd, err = libcontainerd.NewClient(ctx, d.containerdCli, filepath.Join(config.ExecRoot, "containerd"), ContainersNamespace, d)
  914. if err != nil {
  915. return nil, err
  916. }
  917. if err := d.restore(); err != nil {
  918. return nil, err
  919. }
  920. close(d.startupDone)
  921. // FIXME: this method never returns an error
  922. info, _ := d.SystemInfo()
  923. engineInfo.WithValues(
  924. dockerversion.Version,
  925. dockerversion.GitCommit,
  926. info.Architecture,
  927. info.Driver,
  928. info.KernelVersion,
  929. info.OperatingSystem,
  930. info.OSType,
  931. info.ID,
  932. ).Set(1)
  933. engineCpus.Set(float64(info.NCPU))
  934. engineMemory.Set(float64(info.MemTotal))
  935. gd := ""
  936. for os, driver := range d.graphDrivers {
  937. if len(gd) > 0 {
  938. gd += ", "
  939. }
  940. gd += driver
  941. if len(d.graphDrivers) > 1 {
  942. gd = fmt.Sprintf("%s (%s)", gd, os)
  943. }
  944. }
  945. logrus.WithFields(logrus.Fields{
  946. "version": dockerversion.Version,
  947. "commit": dockerversion.GitCommit,
  948. "graphdriver(s)": gd,
  949. }).Info("Docker daemon")
  950. return d, nil
  951. }
  952. // DistributionServices returns services controlling daemon storage
  953. func (daemon *Daemon) DistributionServices() images.DistributionServices {
  954. return daemon.imageService.DistributionServices()
  955. }
  956. func (daemon *Daemon) waitForStartupDone() {
  957. <-daemon.startupDone
  958. }
  959. func (daemon *Daemon) shutdownContainer(c *container.Container) error {
  960. stopTimeout := c.StopTimeout()
  961. // If container failed to exit in stopTimeout seconds of SIGTERM, then using the force
  962. if err := daemon.containerStop(c, stopTimeout); err != nil {
  963. return fmt.Errorf("Failed to stop container %s with error: %v", c.ID, err)
  964. }
  965. // Wait without timeout for the container to exit.
  966. // Ignore the result.
  967. <-c.Wait(context.Background(), container.WaitConditionNotRunning)
  968. return nil
  969. }
  970. // ShutdownTimeout returns the timeout (in seconds) before containers are forcibly
  971. // killed during shutdown. The default timeout can be configured both on the daemon
  972. // and per container, and the longest timeout will be used. A grace-period of
  973. // 5 seconds is added to the configured timeout.
  974. //
  975. // A negative (-1) timeout means "indefinitely", which means that containers
  976. // are not forcibly killed, and the daemon shuts down after all containers exit.
  977. func (daemon *Daemon) ShutdownTimeout() int {
  978. shutdownTimeout := daemon.configStore.ShutdownTimeout
  979. if shutdownTimeout < 0 {
  980. return -1
  981. }
  982. if daemon.containers == nil {
  983. return shutdownTimeout
  984. }
  985. graceTimeout := 5
  986. for _, c := range daemon.containers.List() {
  987. stopTimeout := c.StopTimeout()
  988. if stopTimeout < 0 {
  989. return -1
  990. }
  991. if stopTimeout+graceTimeout > shutdownTimeout {
  992. shutdownTimeout = stopTimeout + graceTimeout
  993. }
  994. }
  995. return shutdownTimeout
  996. }
  997. // Shutdown stops the daemon.
  998. func (daemon *Daemon) Shutdown() error {
  999. daemon.shutdown = true
  1000. // Keep mounts and networking running on daemon shutdown if
  1001. // we are to keep containers running and restore them.
  1002. if daemon.configStore.LiveRestoreEnabled && daemon.containers != nil {
  1003. // check if there are any running containers, if none we should do some cleanup
  1004. if ls, err := daemon.Containers(&types.ContainerListOptions{}); len(ls) != 0 || err != nil {
  1005. // metrics plugins still need some cleanup
  1006. daemon.cleanupMetricsPlugins()
  1007. return nil
  1008. }
  1009. }
  1010. if daemon.containers != nil {
  1011. logrus.Debugf("daemon configured with a %d seconds minimum shutdown timeout", daemon.configStore.ShutdownTimeout)
  1012. logrus.Debugf("start clean shutdown of all containers with a %d seconds timeout...", daemon.ShutdownTimeout())
  1013. daemon.containers.ApplyAll(func(c *container.Container) {
  1014. if !c.IsRunning() {
  1015. return
  1016. }
  1017. logrus.Debugf("stopping %s", c.ID)
  1018. if err := daemon.shutdownContainer(c); err != nil {
  1019. logrus.Errorf("Stop container error: %v", err)
  1020. return
  1021. }
  1022. if mountid, err := daemon.imageService.GetLayerMountID(c.ID, c.OS); err == nil {
  1023. daemon.cleanupMountsByID(mountid)
  1024. }
  1025. logrus.Debugf("container stopped %s", c.ID)
  1026. })
  1027. }
  1028. if daemon.volumes != nil {
  1029. if err := daemon.volumes.Shutdown(); err != nil {
  1030. logrus.Errorf("Error shutting down volume store: %v", err)
  1031. }
  1032. }
  1033. if daemon.imageService != nil {
  1034. daemon.imageService.Cleanup()
  1035. }
  1036. // If we are part of a cluster, clean up cluster's stuff
  1037. if daemon.clusterProvider != nil {
  1038. logrus.Debugf("start clean shutdown of cluster resources...")
  1039. daemon.DaemonLeavesCluster()
  1040. }
  1041. daemon.cleanupMetricsPlugins()
  1042. // Shutdown plugins after containers and layerstore. Don't change the order.
  1043. daemon.pluginShutdown()
  1044. // trigger libnetwork Stop only if it's initialized
  1045. if daemon.netController != nil {
  1046. daemon.netController.Stop()
  1047. }
  1048. if daemon.containerdCli != nil {
  1049. daemon.containerdCli.Close()
  1050. }
  1051. return daemon.cleanupMounts()
  1052. }
  1053. // Mount sets container.BaseFS
  1054. // (is it not set coming in? why is it unset?)
  1055. func (daemon *Daemon) Mount(container *container.Container) error {
  1056. if container.RWLayer == nil {
  1057. return errors.New("RWLayer of container " + container.ID + " is unexpectedly nil")
  1058. }
  1059. dir, err := container.RWLayer.Mount(container.GetMountLabel())
  1060. if err != nil {
  1061. return err
  1062. }
  1063. logrus.Debugf("container mounted via layerStore: %v", dir)
  1064. if container.BaseFS != nil && container.BaseFS.Path() != dir.Path() {
  1065. // The mount path reported by the graph driver should always be trusted on Windows, since the
  1066. // volume path for a given mounted layer may change over time. This should only be an error
  1067. // on non-Windows operating systems.
  1068. if runtime.GOOS != "windows" {
  1069. daemon.Unmount(container)
  1070. return fmt.Errorf("Error: driver %s is returning inconsistent paths for container %s ('%s' then '%s')",
  1071. daemon.imageService.GraphDriverForOS(container.OS), container.ID, container.BaseFS, dir)
  1072. }
  1073. }
  1074. container.BaseFS = dir // TODO: combine these fields
  1075. return nil
  1076. }
  1077. // Unmount unsets the container base filesystem
  1078. func (daemon *Daemon) Unmount(container *container.Container) error {
  1079. if container.RWLayer == nil {
  1080. return errors.New("RWLayer of container " + container.ID + " is unexpectedly nil")
  1081. }
  1082. if err := container.RWLayer.Unmount(); err != nil {
  1083. logrus.Errorf("Error unmounting container %s: %s", container.ID, err)
  1084. return err
  1085. }
  1086. return nil
  1087. }
  1088. // Subnets return the IPv4 and IPv6 subnets of networks that are manager by Docker.
  1089. func (daemon *Daemon) Subnets() ([]net.IPNet, []net.IPNet) {
  1090. var v4Subnets []net.IPNet
  1091. var v6Subnets []net.IPNet
  1092. managedNetworks := daemon.netController.Networks()
  1093. for _, managedNetwork := range managedNetworks {
  1094. v4infos, v6infos := managedNetwork.Info().IpamInfo()
  1095. for _, info := range v4infos {
  1096. if info.IPAMData.Pool != nil {
  1097. v4Subnets = append(v4Subnets, *info.IPAMData.Pool)
  1098. }
  1099. }
  1100. for _, info := range v6infos {
  1101. if info.IPAMData.Pool != nil {
  1102. v6Subnets = append(v6Subnets, *info.IPAMData.Pool)
  1103. }
  1104. }
  1105. }
  1106. return v4Subnets, v6Subnets
  1107. }
  1108. // prepareTempDir prepares and returns the default directory to use
  1109. // for temporary files.
  1110. // If it doesn't exist, it is created. If it exists, its content is removed.
  1111. func prepareTempDir(rootDir string, rootIdentity idtools.Identity) (string, error) {
  1112. var tmpDir string
  1113. if tmpDir = os.Getenv("DOCKER_TMPDIR"); tmpDir == "" {
  1114. tmpDir = filepath.Join(rootDir, "tmp")
  1115. newName := tmpDir + "-old"
  1116. if err := os.Rename(tmpDir, newName); err == nil {
  1117. go func() {
  1118. if err := os.RemoveAll(newName); err != nil {
  1119. logrus.Warnf("failed to delete old tmp directory: %s", newName)
  1120. }
  1121. }()
  1122. } else if !os.IsNotExist(err) {
  1123. logrus.Warnf("failed to rename %s for background deletion: %s. Deleting synchronously", tmpDir, err)
  1124. if err := os.RemoveAll(tmpDir); err != nil {
  1125. logrus.Warnf("failed to delete old tmp directory: %s", tmpDir)
  1126. }
  1127. }
  1128. }
  1129. // We don't remove the content of tmpdir if it's not the default,
  1130. // it may hold things that do not belong to us.
  1131. return tmpDir, idtools.MkdirAllAndChown(tmpDir, 0700, rootIdentity)
  1132. }
  1133. func (daemon *Daemon) setGenericResources(conf *config.Config) error {
  1134. genericResources, err := config.ParseGenericResources(conf.NodeGenericResources)
  1135. if err != nil {
  1136. return err
  1137. }
  1138. daemon.genericResources = genericResources
  1139. return nil
  1140. }
  1141. func setDefaultMtu(conf *config.Config) {
  1142. // do nothing if the config does not have the default 0 value.
  1143. if conf.Mtu != 0 {
  1144. return
  1145. }
  1146. conf.Mtu = config.DefaultNetworkMtu
  1147. }
  1148. // IsShuttingDown tells whether the daemon is shutting down or not
  1149. func (daemon *Daemon) IsShuttingDown() bool {
  1150. return daemon.shutdown
  1151. }
  1152. // initDiscovery initializes the discovery watcher for this daemon.
  1153. func (daemon *Daemon) initDiscovery(conf *config.Config) error {
  1154. advertise, err := config.ParseClusterAdvertiseSettings(conf.ClusterStore, conf.ClusterAdvertise)
  1155. if err != nil {
  1156. if err == discovery.ErrDiscoveryDisabled {
  1157. return nil
  1158. }
  1159. return err
  1160. }
  1161. conf.ClusterAdvertise = advertise
  1162. discoveryWatcher, err := discovery.Init(conf.ClusterStore, conf.ClusterAdvertise, conf.ClusterOpts)
  1163. if err != nil {
  1164. return fmt.Errorf("discovery initialization failed (%v)", err)
  1165. }
  1166. daemon.discoveryWatcher = discoveryWatcher
  1167. return nil
  1168. }
  1169. func isBridgeNetworkDisabled(conf *config.Config) bool {
  1170. return conf.BridgeConfig.Iface == config.DisableNetworkBridge
  1171. }
  1172. func (daemon *Daemon) networkOptions(dconfig *config.Config, pg plugingetter.PluginGetter, activeSandboxes map[string]interface{}) ([]nwconfig.Option, error) {
  1173. options := []nwconfig.Option{}
  1174. if dconfig == nil {
  1175. return options, nil
  1176. }
  1177. options = append(options, nwconfig.OptionExperimental(dconfig.Experimental))
  1178. options = append(options, nwconfig.OptionDataDir(dconfig.Root))
  1179. options = append(options, nwconfig.OptionExecRoot(dconfig.GetExecRoot()))
  1180. dd := runconfig.DefaultDaemonNetworkMode()
  1181. dn := runconfig.DefaultDaemonNetworkMode().NetworkName()
  1182. options = append(options, nwconfig.OptionDefaultDriver(string(dd)))
  1183. options = append(options, nwconfig.OptionDefaultNetwork(dn))
  1184. if strings.TrimSpace(dconfig.ClusterStore) != "" {
  1185. kv := strings.Split(dconfig.ClusterStore, "://")
  1186. if len(kv) != 2 {
  1187. return nil, errors.New("kv store daemon config must be of the form KV-PROVIDER://KV-URL")
  1188. }
  1189. options = append(options, nwconfig.OptionKVProvider(kv[0]))
  1190. options = append(options, nwconfig.OptionKVProviderURL(kv[1]))
  1191. }
  1192. if len(dconfig.ClusterOpts) > 0 {
  1193. options = append(options, nwconfig.OptionKVOpts(dconfig.ClusterOpts))
  1194. }
  1195. if daemon.discoveryWatcher != nil {
  1196. options = append(options, nwconfig.OptionDiscoveryWatcher(daemon.discoveryWatcher))
  1197. }
  1198. if dconfig.ClusterAdvertise != "" {
  1199. options = append(options, nwconfig.OptionDiscoveryAddress(dconfig.ClusterAdvertise))
  1200. }
  1201. options = append(options, nwconfig.OptionLabels(dconfig.Labels))
  1202. options = append(options, driverOptions(dconfig)...)
  1203. if len(dconfig.NetworkConfig.DefaultAddressPools.Value()) > 0 {
  1204. options = append(options, nwconfig.OptionDefaultAddressPoolConfig(dconfig.NetworkConfig.DefaultAddressPools.Value()))
  1205. }
  1206. if daemon.configStore != nil && daemon.configStore.LiveRestoreEnabled && len(activeSandboxes) != 0 {
  1207. options = append(options, nwconfig.OptionActiveSandboxes(activeSandboxes))
  1208. }
  1209. if pg != nil {
  1210. options = append(options, nwconfig.OptionPluginGetter(pg))
  1211. }
  1212. options = append(options, nwconfig.OptionNetworkControlPlaneMTU(dconfig.NetworkControlPlaneMTU))
  1213. return options, nil
  1214. }
  1215. // GetCluster returns the cluster
  1216. func (daemon *Daemon) GetCluster() Cluster {
  1217. return daemon.cluster
  1218. }
  1219. // SetCluster sets the cluster
  1220. func (daemon *Daemon) SetCluster(cluster Cluster) {
  1221. daemon.cluster = cluster
  1222. }
  1223. func (daemon *Daemon) pluginShutdown() {
  1224. manager := daemon.pluginManager
  1225. // Check for a valid manager object. In error conditions, daemon init can fail
  1226. // and shutdown called, before plugin manager is initialized.
  1227. if manager != nil {
  1228. manager.Shutdown()
  1229. }
  1230. }
  1231. // PluginManager returns current pluginManager associated with the daemon
  1232. func (daemon *Daemon) PluginManager() *plugin.Manager { // set up before daemon to avoid this method
  1233. return daemon.pluginManager
  1234. }
  1235. // PluginGetter returns current pluginStore associated with the daemon
  1236. func (daemon *Daemon) PluginGetter() *plugin.Store {
  1237. return daemon.PluginStore
  1238. }
  1239. // CreateDaemonRoot creates the root for the daemon
  1240. func CreateDaemonRoot(config *config.Config) error {
  1241. // get the canonical path to the Docker root directory
  1242. var realRoot string
  1243. if _, err := os.Stat(config.Root); err != nil && os.IsNotExist(err) {
  1244. realRoot = config.Root
  1245. } else {
  1246. realRoot, err = getRealPath(config.Root)
  1247. if err != nil {
  1248. return fmt.Errorf("Unable to get the full path to root (%s): %s", config.Root, err)
  1249. }
  1250. }
  1251. idMapping, err := setupRemappedRoot(config)
  1252. if err != nil {
  1253. return err
  1254. }
  1255. return setupDaemonRoot(config, realRoot, idMapping.RootPair())
  1256. }
  1257. // checkpointAndSave grabs a container lock to safely call container.CheckpointTo
  1258. func (daemon *Daemon) checkpointAndSave(container *container.Container) error {
  1259. container.Lock()
  1260. defer container.Unlock()
  1261. if err := container.CheckpointTo(daemon.containersReplica); err != nil {
  1262. return fmt.Errorf("Error saving container state: %v", err)
  1263. }
  1264. return nil
  1265. }
  1266. // because the CLI sends a -1 when it wants to unset the swappiness value
  1267. // we need to clear it on the server side
  1268. func fixMemorySwappiness(resources *containertypes.Resources) {
  1269. if resources.MemorySwappiness != nil && *resources.MemorySwappiness == -1 {
  1270. resources.MemorySwappiness = nil
  1271. }
  1272. }
  1273. // GetAttachmentStore returns current attachment store associated with the daemon
  1274. func (daemon *Daemon) GetAttachmentStore() *network.AttachmentStore {
  1275. return &daemon.attachmentStore
  1276. }
  1277. // IdentityMapping returns uid/gid mapping or a SID (in the case of Windows) for the builder
  1278. func (daemon *Daemon) IdentityMapping() *idtools.IdentityMapping {
  1279. return daemon.idMapping
  1280. }
  1281. // ImageService returns the Daemon's ImageService
  1282. func (daemon *Daemon) ImageService() *images.ImageService {
  1283. return daemon.imageService
  1284. }
  1285. // BuilderBackend returns the backend used by builder
  1286. func (daemon *Daemon) BuilderBackend() builder.Backend {
  1287. return struct {
  1288. *Daemon
  1289. *images.ImageService
  1290. }{daemon, daemon.imageService}
  1291. }