runtime.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. package docker
  2. import (
  3. "container/list"
  4. "fmt"
  5. "github.com/dotcloud/docker/utils"
  6. "github.com/dotcloud/docker/devmapper"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "path/filepath"
  14. "sort"
  15. "strings"
  16. "time"
  17. )
  18. const (
  19. DefaultFilesystemType = "devicemapper"
  20. )
  21. var defaultDns = []string{"8.8.8.8", "8.8.4.4"}
  22. type Capabilities struct {
  23. MemoryLimit bool
  24. SwapLimit bool
  25. IPv4ForwardingDisabled bool
  26. }
  27. type Runtime struct {
  28. root string
  29. repository string
  30. containers *list.List
  31. networkManager *NetworkManager
  32. graph *Graph
  33. repositories *TagStore
  34. idIndex *utils.TruncIndex
  35. capabilities *Capabilities
  36. kernelVersion *utils.KernelVersionInfo
  37. autoRestart bool
  38. volumes *Graph
  39. srv *Server
  40. Dns []string
  41. deviceSet *devmapper.DeviceSet
  42. }
  43. var sysInitPath string
  44. func init() {
  45. env := os.Getenv("_DOCKER_INIT_PATH")
  46. if env != "" {
  47. sysInitPath = env
  48. } else {
  49. selfPath := utils.SelfPath()
  50. // If we have a separate docker-init, use that, otherwise use the
  51. // main docker binary
  52. dir := filepath.Dir(selfPath)
  53. dockerInitPath := filepath.Join(dir, "docker-init")
  54. if _, err := os.Stat(dockerInitPath); err != nil {
  55. sysInitPath = selfPath
  56. } else {
  57. sysInitPath = dockerInitPath
  58. }
  59. }
  60. }
  61. // List returns an array of all containers registered in the runtime.
  62. func (runtime *Runtime) List() []*Container {
  63. containers := new(History)
  64. for e := runtime.containers.Front(); e != nil; e = e.Next() {
  65. containers.Add(e.Value.(*Container))
  66. }
  67. return *containers
  68. }
  69. func (runtime *Runtime) getContainerElement(id string) *list.Element {
  70. for e := runtime.containers.Front(); e != nil; e = e.Next() {
  71. container := e.Value.(*Container)
  72. if container.ID == id {
  73. return e
  74. }
  75. }
  76. return nil
  77. }
  78. func (runtime *Runtime) GetDeviceSet() (*devmapper.DeviceSet, error) {
  79. if runtime.deviceSet == nil {
  80. return nil, fmt.Errorf("No device set available")
  81. }
  82. return runtime.deviceSet, nil
  83. }
  84. // Get looks for a container by the specified ID or name, and returns it.
  85. // If the container is not found, or if an error occurs, nil is returned.
  86. func (runtime *Runtime) Get(name string) *Container {
  87. id, err := runtime.idIndex.Get(name)
  88. if err != nil {
  89. return nil
  90. }
  91. e := runtime.getContainerElement(id)
  92. if e == nil {
  93. return nil
  94. }
  95. return e.Value.(*Container)
  96. }
  97. // Exists returns a true if a container of the specified ID or name exists,
  98. // false otherwise.
  99. func (runtime *Runtime) Exists(id string) bool {
  100. return runtime.Get(id) != nil
  101. }
  102. func (runtime *Runtime) containerRoot(id string) string {
  103. return path.Join(runtime.repository, id)
  104. }
  105. // Load reads the contents of a container from disk and registers
  106. // it with Register.
  107. // This is typically done at startup.
  108. func (runtime *Runtime) load(id string) (*Container, error) {
  109. container := &Container{root: runtime.containerRoot(id)}
  110. if err := container.FromDisk(); err != nil {
  111. return nil, err
  112. }
  113. if container.ID != id {
  114. return container, fmt.Errorf("Container %s is stored at %s", container.ID, id)
  115. }
  116. if container.State.Running {
  117. container.State.Ghost = true
  118. }
  119. return container, nil
  120. }
  121. // Register makes a container object usable by the runtime as <container.ID>
  122. func (runtime *Runtime) Register(container *Container) error {
  123. if container.runtime != nil || runtime.Exists(container.ID) {
  124. return fmt.Errorf("Container is already loaded")
  125. }
  126. if err := validateID(container.ID); err != nil {
  127. return err
  128. }
  129. // init the wait lock
  130. container.waitLock = make(chan struct{})
  131. container.runtime = runtime
  132. // Attach to stdout and stderr
  133. container.stderr = utils.NewWriteBroadcaster()
  134. container.stdout = utils.NewWriteBroadcaster()
  135. // Attach to stdin
  136. if container.Config.OpenStdin {
  137. container.stdin, container.stdinPipe = io.Pipe()
  138. } else {
  139. container.stdinPipe = utils.NopWriteCloser(ioutil.Discard) // Silently drop stdin
  140. }
  141. // done
  142. runtime.containers.PushBack(container)
  143. runtime.idIndex.Add(container.ID)
  144. // When we actually restart, Start() do the monitoring.
  145. // However, when we simply 'reattach', we have to restart a monitor
  146. nomonitor := false
  147. // FIXME: if the container is supposed to be running but is not, auto restart it?
  148. // if so, then we need to restart monitor and init a new lock
  149. // If the container is supposed to be running, make sure of it
  150. if container.State.Running {
  151. output, err := exec.Command("lxc-info", "-n", container.ID).CombinedOutput()
  152. if err != nil {
  153. return err
  154. }
  155. if !strings.Contains(string(output), "RUNNING") {
  156. utils.Debugf("Container %s was supposed to be running be is not.", container.ID)
  157. if runtime.autoRestart {
  158. utils.Debugf("Restarting")
  159. container.State.Ghost = false
  160. container.State.setStopped(0)
  161. hostConfig := &HostConfig{}
  162. if err := container.Start(hostConfig); err != nil {
  163. return err
  164. }
  165. nomonitor = true
  166. } else {
  167. utils.Debugf("Marking as stopped")
  168. container.State.setStopped(-127)
  169. if err := container.ToDisk(); err != nil {
  170. return err
  171. }
  172. }
  173. }
  174. }
  175. // If the container is not running or just has been flagged not running
  176. // then close the wait lock chan (will be reset upon start)
  177. if !container.State.Running {
  178. close(container.waitLock)
  179. } else if !nomonitor {
  180. container.allocateNetwork()
  181. // hostConfig isn't needed here and can be nil
  182. go container.monitor(nil)
  183. }
  184. return nil
  185. }
  186. func (runtime *Runtime) LogToDisk(src *utils.WriteBroadcaster, dst, stream string) error {
  187. log, err := os.OpenFile(dst, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0600)
  188. if err != nil {
  189. return err
  190. }
  191. src.AddWriter(log, stream)
  192. return nil
  193. }
  194. // Destroy unregisters a container from the runtime and cleanly removes its contents from the filesystem.
  195. func (runtime *Runtime) Destroy(container *Container) error {
  196. if container == nil {
  197. return fmt.Errorf("The given container is <nil>")
  198. }
  199. element := runtime.getContainerElement(container.ID)
  200. if element == nil {
  201. return fmt.Errorf("Container %v not found - maybe it was already destroyed?", container.ID)
  202. }
  203. if err := container.Stop(3); err != nil {
  204. return err
  205. }
  206. if mounted, err := container.Mounted(); err != nil {
  207. return err
  208. } else if mounted {
  209. if err := container.Unmount(); err != nil {
  210. return fmt.Errorf("Unable to unmount container %v: %v", container.ID, err)
  211. }
  212. }
  213. // Deregister the container before removing its directory, to avoid race conditions
  214. runtime.idIndex.Delete(container.ID)
  215. runtime.containers.Remove(element)
  216. if err := os.RemoveAll(container.root); err != nil {
  217. return fmt.Errorf("Unable to remove filesystem for %v: %v", container.ID, err)
  218. }
  219. if runtime.deviceSet.HasDevice(container.ID) {
  220. if err := runtime.deviceSet.RemoveDevice(container.ID); err != nil {
  221. return fmt.Errorf("Unable to remove device for %v: %v", container.ID, err)
  222. }
  223. }
  224. return nil
  225. }
  226. func (runtime *Runtime) DeleteImage(id string) error {
  227. err := runtime.graph.Delete(id)
  228. if err != nil {
  229. return err
  230. }
  231. if runtime.deviceSet.HasDevice(id) {
  232. if err := runtime.deviceSet.RemoveDevice(id); err != nil {
  233. return fmt.Errorf("Unable to remove device for %v: %v", id, err)
  234. }
  235. }
  236. return nil
  237. }
  238. func (runtime *Runtime) restore() error {
  239. wheel := "-\\|/"
  240. if os.Getenv("DEBUG") == "" && os.Getenv("TEST") == "" {
  241. fmt.Printf("Loading containers: ")
  242. }
  243. dir, err := ioutil.ReadDir(runtime.repository)
  244. if err != nil {
  245. return err
  246. }
  247. var (
  248. containers []*Container
  249. containersToMigrate []*Container
  250. )
  251. for i, v := range dir {
  252. id := v.Name()
  253. container, err := runtime.load(id)
  254. if i%21 == 0 && os.Getenv("DEBUG") == "" && os.Getenv("TEST") == "" {
  255. fmt.Printf("\b%c", wheel[i%4])
  256. }
  257. if err != nil {
  258. utils.Errorf("Failed to load container %v: %v", id, err)
  259. continue
  260. }
  261. utils.Debugf("Loaded container %v", container.ID)
  262. containers = append(containers, container)
  263. if container.FilesystemType != DefaultFilesystemType {
  264. containersToMigrate = append(containersToMigrate, container)
  265. }
  266. }
  267. // Migrate containers to the default filesystem type
  268. if len(containersToMigrate) > 0 {
  269. if err := migrateToDeviceMapper(runtime, containersToMigrate); err != nil {
  270. return err
  271. }
  272. }
  273. for _, container := range containers {
  274. if err := runtime.Register(container); err != nil {
  275. utils.Debugf("Failed to register container %s: %s", container.ID, err)
  276. continue
  277. }
  278. }
  279. if os.Getenv("DEBUG") == "" && os.Getenv("TEST") == "" {
  280. fmt.Printf("\bdone.\n")
  281. }
  282. return nil
  283. }
  284. func migrateToDeviceMapper(runtime *Runtime, containers []*Container) error {
  285. var (
  286. image *Image
  287. contents []os.FileInfo
  288. err error
  289. )
  290. fmt.Printf("Migrating %d containers to new storage backend\n", len(containers))
  291. for _, container := range containers {
  292. if container.State.Running {
  293. fmt.Printf("WARNING - Cannot migrate %s because the container is running. Please stop the container and relaunch the daemon!")
  294. continue
  295. }
  296. fmt.Printf("Migrating %s\n", container.ID)
  297. if contents, err = ioutil.ReadDir(container.rwPath()); err != nil {
  298. if !os.IsNotExist(err) {
  299. fmt.Printf("Error reading rw dir %s\n", err)
  300. }
  301. continue
  302. }
  303. if len(contents) == 0 {
  304. fmt.Printf("Skipping migration of %s because rw layer contains no changes\n")
  305. continue
  306. }
  307. if image, err = runtime.graph.Get(container.Image); err != nil {
  308. fmt.Printf("Failed to fetch image %s\n", err)
  309. continue
  310. }
  311. unmount := func() {
  312. if err = image.Unmount(runtime, container.RootfsPath(), container.ID); err != nil {
  313. fmt.Printf("Failed to unmount image %s\n", err)
  314. }
  315. }
  316. if err = image.Mount(runtime, container.RootfsPath(), container.rwPath(), container.ID); err != nil {
  317. fmt.Printf("Failed to mount image %s\n", err)
  318. continue
  319. }
  320. if err = image.applyLayer(container.rwPath(), container.RootfsPath()); err != nil {
  321. fmt.Printf("Failed to apply layer in storage backend %s\n", err)
  322. unmount()
  323. continue
  324. }
  325. unmount()
  326. if err = os.RemoveAll(container.rwPath()); err != nil {
  327. fmt.Printf("Failed to remove rw layer %s\n", err)
  328. }
  329. container.FilesystemType = DefaultFilesystemType
  330. if err := container.ToDisk(); err != nil {
  331. fmt.Printf("Failed to save filesystem type to disk %s\n", err)
  332. }
  333. fmt.Printf("Successful migration for %s\n", container.ID)
  334. }
  335. fmt.Printf("Migration complete\n")
  336. return nil
  337. }
  338. // FIXME: comment please!
  339. func (runtime *Runtime) UpdateCapabilities(quiet bool) {
  340. if cgroupMemoryMountpoint, err := utils.FindCgroupMountpoint("memory"); err != nil {
  341. if !quiet {
  342. log.Printf("WARNING: %s\n", err)
  343. }
  344. } else {
  345. _, err1 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.limit_in_bytes"))
  346. _, err2 := ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.soft_limit_in_bytes"))
  347. runtime.capabilities.MemoryLimit = err1 == nil && err2 == nil
  348. if !runtime.capabilities.MemoryLimit && !quiet {
  349. log.Printf("WARNING: Your kernel does not support cgroup memory limit.")
  350. }
  351. _, err = ioutil.ReadFile(path.Join(cgroupMemoryMountpoint, "memory.memsw.limit_in_bytes"))
  352. runtime.capabilities.SwapLimit = err == nil
  353. if !runtime.capabilities.SwapLimit && !quiet {
  354. log.Printf("WARNING: Your kernel does not support cgroup swap limit.")
  355. }
  356. }
  357. content, err3 := ioutil.ReadFile("/proc/sys/net/ipv4/ip_forward")
  358. runtime.capabilities.IPv4ForwardingDisabled = err3 != nil || len(content) == 0 || content[0] != '1'
  359. if runtime.capabilities.IPv4ForwardingDisabled && !quiet {
  360. log.Printf("WARNING: IPv4 forwarding is disabled.")
  361. }
  362. }
  363. // Create creates a new container from the given configuration.
  364. func (runtime *Runtime) Create(config *Config) (*Container, error) {
  365. // Lookup image
  366. img, err := runtime.repositories.LookupImage(config.Image)
  367. if err != nil {
  368. return nil, err
  369. }
  370. if img.Config != nil {
  371. MergeConfig(config, img.Config)
  372. }
  373. if len(config.Entrypoint) != 0 && config.Cmd == nil {
  374. config.Cmd = []string{}
  375. } else if config.Cmd == nil || len(config.Cmd) == 0 {
  376. return nil, fmt.Errorf("No command specified")
  377. }
  378. // Generate id
  379. id := GenerateID()
  380. // Generate default hostname
  381. // FIXME: the lxc template no longer needs to set a default hostname
  382. if config.Hostname == "" {
  383. config.Hostname = id[:12]
  384. }
  385. var args []string
  386. var entrypoint string
  387. if len(config.Entrypoint) != 0 {
  388. entrypoint = config.Entrypoint[0]
  389. args = append(config.Entrypoint[1:], config.Cmd...)
  390. } else {
  391. entrypoint = config.Cmd[0]
  392. args = config.Cmd[1:]
  393. }
  394. container := &Container{
  395. // FIXME: we should generate the ID here instead of receiving it as an argument
  396. ID: id,
  397. Created: time.Now(),
  398. Path: entrypoint,
  399. Args: args, //FIXME: de-duplicate from config
  400. Config: config,
  401. Image: img.ID, // Always use the resolved image id
  402. NetworkSettings: &NetworkSettings{},
  403. // FIXME: do we need to store this in the container?
  404. SysInitPath: sysInitPath,
  405. FilesystemType: DefaultFilesystemType,
  406. }
  407. container.root = runtime.containerRoot(container.ID)
  408. // Step 1: create the container directory.
  409. // This doubles as a barrier to avoid race conditions.
  410. if err := os.Mkdir(container.root, 0700); err != nil {
  411. return nil, err
  412. }
  413. resolvConf, err := utils.GetResolvConf()
  414. if err != nil {
  415. return nil, err
  416. }
  417. if len(config.Dns) == 0 && len(runtime.Dns) == 0 && utils.CheckLocalDns(resolvConf) {
  418. //"WARNING: Docker detected local DNS server on resolv.conf. Using default external servers: %v", defaultDns
  419. runtime.Dns = defaultDns
  420. }
  421. // If custom dns exists, then create a resolv.conf for the container
  422. if len(config.Dns) > 0 || len(runtime.Dns) > 0 {
  423. var dns []string
  424. if len(config.Dns) > 0 {
  425. dns = config.Dns
  426. } else {
  427. dns = runtime.Dns
  428. }
  429. container.ResolvConfPath = path.Join(container.root, "resolv.conf")
  430. f, err := os.Create(container.ResolvConfPath)
  431. if err != nil {
  432. return nil, err
  433. }
  434. defer f.Close()
  435. for _, dns := range dns {
  436. if _, err := f.Write([]byte("nameserver " + dns + "\n")); err != nil {
  437. return nil, err
  438. }
  439. }
  440. } else {
  441. container.ResolvConfPath = "/etc/resolv.conf"
  442. }
  443. // Step 2: save the container json
  444. if err := container.ToDisk(); err != nil {
  445. return nil, err
  446. }
  447. // Step 3: if hostname, build hostname and hosts files
  448. container.HostnamePath = path.Join(container.root, "hostname")
  449. ioutil.WriteFile(container.HostnamePath, []byte(container.Config.Hostname+"\n"), 0644)
  450. hostsContent := []byte(`
  451. 127.0.0.1 localhost
  452. ::1 localhost ip6-localhost ip6-loopback
  453. fe00::0 ip6-localnet
  454. ff00::0 ip6-mcastprefix
  455. ff02::1 ip6-allnodes
  456. ff02::2 ip6-allrouters
  457. `)
  458. container.HostsPath = path.Join(container.root, "hosts")
  459. if container.Config.Domainname != "" {
  460. hostsContent = append([]byte(fmt.Sprintf("::1\t\t%s.%s %s\n", container.Config.Hostname, container.Config.Domainname, container.Config.Hostname)), hostsContent...)
  461. hostsContent = append([]byte(fmt.Sprintf("127.0.0.1\t%s.%s %s\n", container.Config.Hostname, container.Config.Domainname, container.Config.Hostname)), hostsContent...)
  462. } else {
  463. hostsContent = append([]byte(fmt.Sprintf("::1\t\t%s\n", container.Config.Hostname)), hostsContent...)
  464. hostsContent = append([]byte(fmt.Sprintf("127.0.0.1\t%s\n", container.Config.Hostname)), hostsContent...)
  465. }
  466. ioutil.WriteFile(container.HostsPath, hostsContent, 0644)
  467. // Step 4: register the container
  468. if err := runtime.Register(container); err != nil {
  469. return nil, err
  470. }
  471. return container, nil
  472. }
  473. // Commit creates a new filesystem image from the current state of a container.
  474. // The image can optionally be tagged into a repository
  475. func (runtime *Runtime) Commit(container *Container, repository, tag, comment, author string, config *Config) (*Image, error) {
  476. // FIXME: freeze the container before copying it to avoid data corruption?
  477. // FIXME: this shouldn't be in commands.
  478. if err := container.EnsureMounted(); err != nil {
  479. return nil, err
  480. }
  481. rwTar, err := container.ExportRw()
  482. if err != nil {
  483. return nil, err
  484. }
  485. // Create a new image from the container's base layers + a new layer from container changes
  486. img, err := runtime.graph.Create(rwTar, container, comment, author, config)
  487. if err != nil {
  488. return nil, err
  489. }
  490. // Register the image if needed
  491. if repository != "" {
  492. if err := runtime.repositories.Set(repository, tag, img.ID, true); err != nil {
  493. return img, err
  494. }
  495. }
  496. return img, nil
  497. }
  498. // FIXME: harmonize with NewGraph()
  499. func NewRuntime(flGraphPath string, autoRestart bool, dns []string) (*Runtime, error) {
  500. runtime, err := NewRuntimeFromDirectory(flGraphPath, autoRestart)
  501. if err != nil {
  502. return nil, err
  503. }
  504. runtime.Dns = dns
  505. if k, err := utils.GetKernelVersion(); err != nil {
  506. log.Printf("WARNING: %s\n", err)
  507. } else {
  508. runtime.kernelVersion = k
  509. if utils.CompareKernelVersion(k, &utils.KernelVersionInfo{Kernel: 3, Major: 8, Minor: 0}) < 0 {
  510. log.Printf("WARNING: You are running linux kernel version %s, which might be unstable running docker. Please upgrade your kernel to 3.8.0.", k.String())
  511. }
  512. }
  513. runtime.UpdateCapabilities(false)
  514. return runtime, nil
  515. }
  516. func NewRuntimeFromDirectory(root string, autoRestart bool) (*Runtime, error) {
  517. runtimeRepo := path.Join(root, "containers")
  518. if err := os.MkdirAll(runtimeRepo, 0700); err != nil && !os.IsExist(err) {
  519. return nil, err
  520. }
  521. g, err := NewGraph(path.Join(root, "graph"))
  522. if err != nil {
  523. return nil, err
  524. }
  525. volumes, err := NewGraph(path.Join(root, "volumes"))
  526. if err != nil {
  527. return nil, err
  528. }
  529. repositories, err := NewTagStore(path.Join(root, "repositories"), g)
  530. if err != nil {
  531. return nil, fmt.Errorf("Couldn't create Tag store: %s", err)
  532. }
  533. if NetworkBridgeIface == "" {
  534. NetworkBridgeIface = DefaultNetworkBridge
  535. }
  536. netManager, err := newNetworkManager(NetworkBridgeIface)
  537. if err != nil {
  538. return nil, err
  539. }
  540. deviceSet := devmapper.NewDeviceSet(root)
  541. // Initialize devicemapper deviceSet
  542. runtime := &Runtime{
  543. root: root,
  544. repository: runtimeRepo,
  545. containers: list.New(),
  546. networkManager: netManager,
  547. graph: g,
  548. repositories: repositories,
  549. idIndex: utils.NewTruncIndex(),
  550. capabilities: &Capabilities{},
  551. autoRestart: autoRestart,
  552. volumes: volumes,
  553. deviceSet: deviceSet,
  554. }
  555. if err := runtime.restore(); err != nil {
  556. return nil, err
  557. }
  558. return runtime, nil
  559. }
  560. // History is a convenience type for storing a list of containers,
  561. // ordered by creation date.
  562. type History []*Container
  563. func (history *History) Len() int {
  564. return len(*history)
  565. }
  566. func (history *History) Less(i, j int) bool {
  567. containers := *history
  568. return containers[j].When().Before(containers[i].When())
  569. }
  570. func (history *History) Swap(i, j int) {
  571. containers := *history
  572. tmp := containers[i]
  573. containers[i] = containers[j]
  574. containers[j] = tmp
  575. }
  576. func (history *History) Add(container *Container) {
  577. *history = append(*history, container)
  578. sort.Sort(history)
  579. }