daemon.go 50 KB

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