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