daemon.go 48 KB

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