container.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. package docker
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/dotcloud/docker/archive"
  7. "github.com/dotcloud/docker/engine"
  8. "github.com/dotcloud/docker/execdriver"
  9. "github.com/dotcloud/docker/graphdriver"
  10. "github.com/dotcloud/docker/links"
  11. "github.com/dotcloud/docker/nat"
  12. "github.com/dotcloud/docker/runconfig"
  13. "github.com/dotcloud/docker/utils"
  14. "io"
  15. "io/ioutil"
  16. "log"
  17. "os"
  18. "path"
  19. "strings"
  20. "sync"
  21. "syscall"
  22. "time"
  23. )
  24. const defaultPathEnv = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  25. var (
  26. ErrNotATTY = errors.New("The PTY is not a file")
  27. ErrNoTTY = errors.New("No PTY found")
  28. ErrContainerStart = errors.New("The container failed to start. Unknown error")
  29. ErrContainerStartTimeout = errors.New("The container failed to start due to timed out.")
  30. )
  31. type Container struct {
  32. sync.Mutex
  33. root string // Path to the "home" of the container, including metadata.
  34. basefs string // Path to the graphdriver mountpoint
  35. ID string
  36. Created time.Time
  37. Path string
  38. Args []string
  39. Config *runconfig.Config
  40. State State
  41. Image string
  42. NetworkSettings *NetworkSettings
  43. ResolvConfPath string
  44. HostnamePath string
  45. HostsPath string
  46. Name string
  47. Driver string
  48. ExecDriver string
  49. command *execdriver.Command
  50. stdout *utils.WriteBroadcaster
  51. stderr *utils.WriteBroadcaster
  52. stdin io.ReadCloser
  53. stdinPipe io.WriteCloser
  54. runtime *Runtime
  55. waitLock chan struct{}
  56. Volumes map[string]string
  57. // Store rw/ro in a separate structure to preserve reverse-compatibility on-disk.
  58. // Easier than migrating older container configs :)
  59. VolumesRW map[string]bool
  60. hostConfig *runconfig.HostConfig
  61. activeLinks map[string]*links.Link
  62. }
  63. // FIXME: move deprecated port stuff to nat to clean up the core.
  64. type PortMapping map[string]string // Deprecated
  65. type NetworkSettings struct {
  66. IPAddress string
  67. IPPrefixLen int
  68. Gateway string
  69. Bridge string
  70. PortMapping map[string]PortMapping // Deprecated
  71. Ports nat.PortMap
  72. }
  73. func (settings *NetworkSettings) PortMappingAPI() *engine.Table {
  74. var outs = engine.NewTable("", 0)
  75. for port, bindings := range settings.Ports {
  76. p, _ := nat.ParsePort(port.Port())
  77. if len(bindings) == 0 {
  78. out := &engine.Env{}
  79. out.SetInt("PublicPort", p)
  80. out.Set("Type", port.Proto())
  81. outs.Add(out)
  82. continue
  83. }
  84. for _, binding := range bindings {
  85. out := &engine.Env{}
  86. h, _ := nat.ParsePort(binding.HostPort)
  87. out.SetInt("PrivatePort", p)
  88. out.SetInt("PublicPort", h)
  89. out.Set("Type", port.Proto())
  90. out.Set("IP", binding.HostIp)
  91. outs.Add(out)
  92. }
  93. }
  94. return outs
  95. }
  96. // Inject the io.Reader at the given path. Note: do not close the reader
  97. func (container *Container) Inject(file io.Reader, pth string) error {
  98. if err := container.Mount(); err != nil {
  99. return fmt.Errorf("inject: error mounting container %s: %s", container.ID, err)
  100. }
  101. defer container.Unmount()
  102. // Return error if path exists
  103. destPath := path.Join(container.basefs, pth)
  104. if _, err := os.Stat(destPath); err == nil {
  105. // Since err is nil, the path could be stat'd and it exists
  106. return fmt.Errorf("%s exists", pth)
  107. } else if !os.IsNotExist(err) {
  108. // Expect err might be that the file doesn't exist, so
  109. // if it's some other error, return that.
  110. return err
  111. }
  112. // Make sure the directory exists
  113. if err := os.MkdirAll(path.Join(container.basefs, path.Dir(pth)), 0755); err != nil {
  114. return err
  115. }
  116. dest, err := os.Create(destPath)
  117. if err != nil {
  118. return err
  119. }
  120. defer dest.Close()
  121. if _, err := io.Copy(dest, file); err != nil {
  122. return err
  123. }
  124. return nil
  125. }
  126. func (container *Container) When() time.Time {
  127. return container.Created
  128. }
  129. func (container *Container) FromDisk() error {
  130. data, err := ioutil.ReadFile(container.jsonPath())
  131. if err != nil {
  132. return err
  133. }
  134. // Load container settings
  135. // udp broke compat of docker.PortMapping, but it's not used when loading a container, we can skip it
  136. if err := json.Unmarshal(data, container); err != nil && !strings.Contains(err.Error(), "docker.PortMapping") {
  137. return err
  138. }
  139. return container.readHostConfig()
  140. }
  141. func (container *Container) ToDisk() (err error) {
  142. data, err := json.Marshal(container)
  143. if err != nil {
  144. return
  145. }
  146. err = ioutil.WriteFile(container.jsonPath(), data, 0666)
  147. if err != nil {
  148. return
  149. }
  150. return container.writeHostConfig()
  151. }
  152. func (container *Container) readHostConfig() error {
  153. container.hostConfig = &runconfig.HostConfig{}
  154. // If the hostconfig file does not exist, do not read it.
  155. // (We still have to initialize container.hostConfig,
  156. // but that's OK, since we just did that above.)
  157. _, err := os.Stat(container.hostConfigPath())
  158. if os.IsNotExist(err) {
  159. return nil
  160. }
  161. data, err := ioutil.ReadFile(container.hostConfigPath())
  162. if err != nil {
  163. return err
  164. }
  165. return json.Unmarshal(data, container.hostConfig)
  166. }
  167. func (container *Container) writeHostConfig() (err error) {
  168. data, err := json.Marshal(container.hostConfig)
  169. if err != nil {
  170. return
  171. }
  172. return ioutil.WriteFile(container.hostConfigPath(), data, 0666)
  173. }
  174. func (container *Container) generateEnvConfig(env []string) error {
  175. data, err := json.Marshal(env)
  176. if err != nil {
  177. return err
  178. }
  179. p, err := container.EnvConfigPath()
  180. if err != nil {
  181. return err
  182. }
  183. ioutil.WriteFile(p, data, 0600)
  184. return nil
  185. }
  186. func (container *Container) Attach(stdin io.ReadCloser, stdinCloser io.Closer, stdout io.Writer, stderr io.Writer) chan error {
  187. var cStdout, cStderr io.ReadCloser
  188. var nJobs int
  189. errors := make(chan error, 3)
  190. if stdin != nil && container.Config.OpenStdin {
  191. nJobs += 1
  192. if cStdin, err := container.StdinPipe(); err != nil {
  193. errors <- err
  194. } else {
  195. go func() {
  196. utils.Debugf("attach: stdin: begin")
  197. defer utils.Debugf("attach: stdin: end")
  198. // No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr
  199. if container.Config.StdinOnce && !container.Config.Tty {
  200. defer cStdin.Close()
  201. } else {
  202. defer func() {
  203. if cStdout != nil {
  204. cStdout.Close()
  205. }
  206. if cStderr != nil {
  207. cStderr.Close()
  208. }
  209. }()
  210. }
  211. if container.Config.Tty {
  212. _, err = utils.CopyEscapable(cStdin, stdin)
  213. } else {
  214. _, err = io.Copy(cStdin, stdin)
  215. }
  216. if err == io.ErrClosedPipe {
  217. err = nil
  218. }
  219. if err != nil {
  220. utils.Errorf("attach: stdin: %s", err)
  221. }
  222. errors <- err
  223. }()
  224. }
  225. }
  226. if stdout != nil {
  227. nJobs += 1
  228. if p, err := container.StdoutPipe(); err != nil {
  229. errors <- err
  230. } else {
  231. cStdout = p
  232. go func() {
  233. utils.Debugf("attach: stdout: begin")
  234. defer utils.Debugf("attach: stdout: end")
  235. // If we are in StdinOnce mode, then close stdin
  236. if container.Config.StdinOnce && stdin != nil {
  237. defer stdin.Close()
  238. }
  239. if stdinCloser != nil {
  240. defer stdinCloser.Close()
  241. }
  242. _, err := io.Copy(stdout, cStdout)
  243. if err == io.ErrClosedPipe {
  244. err = nil
  245. }
  246. if err != nil {
  247. utils.Errorf("attach: stdout: %s", err)
  248. }
  249. errors <- err
  250. }()
  251. }
  252. } else {
  253. go func() {
  254. if stdinCloser != nil {
  255. defer stdinCloser.Close()
  256. }
  257. if cStdout, err := container.StdoutPipe(); err != nil {
  258. utils.Errorf("attach: stdout pipe: %s", err)
  259. } else {
  260. io.Copy(&utils.NopWriter{}, cStdout)
  261. }
  262. }()
  263. }
  264. if stderr != nil {
  265. nJobs += 1
  266. if p, err := container.StderrPipe(); err != nil {
  267. errors <- err
  268. } else {
  269. cStderr = p
  270. go func() {
  271. utils.Debugf("attach: stderr: begin")
  272. defer utils.Debugf("attach: stderr: end")
  273. // If we are in StdinOnce mode, then close stdin
  274. if container.Config.StdinOnce && stdin != nil {
  275. defer stdin.Close()
  276. }
  277. if stdinCloser != nil {
  278. defer stdinCloser.Close()
  279. }
  280. _, err := io.Copy(stderr, cStderr)
  281. if err == io.ErrClosedPipe {
  282. err = nil
  283. }
  284. if err != nil {
  285. utils.Errorf("attach: stderr: %s", err)
  286. }
  287. errors <- err
  288. }()
  289. }
  290. } else {
  291. go func() {
  292. if stdinCloser != nil {
  293. defer stdinCloser.Close()
  294. }
  295. if cStderr, err := container.StderrPipe(); err != nil {
  296. utils.Errorf("attach: stdout pipe: %s", err)
  297. } else {
  298. io.Copy(&utils.NopWriter{}, cStderr)
  299. }
  300. }()
  301. }
  302. return utils.Go(func() error {
  303. defer func() {
  304. if cStdout != nil {
  305. cStdout.Close()
  306. }
  307. if cStderr != nil {
  308. cStderr.Close()
  309. }
  310. }()
  311. // FIXME: how to clean up the stdin goroutine without the unwanted side effect
  312. // of closing the passed stdin? Add an intermediary io.Pipe?
  313. for i := 0; i < nJobs; i += 1 {
  314. utils.Debugf("attach: waiting for job %d/%d", i+1, nJobs)
  315. if err := <-errors; err != nil {
  316. utils.Errorf("attach: job %d returned error %s, aborting all jobs", i+1, err)
  317. return err
  318. }
  319. utils.Debugf("attach: job %d completed successfully", i+1)
  320. }
  321. utils.Debugf("attach: all jobs completed successfully")
  322. return nil
  323. })
  324. }
  325. func populateCommand(c *Container) {
  326. var (
  327. en *execdriver.Network
  328. driverConfig []string
  329. )
  330. if !c.Config.NetworkDisabled {
  331. network := c.NetworkSettings
  332. en = &execdriver.Network{
  333. Gateway: network.Gateway,
  334. Bridge: network.Bridge,
  335. IPAddress: network.IPAddress,
  336. IPPrefixLen: network.IPPrefixLen,
  337. Mtu: c.runtime.config.Mtu,
  338. }
  339. }
  340. if lxcConf := c.hostConfig.LxcConf; lxcConf != nil {
  341. for _, pair := range lxcConf {
  342. driverConfig = append(driverConfig, fmt.Sprintf("%s = %s", pair.Key, pair.Value))
  343. }
  344. }
  345. resources := &execdriver.Resources{
  346. Memory: c.Config.Memory,
  347. MemorySwap: c.Config.MemorySwap,
  348. CpuShares: c.Config.CpuShares,
  349. }
  350. c.command = &execdriver.Command{
  351. ID: c.ID,
  352. Privileged: c.hostConfig.Privileged,
  353. Rootfs: c.RootfsPath(),
  354. InitPath: "/.dockerinit",
  355. Entrypoint: c.Path,
  356. Arguments: c.Args,
  357. WorkingDir: c.Config.WorkingDir,
  358. Network: en,
  359. Tty: c.Config.Tty,
  360. User: c.Config.User,
  361. Config: driverConfig,
  362. Resources: resources,
  363. }
  364. c.command.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
  365. }
  366. func (container *Container) Start() (err error) {
  367. container.Lock()
  368. defer container.Unlock()
  369. if container.State.IsRunning() {
  370. return fmt.Errorf("The container %s is already running.", container.ID)
  371. }
  372. defer func() {
  373. if err != nil {
  374. container.cleanup()
  375. }
  376. }()
  377. if err := container.Mount(); err != nil {
  378. return err
  379. }
  380. if container.runtime.config.DisableNetwork {
  381. container.Config.NetworkDisabled = true
  382. container.buildHostnameAndHostsFiles("127.0.1.1")
  383. } else {
  384. if err := container.allocateNetwork(); err != nil {
  385. return err
  386. }
  387. container.buildHostnameAndHostsFiles(container.NetworkSettings.IPAddress)
  388. }
  389. // Make sure the config is compatible with the current kernel
  390. if container.Config.Memory > 0 && !container.runtime.sysInfo.MemoryLimit {
  391. log.Printf("WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n")
  392. container.Config.Memory = 0
  393. }
  394. if container.Config.Memory > 0 && !container.runtime.sysInfo.SwapLimit {
  395. log.Printf("WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.\n")
  396. container.Config.MemorySwap = -1
  397. }
  398. if container.runtime.sysInfo.IPv4ForwardingDisabled {
  399. log.Printf("WARNING: IPv4 forwarding is disabled. Networking will not work")
  400. }
  401. if err := prepareVolumesForContainer(container); err != nil {
  402. return err
  403. }
  404. // Setup environment
  405. env := []string{
  406. "HOME=/",
  407. "PATH=" + defaultPathEnv,
  408. "HOSTNAME=" + container.Config.Hostname,
  409. }
  410. if container.Config.Tty {
  411. env = append(env, "TERM=xterm")
  412. }
  413. // Init any links between the parent and children
  414. runtime := container.runtime
  415. children, err := runtime.Children(container.Name)
  416. if err != nil {
  417. return err
  418. }
  419. if len(children) > 0 {
  420. container.activeLinks = make(map[string]*links.Link, len(children))
  421. // If we encounter an error make sure that we rollback any network
  422. // config and ip table changes
  423. rollback := func() {
  424. for _, link := range container.activeLinks {
  425. link.Disable()
  426. }
  427. container.activeLinks = nil
  428. }
  429. for linkAlias, child := range children {
  430. if !child.State.IsRunning() {
  431. return fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
  432. }
  433. link, err := links.NewLink(
  434. container.NetworkSettings.IPAddress,
  435. child.NetworkSettings.IPAddress,
  436. linkAlias,
  437. child.Config.Env,
  438. child.Config.ExposedPorts,
  439. runtime.eng)
  440. if err != nil {
  441. rollback()
  442. return err
  443. }
  444. container.activeLinks[link.Alias()] = link
  445. if err := link.Enable(); err != nil {
  446. rollback()
  447. return err
  448. }
  449. for _, envVar := range link.ToEnv() {
  450. env = append(env, envVar)
  451. }
  452. }
  453. }
  454. // because the env on the container can override certain default values
  455. // we need to replace the 'env' keys where they match and append anything
  456. // else.
  457. env = utils.ReplaceOrAppendEnvValues(env, container.Config.Env)
  458. if err := container.generateEnvConfig(env); err != nil {
  459. return err
  460. }
  461. if container.Config.WorkingDir != "" {
  462. container.Config.WorkingDir = path.Clean(container.Config.WorkingDir)
  463. if err := os.MkdirAll(path.Join(container.basefs, container.Config.WorkingDir), 0755); err != nil {
  464. return nil
  465. }
  466. }
  467. envPath, err := container.EnvConfigPath()
  468. if err != nil {
  469. return err
  470. }
  471. if err := mountVolumesForContainer(container, envPath); err != nil {
  472. return err
  473. }
  474. populateCommand(container)
  475. container.command.Env = env
  476. // Setup logging of stdout and stderr to disk
  477. if err := container.runtime.LogToDisk(container.stdout, container.logPath("json"), "stdout"); err != nil {
  478. return err
  479. }
  480. if err := container.runtime.LogToDisk(container.stderr, container.logPath("json"), "stderr"); err != nil {
  481. return err
  482. }
  483. container.waitLock = make(chan struct{})
  484. callbackLock := make(chan struct{})
  485. callback := func(command *execdriver.Command) {
  486. container.State.SetRunning(command.Pid())
  487. if command.Tty {
  488. // The callback is called after the process Start()
  489. // so we are in the parent process. In TTY mode, stdin/out/err is the PtySlace
  490. // which we close here.
  491. if c, ok := command.Stdout.(io.Closer); ok {
  492. c.Close()
  493. }
  494. }
  495. if err := container.ToDisk(); err != nil {
  496. utils.Debugf("%s", err)
  497. }
  498. close(callbackLock)
  499. }
  500. // We use a callback here instead of a goroutine and an chan for
  501. // syncronization purposes
  502. cErr := utils.Go(func() error { return container.monitor(callback) })
  503. // Start should not return until the process is actually running
  504. select {
  505. case <-callbackLock:
  506. case err := <-cErr:
  507. return err
  508. }
  509. return nil
  510. }
  511. func (container *Container) Run() error {
  512. if err := container.Start(); err != nil {
  513. return err
  514. }
  515. container.Wait()
  516. return nil
  517. }
  518. func (container *Container) Output() (output []byte, err error) {
  519. pipe, err := container.StdoutPipe()
  520. if err != nil {
  521. return nil, err
  522. }
  523. defer pipe.Close()
  524. if err := container.Start(); err != nil {
  525. return nil, err
  526. }
  527. output, err = ioutil.ReadAll(pipe)
  528. container.Wait()
  529. return output, err
  530. }
  531. // Container.StdinPipe returns a WriteCloser which can be used to feed data
  532. // to the standard input of the container's active process.
  533. // Container.StdoutPipe and Container.StderrPipe each return a ReadCloser
  534. // which can be used to retrieve the standard output (and error) generated
  535. // by the container's active process. The output (and error) are actually
  536. // copied and delivered to all StdoutPipe and StderrPipe consumers, using
  537. // a kind of "broadcaster".
  538. func (container *Container) StdinPipe() (io.WriteCloser, error) {
  539. return container.stdinPipe, nil
  540. }
  541. func (container *Container) StdoutPipe() (io.ReadCloser, error) {
  542. reader, writer := io.Pipe()
  543. container.stdout.AddWriter(writer, "")
  544. return utils.NewBufReader(reader), nil
  545. }
  546. func (container *Container) StderrPipe() (io.ReadCloser, error) {
  547. reader, writer := io.Pipe()
  548. container.stderr.AddWriter(writer, "")
  549. return utils.NewBufReader(reader), nil
  550. }
  551. func (container *Container) buildHostnameAndHostsFiles(IP string) {
  552. container.HostnamePath = path.Join(container.root, "hostname")
  553. ioutil.WriteFile(container.HostnamePath, []byte(container.Config.Hostname+"\n"), 0644)
  554. hostsContent := []byte(`
  555. 127.0.0.1 localhost
  556. ::1 localhost ip6-localhost ip6-loopback
  557. fe00::0 ip6-localnet
  558. ff00::0 ip6-mcastprefix
  559. ff02::1 ip6-allnodes
  560. ff02::2 ip6-allrouters
  561. `)
  562. container.HostsPath = path.Join(container.root, "hosts")
  563. if container.Config.Domainname != "" {
  564. hostsContent = append([]byte(fmt.Sprintf("%s\t%s.%s %s\n", IP, container.Config.Hostname, container.Config.Domainname, container.Config.Hostname)), hostsContent...)
  565. } else if !container.Config.NetworkDisabled {
  566. hostsContent = append([]byte(fmt.Sprintf("%s\t%s\n", IP, container.Config.Hostname)), hostsContent...)
  567. }
  568. ioutil.WriteFile(container.HostsPath, hostsContent, 0644)
  569. }
  570. func (container *Container) allocateNetwork() error {
  571. if container.Config.NetworkDisabled {
  572. return nil
  573. }
  574. var (
  575. env *engine.Env
  576. err error
  577. eng = container.runtime.eng
  578. )
  579. if container.State.IsGhost() {
  580. if container.runtime.config.DisableNetwork {
  581. env = &engine.Env{}
  582. } else {
  583. currentIP := container.NetworkSettings.IPAddress
  584. job := eng.Job("allocate_interface", container.ID)
  585. if currentIP != "" {
  586. job.Setenv("RequestIP", currentIP)
  587. }
  588. env, err = job.Stdout.AddEnv()
  589. if err != nil {
  590. return err
  591. }
  592. if err := job.Run(); err != nil {
  593. return err
  594. }
  595. }
  596. } else {
  597. job := eng.Job("allocate_interface", container.ID)
  598. env, err = job.Stdout.AddEnv()
  599. if err != nil {
  600. return err
  601. }
  602. if err := job.Run(); err != nil {
  603. return err
  604. }
  605. }
  606. if container.Config.PortSpecs != nil {
  607. utils.Debugf("Migrating port mappings for container: %s", strings.Join(container.Config.PortSpecs, ", "))
  608. if err := migratePortMappings(container.Config, container.hostConfig); err != nil {
  609. return err
  610. }
  611. container.Config.PortSpecs = nil
  612. if err := container.writeHostConfig(); err != nil {
  613. return err
  614. }
  615. }
  616. var (
  617. portSpecs = make(nat.PortSet)
  618. bindings = make(nat.PortMap)
  619. )
  620. if !container.State.IsGhost() {
  621. if container.Config.ExposedPorts != nil {
  622. portSpecs = container.Config.ExposedPorts
  623. }
  624. if container.hostConfig.PortBindings != nil {
  625. bindings = container.hostConfig.PortBindings
  626. }
  627. } else {
  628. if container.NetworkSettings.Ports != nil {
  629. for port, binding := range container.NetworkSettings.Ports {
  630. portSpecs[port] = struct{}{}
  631. bindings[port] = binding
  632. }
  633. }
  634. }
  635. container.NetworkSettings.PortMapping = nil
  636. for port := range portSpecs {
  637. binding := bindings[port]
  638. if container.hostConfig.PublishAllPorts && len(binding) == 0 {
  639. binding = append(binding, nat.PortBinding{})
  640. }
  641. for i := 0; i < len(binding); i++ {
  642. b := binding[i]
  643. portJob := eng.Job("allocate_port", container.ID)
  644. portJob.Setenv("HostIP", b.HostIp)
  645. portJob.Setenv("HostPort", b.HostPort)
  646. portJob.Setenv("Proto", port.Proto())
  647. portJob.Setenv("ContainerPort", port.Port())
  648. portEnv, err := portJob.Stdout.AddEnv()
  649. if err != nil {
  650. return err
  651. }
  652. if err := portJob.Run(); err != nil {
  653. eng.Job("release_interface", container.ID).Run()
  654. return err
  655. }
  656. b.HostIp = portEnv.Get("HostIP")
  657. b.HostPort = portEnv.Get("HostPort")
  658. binding[i] = b
  659. }
  660. bindings[port] = binding
  661. }
  662. container.writeHostConfig()
  663. container.NetworkSettings.Ports = bindings
  664. container.NetworkSettings.Bridge = env.Get("Bridge")
  665. container.NetworkSettings.IPAddress = env.Get("IP")
  666. container.NetworkSettings.IPPrefixLen = env.GetInt("IPPrefixLen")
  667. container.NetworkSettings.Gateway = env.Get("Gateway")
  668. return nil
  669. }
  670. func (container *Container) releaseNetwork() {
  671. if container.Config.NetworkDisabled {
  672. return
  673. }
  674. eng := container.runtime.eng
  675. eng.Job("release_interface", container.ID).Run()
  676. container.NetworkSettings = &NetworkSettings{}
  677. }
  678. func (container *Container) monitor(callback execdriver.StartCallback) error {
  679. var (
  680. err error
  681. exitCode int
  682. )
  683. pipes := execdriver.NewPipes(container.stdin, container.stdout, container.stderr, container.Config.OpenStdin)
  684. exitCode, err = container.runtime.Run(container, pipes, callback)
  685. if err != nil {
  686. utils.Errorf("Error running container: %s", err)
  687. }
  688. container.State.SetStopped(exitCode)
  689. // FIXME: there is a race condition here which causes this to fail during the unit tests.
  690. // If another goroutine was waiting for Wait() to return before removing the container's root
  691. // from the filesystem... At this point it may already have done so.
  692. // This is because State.setStopped() has already been called, and has caused Wait()
  693. // to return.
  694. // FIXME: why are we serializing running state to disk in the first place?
  695. //log.Printf("%s: Failed to dump configuration to the disk: %s", container.ID, err)
  696. if err := container.ToDisk(); err != nil {
  697. utils.Errorf("Error dumping container state to disk: %s\n", err)
  698. }
  699. // Cleanup
  700. container.cleanup()
  701. // Re-create a brand new stdin pipe once the container exited
  702. if container.Config.OpenStdin {
  703. container.stdin, container.stdinPipe = io.Pipe()
  704. }
  705. if container.runtime != nil && container.runtime.srv != nil {
  706. container.runtime.srv.LogEvent("die", container.ID, container.runtime.repositories.ImageName(container.Image))
  707. }
  708. close(container.waitLock)
  709. return err
  710. }
  711. func (container *Container) cleanup() {
  712. container.releaseNetwork()
  713. // Disable all active links
  714. if container.activeLinks != nil {
  715. for _, link := range container.activeLinks {
  716. link.Disable()
  717. }
  718. }
  719. if container.Config.OpenStdin {
  720. if err := container.stdin.Close(); err != nil {
  721. utils.Errorf("%s: Error close stdin: %s", container.ID, err)
  722. }
  723. }
  724. if err := container.stdout.CloseWriters(); err != nil {
  725. utils.Errorf("%s: Error close stdout: %s", container.ID, err)
  726. }
  727. if err := container.stderr.CloseWriters(); err != nil {
  728. utils.Errorf("%s: Error close stderr: %s", container.ID, err)
  729. }
  730. if container.command != nil && container.command.Terminal != nil {
  731. if err := container.command.Terminal.Close(); err != nil {
  732. utils.Errorf("%s: Error closing terminal: %s", container.ID, err)
  733. }
  734. }
  735. unmountVolumesForContainer(container)
  736. if err := container.Unmount(); err != nil {
  737. log.Printf("%v: Failed to umount filesystem: %v", container.ID, err)
  738. }
  739. }
  740. func (container *Container) kill(sig int) error {
  741. container.Lock()
  742. defer container.Unlock()
  743. if !container.State.IsRunning() {
  744. return nil
  745. }
  746. return container.runtime.Kill(container, sig)
  747. }
  748. func (container *Container) Kill() error {
  749. if !container.State.IsRunning() {
  750. return nil
  751. }
  752. // 1. Send SIGKILL
  753. if err := container.kill(9); err != nil {
  754. return err
  755. }
  756. // 2. Wait for the process to die, in last resort, try to kill the process directly
  757. if err := container.WaitTimeout(10 * time.Second); err != nil {
  758. if container.command == nil {
  759. return fmt.Errorf("lxc-kill failed, impossible to kill the container %s", utils.TruncateID(container.ID))
  760. }
  761. log.Printf("Container %s failed to exit within 10 seconds of lxc-kill %s - trying direct SIGKILL", "SIGKILL", utils.TruncateID(container.ID))
  762. if err := container.runtime.Kill(container, 9); err != nil {
  763. return err
  764. }
  765. }
  766. container.Wait()
  767. return nil
  768. }
  769. func (container *Container) Stop(seconds int) error {
  770. if !container.State.IsRunning() {
  771. return nil
  772. }
  773. // 1. Send a SIGTERM
  774. if err := container.kill(15); err != nil {
  775. utils.Debugf("Error sending kill SIGTERM: %s", err)
  776. log.Print("Failed to send SIGTERM to the process, force killing")
  777. if err := container.kill(9); err != nil {
  778. return err
  779. }
  780. }
  781. // 2. Wait for the process to exit on its own
  782. if err := container.WaitTimeout(time.Duration(seconds) * time.Second); err != nil {
  783. log.Printf("Container %v failed to exit within %d seconds of SIGTERM - using the force", container.ID, seconds)
  784. // 3. If it doesn't, then send SIGKILL
  785. if err := container.Kill(); err != nil {
  786. return err
  787. }
  788. }
  789. return nil
  790. }
  791. func (container *Container) Restart(seconds int) error {
  792. // Avoid unnecessarily unmounting and then directly mounting
  793. // the container when the container stops and then starts
  794. // again
  795. if err := container.Mount(); err == nil {
  796. defer container.Unmount()
  797. }
  798. if err := container.Stop(seconds); err != nil {
  799. return err
  800. }
  801. return container.Start()
  802. }
  803. // Wait blocks until the container stops running, then returns its exit code.
  804. func (container *Container) Wait() int {
  805. <-container.waitLock
  806. return container.State.GetExitCode()
  807. }
  808. func (container *Container) Resize(h, w int) error {
  809. return container.command.Terminal.Resize(h, w)
  810. }
  811. func (container *Container) ExportRw() (archive.Archive, error) {
  812. if err := container.Mount(); err != nil {
  813. return nil, err
  814. }
  815. if container.runtime == nil {
  816. return nil, fmt.Errorf("Can't load storage driver for unregistered container %s", container.ID)
  817. }
  818. archive, err := container.runtime.Diff(container)
  819. if err != nil {
  820. container.Unmount()
  821. return nil, err
  822. }
  823. return utils.NewReadCloserWrapper(archive, func() error {
  824. err := archive.Close()
  825. container.Unmount()
  826. return err
  827. }), nil
  828. }
  829. func (container *Container) Export() (archive.Archive, error) {
  830. if err := container.Mount(); err != nil {
  831. return nil, err
  832. }
  833. archive, err := archive.Tar(container.basefs, archive.Uncompressed)
  834. if err != nil {
  835. container.Unmount()
  836. return nil, err
  837. }
  838. return utils.NewReadCloserWrapper(archive, func() error {
  839. err := archive.Close()
  840. container.Unmount()
  841. return err
  842. }), nil
  843. }
  844. func (container *Container) WaitTimeout(timeout time.Duration) error {
  845. done := make(chan bool)
  846. go func() {
  847. container.Wait()
  848. done <- true
  849. }()
  850. select {
  851. case <-time.After(timeout):
  852. return fmt.Errorf("Timed Out")
  853. case <-done:
  854. return nil
  855. }
  856. }
  857. func (container *Container) Mount() error {
  858. return container.runtime.Mount(container)
  859. }
  860. func (container *Container) Changes() ([]archive.Change, error) {
  861. return container.runtime.Changes(container)
  862. }
  863. func (container *Container) GetImage() (*Image, error) {
  864. if container.runtime == nil {
  865. return nil, fmt.Errorf("Can't get image of unregistered container")
  866. }
  867. return container.runtime.graph.Get(container.Image)
  868. }
  869. func (container *Container) Unmount() error {
  870. return container.runtime.Unmount(container)
  871. }
  872. func (container *Container) logPath(name string) string {
  873. return path.Join(container.root, fmt.Sprintf("%s-%s.log", container.ID, name))
  874. }
  875. func (container *Container) ReadLog(name string) (io.Reader, error) {
  876. return os.Open(container.logPath(name))
  877. }
  878. func (container *Container) hostConfigPath() string {
  879. return path.Join(container.root, "hostconfig.json")
  880. }
  881. func (container *Container) jsonPath() string {
  882. return path.Join(container.root, "config.json")
  883. }
  884. func (container *Container) EnvConfigPath() (string, error) {
  885. p := path.Join(container.root, "config.env")
  886. if _, err := os.Stat(p); err != nil {
  887. if os.IsNotExist(err) {
  888. f, err := os.Create(p)
  889. if err != nil {
  890. return "", err
  891. }
  892. f.Close()
  893. } else {
  894. return "", err
  895. }
  896. }
  897. return p, nil
  898. }
  899. // This method must be exported to be used from the lxc template
  900. // This directory is only usable when the container is running
  901. func (container *Container) RootfsPath() string {
  902. return path.Join(container.root, "root")
  903. }
  904. // This is the stand-alone version of the root fs, without any additional mounts.
  905. // This directory is usable whenever the container is mounted (and not unmounted)
  906. func (container *Container) BasefsPath() string {
  907. return container.basefs
  908. }
  909. func validateID(id string) error {
  910. if id == "" {
  911. return fmt.Errorf("Invalid empty id")
  912. }
  913. return nil
  914. }
  915. // GetSize, return real size, virtual size
  916. func (container *Container) GetSize() (int64, int64) {
  917. var (
  918. sizeRw, sizeRootfs int64
  919. err error
  920. driver = container.runtime.driver
  921. )
  922. if err := container.Mount(); err != nil {
  923. utils.Errorf("Warning: failed to compute size of container rootfs %s: %s", container.ID, err)
  924. return sizeRw, sizeRootfs
  925. }
  926. defer container.Unmount()
  927. if differ, ok := container.runtime.driver.(graphdriver.Differ); ok {
  928. sizeRw, err = differ.DiffSize(container.ID)
  929. if err != nil {
  930. utils.Errorf("Warning: driver %s couldn't return diff size of container %s: %s", driver, container.ID, err)
  931. // FIXME: GetSize should return an error. Not changing it now in case
  932. // there is a side-effect.
  933. sizeRw = -1
  934. }
  935. } else {
  936. changes, _ := container.Changes()
  937. if changes != nil {
  938. sizeRw = archive.ChangesSize(container.basefs, changes)
  939. } else {
  940. sizeRw = -1
  941. }
  942. }
  943. if _, err = os.Stat(container.basefs); err != nil {
  944. if sizeRootfs, err = utils.TreeSize(container.basefs); err != nil {
  945. sizeRootfs = -1
  946. }
  947. }
  948. return sizeRw, sizeRootfs
  949. }
  950. func (container *Container) Copy(resource string) (io.ReadCloser, error) {
  951. if err := container.Mount(); err != nil {
  952. return nil, err
  953. }
  954. var filter []string
  955. basePath := path.Join(container.basefs, resource)
  956. stat, err := os.Stat(basePath)
  957. if err != nil {
  958. container.Unmount()
  959. return nil, err
  960. }
  961. if !stat.IsDir() {
  962. d, f := path.Split(basePath)
  963. basePath = d
  964. filter = []string{f}
  965. } else {
  966. filter = []string{path.Base(basePath)}
  967. basePath = path.Dir(basePath)
  968. }
  969. archive, err := archive.TarFilter(basePath, &archive.TarOptions{
  970. Compression: archive.Uncompressed,
  971. Includes: filter,
  972. })
  973. if err != nil {
  974. return nil, err
  975. }
  976. return utils.NewReadCloserWrapper(archive, func() error {
  977. err := archive.Close()
  978. container.Unmount()
  979. return err
  980. }), nil
  981. }
  982. // Returns true if the container exposes a certain port
  983. func (container *Container) Exposes(p nat.Port) bool {
  984. _, exists := container.Config.ExposedPorts[p]
  985. return exists
  986. }
  987. func (container *Container) GetPtyMaster() (*os.File, error) {
  988. ttyConsole, ok := container.command.Terminal.(execdriver.TtyTerminal)
  989. if !ok {
  990. return nil, ErrNoTTY
  991. }
  992. return ttyConsole.Master(), nil
  993. }