daemon.go 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  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
  7. import (
  8. "errors"
  9. "fmt"
  10. "io"
  11. "io/ioutil"
  12. "net"
  13. "os"
  14. "path/filepath"
  15. "runtime"
  16. "strings"
  17. "sync"
  18. "syscall"
  19. "time"
  20. "github.com/Sirupsen/logrus"
  21. "github.com/docker/distribution/digest"
  22. "github.com/docker/docker/api"
  23. "github.com/docker/docker/container"
  24. "github.com/docker/docker/daemon/events"
  25. "github.com/docker/docker/daemon/exec"
  26. "github.com/docker/docker/daemon/execdriver"
  27. "github.com/docker/docker/daemon/execdriver/execdrivers"
  28. "github.com/docker/engine-api/types"
  29. containertypes "github.com/docker/engine-api/types/container"
  30. eventtypes "github.com/docker/engine-api/types/events"
  31. "github.com/docker/engine-api/types/filters"
  32. registrytypes "github.com/docker/engine-api/types/registry"
  33. "github.com/docker/engine-api/types/strslice"
  34. // register graph drivers
  35. _ "github.com/docker/docker/daemon/graphdriver/register"
  36. "github.com/docker/docker/daemon/logger"
  37. "github.com/docker/docker/daemon/network"
  38. "github.com/docker/docker/distribution"
  39. dmetadata "github.com/docker/docker/distribution/metadata"
  40. "github.com/docker/docker/distribution/xfer"
  41. derr "github.com/docker/docker/errors"
  42. "github.com/docker/docker/image"
  43. "github.com/docker/docker/image/tarexport"
  44. "github.com/docker/docker/layer"
  45. "github.com/docker/docker/migrate/v1"
  46. "github.com/docker/docker/pkg/archive"
  47. "github.com/docker/docker/pkg/discovery"
  48. "github.com/docker/docker/pkg/fileutils"
  49. "github.com/docker/docker/pkg/graphdb"
  50. "github.com/docker/docker/pkg/idtools"
  51. "github.com/docker/docker/pkg/mount"
  52. "github.com/docker/docker/pkg/namesgenerator"
  53. "github.com/docker/docker/pkg/progress"
  54. "github.com/docker/docker/pkg/signal"
  55. "github.com/docker/docker/pkg/streamformatter"
  56. "github.com/docker/docker/pkg/stringid"
  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/reference"
  61. "github.com/docker/docker/registry"
  62. "github.com/docker/docker/runconfig"
  63. "github.com/docker/docker/utils"
  64. volumedrivers "github.com/docker/docker/volume/drivers"
  65. "github.com/docker/docker/volume/local"
  66. "github.com/docker/docker/volume/store"
  67. "github.com/docker/go-connections/nat"
  68. "github.com/docker/libnetwork"
  69. lntypes "github.com/docker/libnetwork/types"
  70. "github.com/docker/libtrust"
  71. "github.com/opencontainers/runc/libcontainer"
  72. "golang.org/x/net/context"
  73. )
  74. const (
  75. // maxDownloadConcurrency is the maximum number of downloads that
  76. // may take place at a time for each pull.
  77. maxDownloadConcurrency = 3
  78. // maxUploadConcurrency is the maximum number of uploads that
  79. // may take place at a time for each push.
  80. maxUploadConcurrency = 5
  81. )
  82. var (
  83. validContainerNameChars = utils.RestrictedNameChars
  84. validContainerNamePattern = utils.RestrictedNamePattern
  85. errSystemNotSupported = errors.New("The Docker daemon is not supported on this platform.")
  86. )
  87. // ErrImageDoesNotExist is error returned when no image can be found for a reference.
  88. type ErrImageDoesNotExist struct {
  89. RefOrID string
  90. }
  91. func (e ErrImageDoesNotExist) Error() string {
  92. return fmt.Sprintf("no such id: %s", e.RefOrID)
  93. }
  94. type contStore struct {
  95. s map[string]*container.Container
  96. sync.Mutex
  97. }
  98. func (c *contStore) Add(id string, cont *container.Container) {
  99. c.Lock()
  100. c.s[id] = cont
  101. c.Unlock()
  102. }
  103. func (c *contStore) Get(id string) *container.Container {
  104. c.Lock()
  105. res := c.s[id]
  106. c.Unlock()
  107. return res
  108. }
  109. func (c *contStore) Delete(id string) {
  110. c.Lock()
  111. delete(c.s, id)
  112. c.Unlock()
  113. }
  114. func (c *contStore) List() []*container.Container {
  115. containers := new(History)
  116. c.Lock()
  117. for _, cont := range c.s {
  118. containers.Add(cont)
  119. }
  120. c.Unlock()
  121. containers.sort()
  122. return *containers
  123. }
  124. // Daemon holds information about the Docker daemon.
  125. type Daemon struct {
  126. ID string
  127. repository string
  128. containers *contStore
  129. execCommands *exec.Store
  130. referenceStore reference.Store
  131. downloadManager *xfer.LayerDownloadManager
  132. uploadManager *xfer.LayerUploadManager
  133. distributionMetadataStore dmetadata.Store
  134. trustKey libtrust.PrivateKey
  135. idIndex *truncindex.TruncIndex
  136. configStore *Config
  137. containerGraphDB *graphdb.Database
  138. execDriver execdriver.Driver
  139. statsCollector *statsCollector
  140. defaultLogConfig containertypes.LogConfig
  141. RegistryService *registry.Service
  142. EventsService *events.Events
  143. netController libnetwork.NetworkController
  144. volumes *store.VolumeStore
  145. discoveryWatcher discovery.Watcher
  146. root string
  147. shutdown bool
  148. uidMaps []idtools.IDMap
  149. gidMaps []idtools.IDMap
  150. layerStore layer.Store
  151. imageStore image.Store
  152. }
  153. // GetContainer looks for a container using the provided information, which could be
  154. // one of the following inputs from the caller:
  155. // - A full container ID, which will exact match a container in daemon's list
  156. // - A container name, which will only exact match via the GetByName() function
  157. // - A partial container ID prefix (e.g. short ID) of any length that is
  158. // unique enough to only return a single container object
  159. // If none of these searches succeed, an error is returned
  160. func (daemon *Daemon) GetContainer(prefixOrName string) (*container.Container, error) {
  161. if containerByID := daemon.containers.Get(prefixOrName); containerByID != nil {
  162. // prefix is an exact match to a full container ID
  163. return containerByID, nil
  164. }
  165. // GetByName will match only an exact name provided; we ignore errors
  166. if containerByName, _ := daemon.GetByName(prefixOrName); containerByName != nil {
  167. // prefix is an exact match to a full container Name
  168. return containerByName, nil
  169. }
  170. containerID, indexError := daemon.idIndex.Get(prefixOrName)
  171. if indexError != nil {
  172. // When truncindex defines an error type, use that instead
  173. if indexError == truncindex.ErrNotExist {
  174. return nil, derr.ErrorCodeNoSuchContainer.WithArgs(prefixOrName)
  175. }
  176. return nil, indexError
  177. }
  178. return daemon.containers.Get(containerID), nil
  179. }
  180. // Exists returns a true if a container of the specified ID or name exists,
  181. // false otherwise.
  182. func (daemon *Daemon) Exists(id string) bool {
  183. c, _ := daemon.GetContainer(id)
  184. return c != nil
  185. }
  186. // IsPaused returns a bool indicating if the specified container is paused.
  187. func (daemon *Daemon) IsPaused(id string) bool {
  188. c, _ := daemon.GetContainer(id)
  189. return c.State.IsPaused()
  190. }
  191. func (daemon *Daemon) containerRoot(id string) string {
  192. return filepath.Join(daemon.repository, id)
  193. }
  194. // Load reads the contents of a container from disk
  195. // This is typically done at startup.
  196. func (daemon *Daemon) load(id string) (*container.Container, error) {
  197. container := daemon.newBaseContainer(id)
  198. if err := container.FromDisk(); err != nil {
  199. return nil, err
  200. }
  201. if container.ID != id {
  202. return container, fmt.Errorf("Container %s is stored at %s", container.ID, id)
  203. }
  204. return container, nil
  205. }
  206. func (daemon *Daemon) registerName(container *container.Container) error {
  207. if daemon.Exists(container.ID) {
  208. return fmt.Errorf("Container is already loaded")
  209. }
  210. if err := validateID(container.ID); err != nil {
  211. return err
  212. }
  213. if container.Name == "" {
  214. name, err := daemon.generateNewName(container.ID)
  215. if err != nil {
  216. return err
  217. }
  218. container.Name = name
  219. if err := container.ToDiskLocking(); err != nil {
  220. logrus.Errorf("Error saving container name to disk: %v", err)
  221. }
  222. }
  223. return nil
  224. }
  225. // Register makes a container object usable by the daemon as <container.ID>
  226. func (daemon *Daemon) Register(container *container.Container) error {
  227. // Attach to stdout and stderr
  228. if container.Config.OpenStdin {
  229. container.NewInputPipes()
  230. } else {
  231. container.NewNopInputPipe()
  232. }
  233. daemon.containers.Add(container.ID, container)
  234. // don't update the Suffixarray if we're starting up
  235. // we'll waste time if we update it for every container
  236. daemon.idIndex.Add(container.ID)
  237. if container.IsRunning() {
  238. logrus.Debugf("killing old running container %s", container.ID)
  239. // Set exit code to 128 + SIGKILL (9) to properly represent unsuccessful exit
  240. container.SetStoppedLocking(&execdriver.ExitStatus{ExitCode: 137})
  241. // use the current driver and ensure that the container is dead x.x
  242. cmd := &execdriver.Command{
  243. CommonCommand: execdriver.CommonCommand{
  244. ID: container.ID,
  245. },
  246. }
  247. daemon.execDriver.Terminate(cmd)
  248. container.UnmountIpcMounts(mount.Unmount)
  249. daemon.Unmount(container)
  250. if err := container.ToDiskLocking(); err != nil {
  251. logrus.Errorf("Error saving stopped state to disk: %v", err)
  252. }
  253. }
  254. if err := daemon.prepareMountPoints(container); err != nil {
  255. return err
  256. }
  257. return nil
  258. }
  259. func (daemon *Daemon) restore() error {
  260. type cr struct {
  261. container *container.Container
  262. registered bool
  263. }
  264. var (
  265. debug = os.Getenv("DEBUG") != ""
  266. currentDriver = daemon.GraphDriverName()
  267. containers = make(map[string]*cr)
  268. )
  269. if !debug {
  270. logrus.Info("Loading containers: start.")
  271. }
  272. dir, err := ioutil.ReadDir(daemon.repository)
  273. if err != nil {
  274. return err
  275. }
  276. for _, v := range dir {
  277. id := v.Name()
  278. container, err := daemon.load(id)
  279. if !debug && logrus.GetLevel() == logrus.InfoLevel {
  280. fmt.Print(".")
  281. }
  282. if err != nil {
  283. logrus.Errorf("Failed to load container %v: %v", id, err)
  284. continue
  285. }
  286. rwlayer, err := daemon.layerStore.GetRWLayer(container.ID)
  287. if err != nil {
  288. logrus.Errorf("Failed to load container mount %v: %v", id, err)
  289. continue
  290. }
  291. container.RWLayer = rwlayer
  292. // Ignore the container if it does not support the current driver being used by the graph
  293. if (container.Driver == "" && currentDriver == "aufs") || container.Driver == currentDriver {
  294. logrus.Debugf("Loaded container %v", container.ID)
  295. containers[container.ID] = &cr{container: container}
  296. } else {
  297. logrus.Debugf("Cannot load container %s because it was created with another graph driver.", container.ID)
  298. }
  299. }
  300. if entities := daemon.containerGraphDB.List("/", -1); entities != nil {
  301. for _, p := range entities.Paths() {
  302. if !debug && logrus.GetLevel() == logrus.InfoLevel {
  303. fmt.Print(".")
  304. }
  305. e := entities[p]
  306. if c, ok := containers[e.ID()]; ok {
  307. c.registered = true
  308. }
  309. }
  310. }
  311. restartContainers := make(map[*container.Container]chan struct{})
  312. for _, c := range containers {
  313. if !c.registered {
  314. // Try to set the default name for a container if it exists prior to links
  315. c.container.Name, err = daemon.generateNewName(c.container.ID)
  316. if err != nil {
  317. logrus.Debugf("Setting default id - %s", err)
  318. }
  319. if err := daemon.registerName(c.container); err != nil {
  320. logrus.Errorf("Failed to register container %s: %s", c.container.ID, err)
  321. continue
  322. }
  323. }
  324. if err := daemon.Register(c.container); err != nil {
  325. logrus.Errorf("Failed to register container %s: %s", c.container.ID, err)
  326. continue
  327. }
  328. // get list of containers we need to restart
  329. if daemon.configStore.AutoRestart && c.container.ShouldRestart() {
  330. restartContainers[c.container] = make(chan struct{})
  331. }
  332. }
  333. group := sync.WaitGroup{}
  334. for c, notifier := range restartContainers {
  335. group.Add(1)
  336. go func(container *container.Container, chNotify chan struct{}) {
  337. defer group.Done()
  338. logrus.Debugf("Starting container %s", container.ID)
  339. // ignore errors here as this is a best effort to wait for children to be
  340. // running before we try to start the container
  341. children, err := daemon.children(container.Name)
  342. if err != nil {
  343. logrus.Warnf("error getting children for %s: %v", container.Name, err)
  344. }
  345. timeout := time.After(5 * time.Second)
  346. for _, child := range children {
  347. if notifier, exists := restartContainers[child]; exists {
  348. select {
  349. case <-notifier:
  350. case <-timeout:
  351. }
  352. }
  353. }
  354. if err := daemon.containerStart(container); err != nil {
  355. logrus.Errorf("Failed to start container %s: %s", container.ID, err)
  356. }
  357. close(chNotify)
  358. }(c, notifier)
  359. }
  360. group.Wait()
  361. if !debug {
  362. if logrus.GetLevel() == logrus.InfoLevel {
  363. fmt.Println()
  364. }
  365. logrus.Info("Loading containers: done.")
  366. }
  367. return nil
  368. }
  369. func (daemon *Daemon) mergeAndVerifyConfig(config *containertypes.Config, img *image.Image) error {
  370. if img != nil && img.Config != nil {
  371. if err := merge(config, img.Config); err != nil {
  372. return err
  373. }
  374. }
  375. if config.Entrypoint.Len() == 0 && config.Cmd.Len() == 0 {
  376. return fmt.Errorf("No command specified")
  377. }
  378. return nil
  379. }
  380. func (daemon *Daemon) generateIDAndName(name string) (string, string, error) {
  381. var (
  382. err error
  383. id = stringid.GenerateNonCryptoID()
  384. )
  385. if name == "" {
  386. if name, err = daemon.generateNewName(id); err != nil {
  387. return "", "", err
  388. }
  389. return id, name, nil
  390. }
  391. if name, err = daemon.reserveName(id, name); err != nil {
  392. return "", "", err
  393. }
  394. return id, name, nil
  395. }
  396. func (daemon *Daemon) reserveName(id, name string) (string, error) {
  397. if !validContainerNamePattern.MatchString(name) {
  398. return "", fmt.Errorf("Invalid container name (%s), only %s are allowed", name, validContainerNameChars)
  399. }
  400. if name[0] != '/' {
  401. name = "/" + name
  402. }
  403. if _, err := daemon.containerGraphDB.Set(name, id); err != nil {
  404. if !graphdb.IsNonUniqueNameError(err) {
  405. return "", err
  406. }
  407. conflictingContainer, err := daemon.GetByName(name)
  408. if err != nil {
  409. return "", err
  410. }
  411. return "", fmt.Errorf(
  412. "Conflict. The name %q is already in use by container %s. You have to remove (or rename) that container to be able to reuse that name.", strings.TrimPrefix(name, "/"),
  413. stringid.TruncateID(conflictingContainer.ID))
  414. }
  415. return name, nil
  416. }
  417. func (daemon *Daemon) generateNewName(id string) (string, error) {
  418. var name string
  419. for i := 0; i < 6; i++ {
  420. name = namesgenerator.GetRandomName(i)
  421. if name[0] != '/' {
  422. name = "/" + name
  423. }
  424. if _, err := daemon.containerGraphDB.Set(name, id); err != nil {
  425. if !graphdb.IsNonUniqueNameError(err) {
  426. return "", err
  427. }
  428. continue
  429. }
  430. return name, nil
  431. }
  432. name = "/" + stringid.TruncateID(id)
  433. if _, err := daemon.containerGraphDB.Set(name, id); err != nil {
  434. return "", err
  435. }
  436. return name, nil
  437. }
  438. func (daemon *Daemon) generateHostname(id string, config *containertypes.Config) {
  439. // Generate default hostname
  440. if config.Hostname == "" {
  441. config.Hostname = id[:12]
  442. }
  443. }
  444. func (daemon *Daemon) getEntrypointAndArgs(configEntrypoint *strslice.StrSlice, configCmd *strslice.StrSlice) (string, []string) {
  445. cmdSlice := configCmd.Slice()
  446. if configEntrypoint.Len() != 0 {
  447. eSlice := configEntrypoint.Slice()
  448. return eSlice[0], append(eSlice[1:], cmdSlice...)
  449. }
  450. return cmdSlice[0], cmdSlice[1:]
  451. }
  452. func (daemon *Daemon) newContainer(name string, config *containertypes.Config, imgID image.ID) (*container.Container, error) {
  453. var (
  454. id string
  455. err error
  456. noExplicitName = name == ""
  457. )
  458. id, name, err = daemon.generateIDAndName(name)
  459. if err != nil {
  460. return nil, err
  461. }
  462. daemon.generateHostname(id, config)
  463. entrypoint, args := daemon.getEntrypointAndArgs(config.Entrypoint, config.Cmd)
  464. base := daemon.newBaseContainer(id)
  465. base.Created = time.Now().UTC()
  466. base.Path = entrypoint
  467. base.Args = args //FIXME: de-duplicate from config
  468. base.Config = config
  469. base.HostConfig = &containertypes.HostConfig{}
  470. base.ImageID = imgID
  471. base.NetworkSettings = &network.Settings{IsAnonymousEndpoint: noExplicitName}
  472. base.Name = name
  473. base.Driver = daemon.GraphDriverName()
  474. return base, err
  475. }
  476. // GetFullContainerName returns a constructed container name. I think
  477. // it has to do with the fact that a container is a file on disk and
  478. // this is sort of just creating a file name.
  479. func GetFullContainerName(name string) (string, error) {
  480. if name == "" {
  481. return "", fmt.Errorf("Container name cannot be empty")
  482. }
  483. if name[0] != '/' {
  484. name = "/" + name
  485. }
  486. return name, nil
  487. }
  488. // GetByName returns a container given a name.
  489. func (daemon *Daemon) GetByName(name string) (*container.Container, error) {
  490. fullName, err := GetFullContainerName(name)
  491. if err != nil {
  492. return nil, err
  493. }
  494. entity := daemon.containerGraphDB.Get(fullName)
  495. if entity == nil {
  496. return nil, fmt.Errorf("Could not find entity for %s", name)
  497. }
  498. e := daemon.containers.Get(entity.ID())
  499. if e == nil {
  500. return nil, fmt.Errorf("Could not find container for entity id %s", entity.ID())
  501. }
  502. return e, nil
  503. }
  504. // SubscribeToEvents returns the currently record of events, a channel to stream new events from, and a function to cancel the stream of events.
  505. func (daemon *Daemon) SubscribeToEvents(since, sinceNano int64, filter filters.Args) ([]eventtypes.Message, chan interface{}) {
  506. ef := events.NewFilter(filter)
  507. return daemon.EventsService.SubscribeTopic(since, sinceNano, ef)
  508. }
  509. // UnsubscribeFromEvents stops the event subscription for a client by closing the
  510. // channel where the daemon sends events to.
  511. func (daemon *Daemon) UnsubscribeFromEvents(listener chan interface{}) {
  512. daemon.EventsService.Evict(listener)
  513. }
  514. // children returns all child containers of the container with the
  515. // given name. The containers are returned as a map from the container
  516. // name to a pointer to Container.
  517. func (daemon *Daemon) children(name string) (map[string]*container.Container, error) {
  518. name, err := GetFullContainerName(name)
  519. if err != nil {
  520. return nil, err
  521. }
  522. children := make(map[string]*container.Container)
  523. err = daemon.containerGraphDB.Walk(name, func(p string, e *graphdb.Entity) error {
  524. c, err := daemon.GetContainer(e.ID())
  525. if err != nil {
  526. return err
  527. }
  528. children[p] = c
  529. return nil
  530. }, 0)
  531. if err != nil {
  532. return nil, err
  533. }
  534. return children, nil
  535. }
  536. // parents returns the names of the parent containers of the container
  537. // with the given name.
  538. func (daemon *Daemon) parents(name string) ([]string, error) {
  539. name, err := GetFullContainerName(name)
  540. if err != nil {
  541. return nil, err
  542. }
  543. return daemon.containerGraphDB.Parents(name)
  544. }
  545. func (daemon *Daemon) registerLink(parent, child *container.Container, alias string) error {
  546. fullName := filepath.Join(parent.Name, alias)
  547. if !daemon.containerGraphDB.Exists(fullName) {
  548. _, err := daemon.containerGraphDB.Set(fullName, child.ID)
  549. return err
  550. }
  551. return nil
  552. }
  553. // NewDaemon sets up everything for the daemon to be able to service
  554. // requests from the webserver.
  555. func NewDaemon(config *Config, registryService *registry.Service) (daemon *Daemon, err error) {
  556. setDefaultMtu(config)
  557. // Ensure we have compatible configuration options
  558. if err := checkConfigOptions(config); err != nil {
  559. return nil, err
  560. }
  561. // Do we have a disabled network?
  562. config.DisableBridge = isBridgeNetworkDisabled(config)
  563. // Verify the platform is supported as a daemon
  564. if !platformSupported {
  565. return nil, errSystemNotSupported
  566. }
  567. // Validate platform-specific requirements
  568. if err := checkSystem(); err != nil {
  569. return nil, err
  570. }
  571. // set up SIGUSR1 handler on Unix-like systems, or a Win32 global event
  572. // on Windows to dump Go routine stacks
  573. setupDumpStackTrap()
  574. uidMaps, gidMaps, err := setupRemappedRoot(config)
  575. if err != nil {
  576. return nil, err
  577. }
  578. rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
  579. if err != nil {
  580. return nil, err
  581. }
  582. // get the canonical path to the Docker root directory
  583. var realRoot string
  584. if _, err := os.Stat(config.Root); err != nil && os.IsNotExist(err) {
  585. realRoot = config.Root
  586. } else {
  587. realRoot, err = fileutils.ReadSymlinkedDirectory(config.Root)
  588. if err != nil {
  589. return nil, fmt.Errorf("Unable to get the full path to root (%s): %s", config.Root, err)
  590. }
  591. }
  592. if err = setupDaemonRoot(config, realRoot, rootUID, rootGID); err != nil {
  593. return nil, err
  594. }
  595. // set up the tmpDir to use a canonical path
  596. tmp, err := tempDir(config.Root, rootUID, rootGID)
  597. if err != nil {
  598. return nil, fmt.Errorf("Unable to get the TempDir under %s: %s", config.Root, err)
  599. }
  600. realTmp, err := fileutils.ReadSymlinkedDirectory(tmp)
  601. if err != nil {
  602. return nil, fmt.Errorf("Unable to get the full path to the TempDir (%s): %s", tmp, err)
  603. }
  604. os.Setenv("TMPDIR", realTmp)
  605. d := &Daemon{}
  606. // Ensure the daemon is properly shutdown if there is a failure during
  607. // initialization
  608. defer func() {
  609. if err != nil {
  610. if err := d.Shutdown(); err != nil {
  611. logrus.Error(err)
  612. }
  613. }
  614. }()
  615. // Verify logging driver type
  616. if config.LogConfig.Type != "none" {
  617. if _, err := logger.GetLogDriver(config.LogConfig.Type); err != nil {
  618. return nil, fmt.Errorf("error finding the logging driver: %v", err)
  619. }
  620. }
  621. logrus.Debugf("Using default logging driver %s", config.LogConfig.Type)
  622. daemonRepo := filepath.Join(config.Root, "containers")
  623. if err := idtools.MkdirAllAs(daemonRepo, 0700, rootUID, rootGID); err != nil && !os.IsExist(err) {
  624. return nil, err
  625. }
  626. driverName := os.Getenv("DOCKER_DRIVER")
  627. if driverName == "" {
  628. driverName = config.GraphDriver
  629. }
  630. d.layerStore, err = layer.NewStoreFromOptions(layer.StoreOptions{
  631. StorePath: config.Root,
  632. MetadataStorePathTemplate: filepath.Join(config.Root, "image", "%s", "layerdb"),
  633. GraphDriver: driverName,
  634. GraphDriverOptions: config.GraphOptions,
  635. UIDMaps: uidMaps,
  636. GIDMaps: gidMaps,
  637. })
  638. if err != nil {
  639. return nil, err
  640. }
  641. graphDriver := d.layerStore.DriverName()
  642. imageRoot := filepath.Join(config.Root, "image", graphDriver)
  643. // Configure and validate the kernels security support
  644. if err := configureKernelSecuritySupport(config, graphDriver); err != nil {
  645. return nil, err
  646. }
  647. d.downloadManager = xfer.NewLayerDownloadManager(d.layerStore, maxDownloadConcurrency)
  648. d.uploadManager = xfer.NewLayerUploadManager(maxUploadConcurrency)
  649. ifs, err := image.NewFSStoreBackend(filepath.Join(imageRoot, "imagedb"))
  650. if err != nil {
  651. return nil, err
  652. }
  653. d.imageStore, err = image.NewImageStore(ifs, d.layerStore)
  654. if err != nil {
  655. return nil, err
  656. }
  657. // Configure the volumes driver
  658. volStore, err := configureVolumes(config, rootUID, rootGID)
  659. if err != nil {
  660. return nil, err
  661. }
  662. trustKey, err := api.LoadOrCreateTrustKey(config.TrustKeyPath)
  663. if err != nil {
  664. return nil, err
  665. }
  666. trustDir := filepath.Join(config.Root, "trust")
  667. if err := system.MkdirAll(trustDir, 0700); err != nil {
  668. return nil, err
  669. }
  670. distributionMetadataStore, err := dmetadata.NewFSMetadataStore(filepath.Join(imageRoot, "distribution"))
  671. if err != nil {
  672. return nil, err
  673. }
  674. eventsService := events.New()
  675. referenceStore, err := reference.NewReferenceStore(filepath.Join(imageRoot, "repositories.json"))
  676. if err != nil {
  677. return nil, fmt.Errorf("Couldn't create Tag store repositories: %s", err)
  678. }
  679. if err := restoreCustomImage(d.imageStore, d.layerStore, referenceStore); err != nil {
  680. return nil, fmt.Errorf("Couldn't restore custom images: %s", err)
  681. }
  682. migrationStart := time.Now()
  683. if err := v1.Migrate(config.Root, graphDriver, d.layerStore, d.imageStore, referenceStore, distributionMetadataStore); err != nil {
  684. return nil, err
  685. }
  686. logrus.Infof("Graph migration to content-addressability took %.2f seconds", time.Since(migrationStart).Seconds())
  687. // Discovery is only enabled when the daemon is launched with an address to advertise. When
  688. // initialized, the daemon is registered and we can store the discovery backend as its read-only
  689. // DiscoveryWatcher version.
  690. if config.ClusterStore != "" && config.ClusterAdvertise != "" {
  691. advertise, err := discovery.ParseAdvertise(config.ClusterStore, config.ClusterAdvertise)
  692. if err != nil {
  693. return nil, fmt.Errorf("discovery advertise parsing failed (%v)", err)
  694. }
  695. config.ClusterAdvertise = advertise
  696. d.discoveryWatcher, err = initDiscovery(config.ClusterStore, config.ClusterAdvertise, config.ClusterOpts)
  697. if err != nil {
  698. return nil, fmt.Errorf("discovery initialization failed (%v)", err)
  699. }
  700. } else if config.ClusterAdvertise != "" {
  701. return nil, fmt.Errorf("invalid cluster configuration. --cluster-advertise must be accompanied by --cluster-store configuration")
  702. }
  703. d.netController, err = d.initNetworkController(config)
  704. if err != nil {
  705. return nil, fmt.Errorf("Error initializing network controller: %v", err)
  706. }
  707. graphdbPath := filepath.Join(config.Root, "linkgraph.db")
  708. graph, err := graphdb.NewSqliteConn(graphdbPath)
  709. if err != nil {
  710. return nil, err
  711. }
  712. d.containerGraphDB = graph
  713. sysInfo := sysinfo.New(false)
  714. // Check if Devices cgroup is mounted, it is hard requirement for container security,
  715. // on Linux/FreeBSD.
  716. if runtime.GOOS != "windows" && !sysInfo.CgroupDevicesEnabled {
  717. return nil, fmt.Errorf("Devices cgroup isn't mounted")
  718. }
  719. ed, err := execdrivers.NewDriver(config.ExecOptions, config.ExecRoot, config.Root, sysInfo)
  720. if err != nil {
  721. return nil, err
  722. }
  723. d.ID = trustKey.PublicKey().KeyID()
  724. d.repository = daemonRepo
  725. d.containers = &contStore{s: make(map[string]*container.Container)}
  726. d.execCommands = exec.NewStore()
  727. d.referenceStore = referenceStore
  728. d.distributionMetadataStore = distributionMetadataStore
  729. d.trustKey = trustKey
  730. d.idIndex = truncindex.NewTruncIndex([]string{})
  731. d.configStore = config
  732. d.execDriver = ed
  733. d.statsCollector = d.newStatsCollector(1 * time.Second)
  734. d.defaultLogConfig = config.LogConfig
  735. d.RegistryService = registryService
  736. d.EventsService = eventsService
  737. d.volumes = volStore
  738. d.root = config.Root
  739. d.uidMaps = uidMaps
  740. d.gidMaps = gidMaps
  741. if err := d.cleanupMounts(); err != nil {
  742. return nil, err
  743. }
  744. go d.execCommandGC()
  745. if err := d.restore(); err != nil {
  746. return nil, err
  747. }
  748. return d, nil
  749. }
  750. func (daemon *Daemon) shutdownContainer(c *container.Container) error {
  751. // TODO(windows): Handle docker restart with paused containers
  752. if c.IsPaused() {
  753. // To terminate a process in freezer cgroup, we should send
  754. // SIGTERM to this process then unfreeze it, and the process will
  755. // force to terminate immediately.
  756. logrus.Debugf("Found container %s is paused, sending SIGTERM before unpause it", c.ID)
  757. sig, ok := signal.SignalMap["TERM"]
  758. if !ok {
  759. return fmt.Errorf("System doesn not support SIGTERM")
  760. }
  761. if err := daemon.kill(c, int(sig)); err != nil {
  762. return fmt.Errorf("sending SIGTERM to container %s with error: %v", c.ID, err)
  763. }
  764. if err := daemon.containerUnpause(c); err != nil {
  765. return fmt.Errorf("Failed to unpause container %s with error: %v", c.ID, err)
  766. }
  767. if _, err := c.WaitStop(10 * time.Second); err != nil {
  768. logrus.Debugf("container %s failed to exit in 10 second of SIGTERM, sending SIGKILL to force", c.ID)
  769. sig, ok := signal.SignalMap["KILL"]
  770. if !ok {
  771. return fmt.Errorf("System does not support SIGKILL")
  772. }
  773. if err := daemon.kill(c, int(sig)); err != nil {
  774. logrus.Errorf("Failed to SIGKILL container %s", c.ID)
  775. }
  776. c.WaitStop(-1 * time.Second)
  777. return err
  778. }
  779. }
  780. // If container failed to exit in 10 seconds of SIGTERM, then using the force
  781. if err := daemon.containerStop(c, 10); err != nil {
  782. return fmt.Errorf("Stop container %s with error: %v", c.ID, err)
  783. }
  784. c.WaitStop(-1 * time.Second)
  785. return nil
  786. }
  787. // Shutdown stops the daemon.
  788. func (daemon *Daemon) Shutdown() error {
  789. daemon.shutdown = true
  790. if daemon.containers != nil {
  791. group := sync.WaitGroup{}
  792. logrus.Debug("starting clean shutdown of all containers...")
  793. for _, cont := range daemon.List() {
  794. if !cont.IsRunning() {
  795. continue
  796. }
  797. logrus.Debugf("stopping %s", cont.ID)
  798. group.Add(1)
  799. go func(c *container.Container) {
  800. defer group.Done()
  801. if err := daemon.shutdownContainer(c); err != nil {
  802. logrus.Errorf("Stop container error: %v", err)
  803. return
  804. }
  805. logrus.Debugf("container stopped %s", c.ID)
  806. }(cont)
  807. }
  808. group.Wait()
  809. }
  810. // trigger libnetwork Stop only if it's initialized
  811. if daemon.netController != nil {
  812. daemon.netController.Stop()
  813. }
  814. if daemon.containerGraphDB != nil {
  815. if err := daemon.containerGraphDB.Close(); err != nil {
  816. logrus.Errorf("Error during container graph.Close(): %v", err)
  817. }
  818. }
  819. if daemon.layerStore != nil {
  820. if err := daemon.layerStore.Cleanup(); err != nil {
  821. logrus.Errorf("Error during layer Store.Cleanup(): %v", err)
  822. }
  823. }
  824. if err := daemon.cleanupMounts(); err != nil {
  825. return err
  826. }
  827. return nil
  828. }
  829. // Mount sets container.BaseFS
  830. // (is it not set coming in? why is it unset?)
  831. func (daemon *Daemon) Mount(container *container.Container) error {
  832. dir, err := container.RWLayer.Mount(container.GetMountLabel())
  833. if err != nil {
  834. return err
  835. }
  836. logrus.Debugf("container mounted via layerStore: %v", dir)
  837. if container.BaseFS != dir {
  838. // The mount path reported by the graph driver should always be trusted on Windows, since the
  839. // volume path for a given mounted layer may change over time. This should only be an error
  840. // on non-Windows operating systems.
  841. if container.BaseFS != "" && runtime.GOOS != "windows" {
  842. daemon.Unmount(container)
  843. return fmt.Errorf("Error: driver %s is returning inconsistent paths for container %s ('%s' then '%s')",
  844. daemon.GraphDriverName(), container.ID, container.BaseFS, dir)
  845. }
  846. }
  847. container.BaseFS = dir // TODO: combine these fields
  848. return nil
  849. }
  850. // Unmount unsets the container base filesystem
  851. func (daemon *Daemon) Unmount(container *container.Container) {
  852. if err := container.RWLayer.Unmount(); err != nil {
  853. logrus.Errorf("Error unmounting container %s: %s", container.ID, err)
  854. }
  855. }
  856. // Run uses the execution driver to run a given container
  857. func (daemon *Daemon) Run(c *container.Container, pipes *execdriver.Pipes, startCallback execdriver.DriverCallback) (execdriver.ExitStatus, error) {
  858. hooks := execdriver.Hooks{
  859. Start: startCallback,
  860. }
  861. hooks.PreStart = append(hooks.PreStart, func(processConfig *execdriver.ProcessConfig, pid int, chOOM <-chan struct{}) error {
  862. return daemon.setNetworkNamespaceKey(c.ID, pid)
  863. })
  864. return daemon.execDriver.Run(c.Command, pipes, hooks)
  865. }
  866. func (daemon *Daemon) kill(c *container.Container, sig int) error {
  867. return daemon.execDriver.Kill(c.Command, sig)
  868. }
  869. func (daemon *Daemon) stats(c *container.Container) (*execdriver.ResourceStats, error) {
  870. return daemon.execDriver.Stats(c.ID)
  871. }
  872. func (daemon *Daemon) subscribeToContainerStats(c *container.Container) chan interface{} {
  873. return daemon.statsCollector.collect(c)
  874. }
  875. func (daemon *Daemon) unsubscribeToContainerStats(c *container.Container, ch chan interface{}) {
  876. daemon.statsCollector.unsubscribe(c, ch)
  877. }
  878. func (daemon *Daemon) changes(container *container.Container) ([]archive.Change, error) {
  879. return container.RWLayer.Changes()
  880. }
  881. // TagImage creates a tag in the repository and with the name specified by newTag,
  882. // pointing to the image named imageName (alternatively, imageName can also be an image ID).
  883. func (daemon *Daemon) TagImage(newTag reference.Named, imageName string) error {
  884. imageID, err := daemon.GetImageID(imageName)
  885. if err != nil {
  886. return err
  887. }
  888. if err := daemon.referenceStore.AddTag(newTag, imageID, true); err != nil {
  889. return err
  890. }
  891. daemon.LogImageEvent(imageID.String(), newTag.String(), "tag")
  892. return nil
  893. }
  894. func writeDistributionProgress(cancelFunc func(), outStream io.Writer, progressChan <-chan progress.Progress) {
  895. progressOutput := streamformatter.NewJSONStreamFormatter().NewProgressOutput(outStream, false)
  896. operationCancelled := false
  897. for prog := range progressChan {
  898. if err := progressOutput.WriteProgress(prog); err != nil && !operationCancelled {
  899. // don't log broken pipe errors as this is the normal case when a client aborts
  900. if isBrokenPipe(err) {
  901. logrus.Info("Pull session cancelled")
  902. } else {
  903. logrus.Errorf("error writing progress to client: %v", err)
  904. }
  905. cancelFunc()
  906. operationCancelled = true
  907. // Don't return, because we need to continue draining
  908. // progressChan until it's closed to avoid a deadlock.
  909. }
  910. }
  911. }
  912. func isBrokenPipe(e error) bool {
  913. if netErr, ok := e.(*net.OpError); ok {
  914. e = netErr.Err
  915. if sysErr, ok := netErr.Err.(*os.SyscallError); ok {
  916. e = sysErr.Err
  917. }
  918. }
  919. return e == syscall.EPIPE
  920. }
  921. // PullImage initiates a pull operation. image is the repository name to pull, and
  922. // tag may be either empty, or indicate a specific tag to pull.
  923. func (daemon *Daemon) PullImage(ref reference.Named, metaHeaders map[string][]string, authConfig *types.AuthConfig, outStream io.Writer) error {
  924. // Include a buffer so that slow client connections don't affect
  925. // transfer performance.
  926. progressChan := make(chan progress.Progress, 100)
  927. writesDone := make(chan struct{})
  928. ctx, cancelFunc := context.WithCancel(context.Background())
  929. go func() {
  930. writeDistributionProgress(cancelFunc, outStream, progressChan)
  931. close(writesDone)
  932. }()
  933. imagePullConfig := &distribution.ImagePullConfig{
  934. MetaHeaders: metaHeaders,
  935. AuthConfig: authConfig,
  936. ProgressOutput: progress.ChanOutput(progressChan),
  937. RegistryService: daemon.RegistryService,
  938. ImageEventLogger: daemon.LogImageEvent,
  939. MetadataStore: daemon.distributionMetadataStore,
  940. ImageStore: daemon.imageStore,
  941. ReferenceStore: daemon.referenceStore,
  942. DownloadManager: daemon.downloadManager,
  943. }
  944. err := distribution.Pull(ctx, ref, imagePullConfig)
  945. close(progressChan)
  946. <-writesDone
  947. return err
  948. }
  949. // ExportImage exports a list of images to the given output stream. The
  950. // exported images are archived into a tar when written to the output
  951. // stream. All images with the given tag and all versions containing
  952. // the same tag are exported. names is the set of tags to export, and
  953. // outStream is the writer which the images are written to.
  954. func (daemon *Daemon) ExportImage(names []string, outStream io.Writer) error {
  955. imageExporter := tarexport.NewTarExporter(daemon.imageStore, daemon.layerStore, daemon.referenceStore)
  956. return imageExporter.Save(names, outStream)
  957. }
  958. // PushImage initiates a push operation on the repository named localName.
  959. func (daemon *Daemon) PushImage(ref reference.Named, metaHeaders map[string][]string, authConfig *types.AuthConfig, outStream io.Writer) error {
  960. // Include a buffer so that slow client connections don't affect
  961. // transfer performance.
  962. progressChan := make(chan progress.Progress, 100)
  963. writesDone := make(chan struct{})
  964. ctx, cancelFunc := context.WithCancel(context.Background())
  965. go func() {
  966. writeDistributionProgress(cancelFunc, outStream, progressChan)
  967. close(writesDone)
  968. }()
  969. imagePushConfig := &distribution.ImagePushConfig{
  970. MetaHeaders: metaHeaders,
  971. AuthConfig: authConfig,
  972. ProgressOutput: progress.ChanOutput(progressChan),
  973. RegistryService: daemon.RegistryService,
  974. ImageEventLogger: daemon.LogImageEvent,
  975. MetadataStore: daemon.distributionMetadataStore,
  976. LayerStore: daemon.layerStore,
  977. ImageStore: daemon.imageStore,
  978. ReferenceStore: daemon.referenceStore,
  979. TrustKey: daemon.trustKey,
  980. UploadManager: daemon.uploadManager,
  981. }
  982. err := distribution.Push(ctx, ref, imagePushConfig)
  983. close(progressChan)
  984. <-writesDone
  985. return err
  986. }
  987. // LookupImage looks up an image by name and returns it as an ImageInspect
  988. // structure.
  989. func (daemon *Daemon) LookupImage(name string) (*types.ImageInspect, error) {
  990. img, err := daemon.GetImage(name)
  991. if err != nil {
  992. return nil, fmt.Errorf("No such image: %s", name)
  993. }
  994. refs := daemon.referenceStore.References(img.ID())
  995. repoTags := []string{}
  996. repoDigests := []string{}
  997. for _, ref := range refs {
  998. switch ref.(type) {
  999. case reference.NamedTagged:
  1000. repoTags = append(repoTags, ref.String())
  1001. case reference.Canonical:
  1002. repoDigests = append(repoDigests, ref.String())
  1003. }
  1004. }
  1005. var size int64
  1006. var layerMetadata map[string]string
  1007. layerID := img.RootFS.ChainID()
  1008. if layerID != "" {
  1009. l, err := daemon.layerStore.Get(layerID)
  1010. if err != nil {
  1011. return nil, err
  1012. }
  1013. defer layer.ReleaseAndLog(daemon.layerStore, l)
  1014. size, err = l.Size()
  1015. if err != nil {
  1016. return nil, err
  1017. }
  1018. layerMetadata, err = l.Metadata()
  1019. if err != nil {
  1020. return nil, err
  1021. }
  1022. }
  1023. comment := img.Comment
  1024. if len(comment) == 0 && len(img.History) > 0 {
  1025. comment = img.History[len(img.History)-1].Comment
  1026. }
  1027. imageInspect := &types.ImageInspect{
  1028. ID: img.ID().String(),
  1029. RepoTags: repoTags,
  1030. RepoDigests: repoDigests,
  1031. Parent: img.Parent.String(),
  1032. Comment: comment,
  1033. Created: img.Created.Format(time.RFC3339Nano),
  1034. Container: img.Container,
  1035. ContainerConfig: &img.ContainerConfig,
  1036. DockerVersion: img.DockerVersion,
  1037. Author: img.Author,
  1038. Config: img.Config,
  1039. Architecture: img.Architecture,
  1040. Os: img.OS,
  1041. Size: size,
  1042. VirtualSize: size, // TODO: field unused, deprecate
  1043. }
  1044. imageInspect.GraphDriver.Name = daemon.GraphDriverName()
  1045. imageInspect.GraphDriver.Data = layerMetadata
  1046. return imageInspect, nil
  1047. }
  1048. // LoadImage uploads a set of images into the repository. This is the
  1049. // complement of ImageExport. The input stream is an uncompressed tar
  1050. // ball containing images and metadata.
  1051. func (daemon *Daemon) LoadImage(inTar io.ReadCloser, outStream io.Writer) error {
  1052. imageExporter := tarexport.NewTarExporter(daemon.imageStore, daemon.layerStore, daemon.referenceStore)
  1053. return imageExporter.Load(inTar, outStream)
  1054. }
  1055. // ImageHistory returns a slice of ImageHistory structures for the specified image
  1056. // name by walking the image lineage.
  1057. func (daemon *Daemon) ImageHistory(name string) ([]*types.ImageHistory, error) {
  1058. img, err := daemon.GetImage(name)
  1059. if err != nil {
  1060. return nil, err
  1061. }
  1062. history := []*types.ImageHistory{}
  1063. layerCounter := 0
  1064. rootFS := *img.RootFS
  1065. rootFS.DiffIDs = nil
  1066. for _, h := range img.History {
  1067. var layerSize int64
  1068. if !h.EmptyLayer {
  1069. if len(img.RootFS.DiffIDs) <= layerCounter {
  1070. return nil, errors.New("too many non-empty layers in History section")
  1071. }
  1072. rootFS.Append(img.RootFS.DiffIDs[layerCounter])
  1073. l, err := daemon.layerStore.Get(rootFS.ChainID())
  1074. if err != nil {
  1075. return nil, err
  1076. }
  1077. layerSize, err = l.DiffSize()
  1078. layer.ReleaseAndLog(daemon.layerStore, l)
  1079. if err != nil {
  1080. return nil, err
  1081. }
  1082. layerCounter++
  1083. }
  1084. history = append([]*types.ImageHistory{{
  1085. ID: "<missing>",
  1086. Created: h.Created.Unix(),
  1087. CreatedBy: h.CreatedBy,
  1088. Comment: h.Comment,
  1089. Size: layerSize,
  1090. }}, history...)
  1091. }
  1092. // Fill in image IDs and tags
  1093. histImg := img
  1094. id := img.ID()
  1095. for _, h := range history {
  1096. h.ID = id.String()
  1097. var tags []string
  1098. for _, r := range daemon.referenceStore.References(id) {
  1099. if _, ok := r.(reference.NamedTagged); ok {
  1100. tags = append(tags, r.String())
  1101. }
  1102. }
  1103. h.Tags = tags
  1104. id = histImg.Parent
  1105. if id == "" {
  1106. break
  1107. }
  1108. histImg, err = daemon.GetImage(id.String())
  1109. if err != nil {
  1110. break
  1111. }
  1112. }
  1113. return history, nil
  1114. }
  1115. // GetImageID returns an image ID corresponding to the image referred to by
  1116. // refOrID.
  1117. func (daemon *Daemon) GetImageID(refOrID string) (image.ID, error) {
  1118. // Treat as an ID
  1119. if id, err := digest.ParseDigest(refOrID); err == nil {
  1120. return image.ID(id), nil
  1121. }
  1122. // Treat it as a possible tag or digest reference
  1123. if ref, err := reference.ParseNamed(refOrID); err == nil {
  1124. if id, err := daemon.referenceStore.Get(ref); err == nil {
  1125. return id, nil
  1126. }
  1127. if tagged, ok := ref.(reference.NamedTagged); ok {
  1128. if id, err := daemon.imageStore.Search(tagged.Tag()); err == nil {
  1129. for _, namedRef := range daemon.referenceStore.References(id) {
  1130. if namedRef.Name() == ref.Name() {
  1131. return id, nil
  1132. }
  1133. }
  1134. }
  1135. }
  1136. }
  1137. // Search based on ID
  1138. if id, err := daemon.imageStore.Search(refOrID); err == nil {
  1139. return id, nil
  1140. }
  1141. return "", ErrImageDoesNotExist{refOrID}
  1142. }
  1143. // GetImage returns an image corresponding to the image referred to by refOrID.
  1144. func (daemon *Daemon) GetImage(refOrID string) (*image.Image, error) {
  1145. imgID, err := daemon.GetImageID(refOrID)
  1146. if err != nil {
  1147. return nil, err
  1148. }
  1149. return daemon.imageStore.Get(imgID)
  1150. }
  1151. // GraphDriverName returns the name of the graph driver used by the layer.Store
  1152. func (daemon *Daemon) GraphDriverName() string {
  1153. return daemon.layerStore.DriverName()
  1154. }
  1155. // ExecutionDriver returns the currently used driver for creating and
  1156. // starting execs in a container.
  1157. func (daemon *Daemon) ExecutionDriver() execdriver.Driver {
  1158. return daemon.execDriver
  1159. }
  1160. func (daemon *Daemon) containerGraph() *graphdb.Database {
  1161. return daemon.containerGraphDB
  1162. }
  1163. // GetUIDGIDMaps returns the current daemon's user namespace settings
  1164. // for the full uid and gid maps which will be applied to containers
  1165. // started in this instance.
  1166. func (daemon *Daemon) GetUIDGIDMaps() ([]idtools.IDMap, []idtools.IDMap) {
  1167. return daemon.uidMaps, daemon.gidMaps
  1168. }
  1169. // GetRemappedUIDGID returns the current daemon's uid and gid values
  1170. // if user namespaces are in use for this daemon instance. If not
  1171. // this function will return "real" root values of 0, 0.
  1172. func (daemon *Daemon) GetRemappedUIDGID() (int, int) {
  1173. uid, gid, _ := idtools.GetRootUIDGID(daemon.uidMaps, daemon.gidMaps)
  1174. return uid, gid
  1175. }
  1176. // ImageGetCached returns the earliest created image that is a child
  1177. // of the image with imgID, that had the same config when it was
  1178. // created. nil is returned if a child cannot be found. An error is
  1179. // returned if the parent image cannot be found.
  1180. func (daemon *Daemon) ImageGetCached(imgID image.ID, config *containertypes.Config) (*image.Image, error) {
  1181. // Retrieve all images
  1182. imgs := daemon.Map()
  1183. var siblings []image.ID
  1184. for id, img := range imgs {
  1185. if img.Parent == imgID {
  1186. siblings = append(siblings, id)
  1187. }
  1188. }
  1189. // Loop on the children of the given image and check the config
  1190. var match *image.Image
  1191. for _, id := range siblings {
  1192. img, ok := imgs[id]
  1193. if !ok {
  1194. return nil, fmt.Errorf("unable to find image %q", id)
  1195. }
  1196. if runconfig.Compare(&img.ContainerConfig, config) {
  1197. if match == nil || match.Created.Before(img.Created) {
  1198. match = img
  1199. }
  1200. }
  1201. }
  1202. return match, nil
  1203. }
  1204. // tempDir returns the default directory to use for temporary files.
  1205. func tempDir(rootDir string, rootUID, rootGID int) (string, error) {
  1206. var tmpDir string
  1207. if tmpDir = os.Getenv("DOCKER_TMPDIR"); tmpDir == "" {
  1208. tmpDir = filepath.Join(rootDir, "tmp")
  1209. }
  1210. return tmpDir, idtools.MkdirAllAs(tmpDir, 0700, rootUID, rootGID)
  1211. }
  1212. func (daemon *Daemon) setSecurityOptions(container *container.Container, hostConfig *containertypes.HostConfig) error {
  1213. container.Lock()
  1214. defer container.Unlock()
  1215. return parseSecurityOpt(container, hostConfig)
  1216. }
  1217. func (daemon *Daemon) setHostConfig(container *container.Container, hostConfig *containertypes.HostConfig) error {
  1218. // Do not lock while creating volumes since this could be calling out to external plugins
  1219. // Don't want to block other actions, like `docker ps` because we're waiting on an external plugin
  1220. if err := daemon.registerMountPoints(container, hostConfig); err != nil {
  1221. return err
  1222. }
  1223. container.Lock()
  1224. defer container.Unlock()
  1225. // Register any links from the host config before starting the container
  1226. if err := daemon.registerLinks(container, hostConfig); err != nil {
  1227. return err
  1228. }
  1229. container.HostConfig = hostConfig
  1230. container.ToDisk()
  1231. return nil
  1232. }
  1233. func (daemon *Daemon) setupInitLayer(initPath string) error {
  1234. rootUID, rootGID := daemon.GetRemappedUIDGID()
  1235. return setupInitLayer(initPath, rootUID, rootGID)
  1236. }
  1237. func setDefaultMtu(config *Config) {
  1238. // do nothing if the config does not have the default 0 value.
  1239. if config.Mtu != 0 {
  1240. return
  1241. }
  1242. config.Mtu = defaultNetworkMtu
  1243. }
  1244. // verifyContainerSettings performs validation of the hostconfig and config
  1245. // structures.
  1246. func (daemon *Daemon) verifyContainerSettings(hostConfig *containertypes.HostConfig, config *containertypes.Config) ([]string, error) {
  1247. // First perform verification of settings common across all platforms.
  1248. if config != nil {
  1249. if config.WorkingDir != "" {
  1250. config.WorkingDir = filepath.FromSlash(config.WorkingDir) // Ensure in platform semantics
  1251. if !system.IsAbs(config.WorkingDir) {
  1252. return nil, fmt.Errorf("The working directory '%s' is invalid. It needs to be an absolute path.", config.WorkingDir)
  1253. }
  1254. }
  1255. if len(config.StopSignal) > 0 {
  1256. _, err := signal.ParseSignal(config.StopSignal)
  1257. if err != nil {
  1258. return nil, err
  1259. }
  1260. }
  1261. }
  1262. if hostConfig == nil {
  1263. return nil, nil
  1264. }
  1265. for port := range hostConfig.PortBindings {
  1266. _, portStr := nat.SplitProtoPort(string(port))
  1267. if _, err := nat.ParsePort(portStr); err != nil {
  1268. return nil, fmt.Errorf("Invalid port specification: %q", portStr)
  1269. }
  1270. for _, pb := range hostConfig.PortBindings[port] {
  1271. _, err := nat.NewPort(nat.SplitProtoPort(pb.HostPort))
  1272. if err != nil {
  1273. return nil, fmt.Errorf("Invalid port specification: %q", pb.HostPort)
  1274. }
  1275. }
  1276. }
  1277. // Now do platform-specific verification
  1278. return verifyPlatformContainerSettings(daemon, hostConfig, config)
  1279. }
  1280. func configureVolumes(config *Config, rootUID, rootGID int) (*store.VolumeStore, error) {
  1281. volumesDriver, err := local.New(config.Root, rootUID, rootGID)
  1282. if err != nil {
  1283. return nil, err
  1284. }
  1285. volumedrivers.Register(volumesDriver, volumesDriver.Name())
  1286. return store.New(), nil
  1287. }
  1288. // AuthenticateToRegistry checks the validity of credentials in authConfig
  1289. func (daemon *Daemon) AuthenticateToRegistry(authConfig *types.AuthConfig) (string, error) {
  1290. return daemon.RegistryService.Auth(authConfig)
  1291. }
  1292. // SearchRegistryForImages queries the registry for images matching
  1293. // term. authConfig is used to login.
  1294. func (daemon *Daemon) SearchRegistryForImages(term string,
  1295. authConfig *types.AuthConfig,
  1296. headers map[string][]string) (*registrytypes.SearchResults, error) {
  1297. return daemon.RegistryService.Search(term, authConfig, headers)
  1298. }
  1299. // IsShuttingDown tells whether the daemon is shutting down or not
  1300. func (daemon *Daemon) IsShuttingDown() bool {
  1301. return daemon.shutdown
  1302. }
  1303. // GetContainerStats collects all the stats published by a container
  1304. func (daemon *Daemon) GetContainerStats(container *container.Container) (*execdriver.ResourceStats, error) {
  1305. stats, err := daemon.stats(container)
  1306. if err != nil {
  1307. return nil, err
  1308. }
  1309. // Retrieve the nw statistics from libnetwork and inject them in the Stats
  1310. var nwStats []*libcontainer.NetworkInterface
  1311. if nwStats, err = daemon.getNetworkStats(container); err != nil {
  1312. return nil, err
  1313. }
  1314. stats.Interfaces = nwStats
  1315. return stats, nil
  1316. }
  1317. func (daemon *Daemon) getNetworkStats(c *container.Container) ([]*libcontainer.NetworkInterface, error) {
  1318. var list []*libcontainer.NetworkInterface
  1319. sb, err := daemon.netController.SandboxByID(c.NetworkSettings.SandboxID)
  1320. if err != nil {
  1321. return list, err
  1322. }
  1323. stats, err := sb.Statistics()
  1324. if err != nil {
  1325. return list, err
  1326. }
  1327. // Convert libnetwork nw stats into libcontainer nw stats
  1328. for ifName, ifStats := range stats {
  1329. list = append(list, convertLnNetworkStats(ifName, ifStats))
  1330. }
  1331. return list, nil
  1332. }
  1333. // newBaseContainer creates a new container with its initial
  1334. // configuration based on the root storage from the daemon.
  1335. func (daemon *Daemon) newBaseContainer(id string) *container.Container {
  1336. return container.NewBaseContainer(id, daemon.containerRoot(id))
  1337. }
  1338. func convertLnNetworkStats(name string, stats *lntypes.InterfaceStatistics) *libcontainer.NetworkInterface {
  1339. n := &libcontainer.NetworkInterface{Name: name}
  1340. n.RxBytes = stats.RxBytes
  1341. n.RxPackets = stats.RxPackets
  1342. n.RxErrors = stats.RxErrors
  1343. n.RxDropped = stats.RxDropped
  1344. n.TxBytes = stats.TxBytes
  1345. n.TxPackets = stats.TxPackets
  1346. n.TxErrors = stats.TxErrors
  1347. n.TxDropped = stats.TxDropped
  1348. return n
  1349. }
  1350. func validateID(id string) error {
  1351. if id == "" {
  1352. return derr.ErrorCodeEmptyID
  1353. }
  1354. return nil
  1355. }