runtime.go 15 KB

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