container.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. package docker
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/dotcloud/docker/rcli"
  6. "github.com/kr/pty"
  7. "io"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "os/exec"
  12. "path"
  13. "strconv"
  14. "syscall"
  15. "time"
  16. )
  17. type Container struct {
  18. root string
  19. Id string
  20. Created time.Time
  21. Path string
  22. Args []string
  23. Config *Config
  24. State State
  25. Image string
  26. network *NetworkInterface
  27. NetworkSettings *NetworkSettings
  28. SysInitPath string
  29. cmd *exec.Cmd
  30. stdout *writeBroadcaster
  31. stderr *writeBroadcaster
  32. stdin io.ReadCloser
  33. stdinPipe io.WriteCloser
  34. ptyMaster io.Closer
  35. runtime *Runtime
  36. waitLock chan struct{}
  37. }
  38. type Config struct {
  39. Hostname string
  40. User string
  41. Memory int64 // Memory limit (in bytes)
  42. MemorySwap int64 // Total memory usage (memory + swap); set `-1' to disable swap
  43. AttachStdin bool
  44. AttachStdout bool
  45. AttachStderr bool
  46. PortSpecs []string
  47. Tty bool // Attach standard streams to a tty, including stdin if it is not closed.
  48. OpenStdin bool // Open stdin
  49. StdinOnce bool // If true, close stdin after the 1 attached client disconnects.
  50. Env []string
  51. Cmd []string
  52. Image string // Name of the image as it was passed by the operator (eg. could be symbolic)
  53. }
  54. func ParseRun(args []string, stdout io.Writer) (*Config, error) {
  55. cmd := rcli.Subcmd(stdout, "run", "[OPTIONS] IMAGE COMMAND [ARG...]", "Run a command in a new container")
  56. if len(args) > 0 && args[0] != "--help" {
  57. cmd.SetOutput(ioutil.Discard)
  58. }
  59. flHostname := cmd.String("h", "", "Container host name")
  60. flUser := cmd.String("u", "", "Username or UID")
  61. flDetach := cmd.Bool("d", false, "Detached mode: leave the container running in the background")
  62. flAttach := NewAttachOpts()
  63. cmd.Var(flAttach, "a", "Attach to stdin, stdout or stderr.")
  64. flStdin := cmd.Bool("i", false, "Keep stdin open even if not attached")
  65. flTty := cmd.Bool("t", false, "Allocate a pseudo-tty")
  66. flMemory := cmd.Int64("m", 0, "Memory limit (in bytes)")
  67. var flPorts ListOpts
  68. cmd.Var(&flPorts, "p", "Expose a container's port to the host (use 'docker port' to see the actual mapping)")
  69. var flEnv ListOpts
  70. cmd.Var(&flEnv, "e", "Set environment variables")
  71. if err := cmd.Parse(args); err != nil {
  72. return nil, err
  73. }
  74. if *flDetach && len(flAttach) > 0 {
  75. return nil, fmt.Errorf("Conflicting options: -a and -d")
  76. }
  77. // If neither -d or -a are set, attach to everything by default
  78. if len(flAttach) == 0 && !*flDetach {
  79. if !*flDetach {
  80. flAttach.Set("stdout")
  81. flAttach.Set("stderr")
  82. if *flStdin {
  83. flAttach.Set("stdin")
  84. }
  85. }
  86. }
  87. parsedArgs := cmd.Args()
  88. runCmd := []string{}
  89. image := ""
  90. if len(parsedArgs) >= 1 {
  91. image = cmd.Arg(0)
  92. }
  93. if len(parsedArgs) > 1 {
  94. runCmd = parsedArgs[1:]
  95. }
  96. config := &Config{
  97. Hostname: *flHostname,
  98. PortSpecs: flPorts,
  99. User: *flUser,
  100. Tty: *flTty,
  101. OpenStdin: *flStdin,
  102. Memory: *flMemory,
  103. AttachStdin: flAttach.Get("stdin"),
  104. AttachStdout: flAttach.Get("stdout"),
  105. AttachStderr: flAttach.Get("stderr"),
  106. Env: flEnv,
  107. Cmd: runCmd,
  108. Image: image,
  109. }
  110. // When allocating stdin in attached mode, close stdin at client disconnect
  111. if config.OpenStdin && config.AttachStdin {
  112. config.StdinOnce = true
  113. }
  114. return config, nil
  115. }
  116. type NetworkSettings struct {
  117. IpAddress string
  118. IpPrefixLen int
  119. Gateway string
  120. Bridge string
  121. PortMapping map[string]string
  122. }
  123. func (container *Container) Cmd() *exec.Cmd {
  124. return container.cmd
  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. if err := json.Unmarshal(data, container); err != nil {
  136. return err
  137. }
  138. return nil
  139. }
  140. func (container *Container) ToDisk() (err error) {
  141. data, err := json.Marshal(container)
  142. if err != nil {
  143. return
  144. }
  145. return ioutil.WriteFile(container.jsonPath(), data, 0666)
  146. }
  147. func (container *Container) generateLXCConfig() error {
  148. fo, err := os.Create(container.lxcConfigPath())
  149. if err != nil {
  150. return err
  151. }
  152. defer fo.Close()
  153. if err := LxcTemplateCompiled.Execute(fo, container); err != nil {
  154. return err
  155. }
  156. return nil
  157. }
  158. func (container *Container) startPty() error {
  159. ptyMaster, ptySlave, err := pty.Open()
  160. if err != nil {
  161. return err
  162. }
  163. container.ptyMaster = ptyMaster
  164. container.cmd.Stdout = ptySlave
  165. container.cmd.Stderr = ptySlave
  166. // Copy the PTYs to our broadcasters
  167. go func() {
  168. defer container.stdout.CloseWriters()
  169. Debugf("[startPty] Begin of stdout pipe")
  170. io.Copy(container.stdout, ptyMaster)
  171. Debugf("[startPty] End of stdout pipe")
  172. }()
  173. // stdin
  174. if container.Config.OpenStdin {
  175. container.cmd.Stdin = ptySlave
  176. container.cmd.SysProcAttr = &syscall.SysProcAttr{Setctty: true, Setsid: true}
  177. go func() {
  178. defer container.stdin.Close()
  179. Debugf("[startPty] Begin of stdin pipe")
  180. io.Copy(ptyMaster, container.stdin)
  181. Debugf("[startPty] End of stdin pipe")
  182. }()
  183. }
  184. if err := container.cmd.Start(); err != nil {
  185. return err
  186. }
  187. ptySlave.Close()
  188. return nil
  189. }
  190. func (container *Container) start() error {
  191. container.cmd.Stdout = container.stdout
  192. container.cmd.Stderr = container.stderr
  193. if container.Config.OpenStdin {
  194. stdin, err := container.cmd.StdinPipe()
  195. if err != nil {
  196. return err
  197. }
  198. go func() {
  199. defer stdin.Close()
  200. Debugf("Begin of stdin pipe [start]")
  201. io.Copy(stdin, container.stdin)
  202. Debugf("End of stdin pipe [start]")
  203. }()
  204. }
  205. return container.cmd.Start()
  206. }
  207. func (container *Container) Attach(stdin io.ReadCloser, stdinCloser io.Closer, stdout io.Writer, stderr io.Writer) chan error {
  208. var cStdout, cStderr io.ReadCloser
  209. var nJobs int
  210. errors := make(chan error, 3)
  211. if stdin != nil && container.Config.OpenStdin {
  212. nJobs += 1
  213. if cStdin, err := container.StdinPipe(); err != nil {
  214. errors <- err
  215. } else {
  216. go func() {
  217. Debugf("[start] attach stdin\n")
  218. defer Debugf("[end] attach stdin\n")
  219. // No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr
  220. if cStdout != nil {
  221. defer cStdout.Close()
  222. }
  223. if cStderr != nil {
  224. defer cStderr.Close()
  225. }
  226. if container.Config.StdinOnce && !container.Config.Tty {
  227. defer cStdin.Close()
  228. }
  229. _, err := CopyEscapable(cStdin, stdin)
  230. if err != nil {
  231. Debugf("[error] attach stdin: %s\n", err)
  232. }
  233. // Discard error, expecting pipe error
  234. errors <- nil
  235. }()
  236. }
  237. }
  238. if stdout != nil {
  239. nJobs += 1
  240. if p, err := container.StdoutPipe(); err != nil {
  241. errors <- err
  242. } else {
  243. cStdout = p
  244. go func() {
  245. Debugf("[start] attach stdout\n")
  246. defer Debugf("[end] attach stdout\n")
  247. // If we are in StdinOnce mode, then close stdin
  248. if container.Config.StdinOnce {
  249. if stdin != nil {
  250. defer stdin.Close()
  251. }
  252. if stdinCloser != nil {
  253. defer stdinCloser.Close()
  254. }
  255. }
  256. _, err := io.Copy(stdout, cStdout)
  257. if err != nil {
  258. Debugf("[error] attach stdout: %s\n", err)
  259. }
  260. errors <- err
  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. Debugf("[start] attach stderr\n")
  272. defer Debugf("[end] attach stderr\n")
  273. // If we are in StdinOnce mode, then close stdin
  274. if container.Config.StdinOnce {
  275. if stdin != nil {
  276. defer stdin.Close()
  277. }
  278. if stdinCloser != nil {
  279. defer stdinCloser.Close()
  280. }
  281. }
  282. _, err := io.Copy(stderr, cStderr)
  283. if err != nil {
  284. Debugf("[error] attach stderr: %s\n", err)
  285. }
  286. errors <- err
  287. }()
  288. }
  289. }
  290. return Go(func() error {
  291. if cStdout != nil {
  292. defer cStdout.Close()
  293. }
  294. if cStderr != nil {
  295. defer cStderr.Close()
  296. }
  297. // FIXME: how do clean up the stdin goroutine without the unwanted side effect
  298. // of closing the passed stdin? Add an intermediary io.Pipe?
  299. for i := 0; i < nJobs; i += 1 {
  300. Debugf("Waiting for job %d/%d\n", i+1, nJobs)
  301. if err := <-errors; err != nil {
  302. Debugf("Job %d returned error %s. Aborting all jobs\n", i+1, err)
  303. return err
  304. }
  305. Debugf("Job %d completed successfully\n", i+1)
  306. }
  307. Debugf("All jobs completed successfully\n")
  308. return nil
  309. })
  310. }
  311. func (container *Container) Start() error {
  312. container.State.lock()
  313. defer container.State.unlock()
  314. if container.State.Running {
  315. return fmt.Errorf("The container %s is already running.", container.Id)
  316. }
  317. if err := container.EnsureMounted(); err != nil {
  318. return err
  319. }
  320. if err := container.allocateNetwork(); err != nil {
  321. return err
  322. }
  323. if err := container.generateLXCConfig(); err != nil {
  324. return err
  325. }
  326. params := []string{
  327. "-n", container.Id,
  328. "-f", container.lxcConfigPath(),
  329. "--",
  330. "/sbin/init",
  331. }
  332. // Networking
  333. params = append(params, "-g", container.network.Gateway.String())
  334. // User
  335. if container.Config.User != "" {
  336. params = append(params, "-u", container.Config.User)
  337. }
  338. // Program
  339. params = append(params, "--", container.Path)
  340. params = append(params, container.Args...)
  341. container.cmd = exec.Command("lxc-start", params...)
  342. // Setup environment
  343. container.cmd.Env = append(
  344. []string{
  345. "HOME=/",
  346. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  347. },
  348. container.Config.Env...,
  349. )
  350. // Setup logging of stdout and stderr to disk
  351. if err := container.runtime.LogToDisk(container.stdout, container.logPath("stdout")); err != nil {
  352. return err
  353. }
  354. if err := container.runtime.LogToDisk(container.stderr, container.logPath("stderr")); err != nil {
  355. return err
  356. }
  357. var err error
  358. if container.Config.Tty {
  359. container.cmd.Env = append(
  360. []string{"TERM=xterm"},
  361. container.cmd.Env...,
  362. )
  363. err = container.startPty()
  364. } else {
  365. err = container.start()
  366. }
  367. if err != nil {
  368. return err
  369. }
  370. // FIXME: save state on disk *first*, then converge
  371. // this way disk state is used as a journal, eg. we can restore after crash etc.
  372. container.State.setRunning(container.cmd.Process.Pid)
  373. // Init the lock
  374. container.waitLock = make(chan struct{})
  375. container.ToDisk()
  376. go container.monitor()
  377. return nil
  378. }
  379. func (container *Container) Run() error {
  380. if err := container.Start(); err != nil {
  381. return err
  382. }
  383. container.Wait()
  384. return nil
  385. }
  386. func (container *Container) Output() (output []byte, err error) {
  387. pipe, err := container.StdoutPipe()
  388. if err != nil {
  389. return nil, err
  390. }
  391. defer pipe.Close()
  392. if err := container.Start(); err != nil {
  393. return nil, err
  394. }
  395. output, err = ioutil.ReadAll(pipe)
  396. container.Wait()
  397. return output, err
  398. }
  399. // StdinPipe() returns a pipe connected to the standard input of the container's
  400. // active process.
  401. //
  402. func (container *Container) StdinPipe() (io.WriteCloser, error) {
  403. return container.stdinPipe, nil
  404. }
  405. func (container *Container) StdoutPipe() (io.ReadCloser, error) {
  406. reader, writer := io.Pipe()
  407. container.stdout.AddWriter(writer)
  408. return newBufReader(reader), nil
  409. }
  410. func (container *Container) StderrPipe() (io.ReadCloser, error) {
  411. reader, writer := io.Pipe()
  412. container.stderr.AddWriter(writer)
  413. return newBufReader(reader), nil
  414. }
  415. func (container *Container) allocateNetwork() error {
  416. iface, err := container.runtime.networkManager.Allocate()
  417. if err != nil {
  418. return err
  419. }
  420. container.NetworkSettings.PortMapping = make(map[string]string)
  421. for _, spec := range container.Config.PortSpecs {
  422. if nat, err := iface.AllocatePort(spec); err != nil {
  423. iface.Release()
  424. return err
  425. } else {
  426. container.NetworkSettings.PortMapping[strconv.Itoa(nat.Backend)] = strconv.Itoa(nat.Frontend)
  427. }
  428. }
  429. container.network = iface
  430. container.NetworkSettings.Bridge = container.runtime.networkManager.bridgeIface
  431. container.NetworkSettings.IpAddress = iface.IPNet.IP.String()
  432. container.NetworkSettings.IpPrefixLen, _ = iface.IPNet.Mask.Size()
  433. container.NetworkSettings.Gateway = iface.Gateway.String()
  434. return nil
  435. }
  436. func (container *Container) releaseNetwork() {
  437. container.network.Release()
  438. container.network = nil
  439. container.NetworkSettings = &NetworkSettings{}
  440. }
  441. func (container *Container) monitor() {
  442. // Wait for the program to exit
  443. Debugf("Waiting for process")
  444. if err := container.cmd.Wait(); err != nil {
  445. // Discard the error as any signals or non 0 returns will generate an error
  446. Debugf("%s: Process: %s", container.Id, err)
  447. }
  448. Debugf("Process finished")
  449. exitCode := container.cmd.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
  450. // Cleanup
  451. container.releaseNetwork()
  452. if container.Config.OpenStdin {
  453. if err := container.stdin.Close(); err != nil {
  454. Debugf("%s: Error close stdin: %s", container.Id, err)
  455. }
  456. }
  457. if err := container.stdout.CloseWriters(); err != nil {
  458. Debugf("%s: Error close stdout: %s", container.Id, err)
  459. }
  460. if err := container.stderr.CloseWriters(); err != nil {
  461. Debugf("%s: Error close stderr: %s", container.Id, err)
  462. }
  463. if container.ptyMaster != nil {
  464. if err := container.ptyMaster.Close(); err != nil {
  465. Debugf("%s: Error closing Pty master: %s", container.Id, err)
  466. }
  467. }
  468. if err := container.Unmount(); err != nil {
  469. log.Printf("%v: Failed to umount filesystem: %v", container.Id, err)
  470. }
  471. // Re-create a brand new stdin pipe once the container exited
  472. if container.Config.OpenStdin {
  473. container.stdin, container.stdinPipe = io.Pipe()
  474. }
  475. // Report status back
  476. container.State.setStopped(exitCode)
  477. // Release the lock
  478. close(container.waitLock)
  479. if err := container.ToDisk(); err != nil {
  480. // FIXME: there is a race condition here which causes this to fail during the unit tests.
  481. // If another goroutine was waiting for Wait() to return before removing the container's root
  482. // from the filesystem... At this point it may already have done so.
  483. // This is because State.setStopped() has already been called, and has caused Wait()
  484. // to return.
  485. // FIXME: why are we serializing running state to disk in the first place?
  486. //log.Printf("%s: Failed to dump configuration to the disk: %s", container.Id, err)
  487. }
  488. }
  489. func (container *Container) kill() error {
  490. if !container.State.Running || container.cmd == nil {
  491. return nil
  492. }
  493. if err := container.cmd.Process.Kill(); err != nil {
  494. return err
  495. }
  496. // Wait for the container to be actually stopped
  497. container.Wait()
  498. return nil
  499. }
  500. func (container *Container) Kill() error {
  501. container.State.lock()
  502. defer container.State.unlock()
  503. return container.kill()
  504. }
  505. func (container *Container) Stop() error {
  506. container.State.lock()
  507. defer container.State.unlock()
  508. if !container.State.Running {
  509. return nil
  510. }
  511. // 1. Send a SIGTERM
  512. if output, err := exec.Command("lxc-kill", "-n", container.Id, "15").CombinedOutput(); err != nil {
  513. log.Print(string(output))
  514. log.Print("Failed to send SIGTERM to the process, force killing")
  515. if err := container.kill(); err != nil {
  516. return err
  517. }
  518. }
  519. // 2. Wait for the process to exit on its own
  520. if err := container.WaitTimeout(10 * time.Second); err != nil {
  521. log.Printf("Container %v failed to exit within 10 seconds of SIGTERM - using the force", container.Id)
  522. if err := container.Kill(); err != nil {
  523. return err
  524. }
  525. }
  526. return nil
  527. }
  528. func (container *Container) Restart() error {
  529. if err := container.Stop(); err != nil {
  530. return err
  531. }
  532. if err := container.Start(); err != nil {
  533. return err
  534. }
  535. return nil
  536. }
  537. // Wait blocks until the container stops running, then returns its exit code.
  538. func (container *Container) Wait() int {
  539. <-container.waitLock
  540. return container.State.ExitCode
  541. }
  542. func (container *Container) ExportRw() (Archive, error) {
  543. return Tar(container.rwPath(), Uncompressed)
  544. }
  545. func (container *Container) Export() (Archive, error) {
  546. if err := container.EnsureMounted(); err != nil {
  547. return nil, err
  548. }
  549. return Tar(container.RootfsPath(), Uncompressed)
  550. }
  551. func (container *Container) WaitTimeout(timeout time.Duration) error {
  552. done := make(chan bool)
  553. go func() {
  554. container.Wait()
  555. done <- true
  556. }()
  557. select {
  558. case <-time.After(timeout):
  559. return fmt.Errorf("Timed Out")
  560. case <-done:
  561. return nil
  562. }
  563. panic("unreachable")
  564. }
  565. func (container *Container) EnsureMounted() error {
  566. if mounted, err := container.Mounted(); err != nil {
  567. return err
  568. } else if mounted {
  569. return nil
  570. }
  571. return container.Mount()
  572. }
  573. func (container *Container) Mount() error {
  574. image, err := container.GetImage()
  575. if err != nil {
  576. return err
  577. }
  578. return image.Mount(container.RootfsPath(), container.rwPath())
  579. }
  580. func (container *Container) Changes() ([]Change, error) {
  581. image, err := container.GetImage()
  582. if err != nil {
  583. return nil, err
  584. }
  585. return image.Changes(container.rwPath())
  586. }
  587. func (container *Container) GetImage() (*Image, error) {
  588. if container.runtime == nil {
  589. return nil, fmt.Errorf("Can't get image of unregistered container")
  590. }
  591. return container.runtime.graph.Get(container.Image)
  592. }
  593. func (container *Container) Mounted() (bool, error) {
  594. return Mounted(container.RootfsPath())
  595. }
  596. func (container *Container) Unmount() error {
  597. return Unmount(container.RootfsPath())
  598. }
  599. // ShortId returns a shorthand version of the container's id for convenience.
  600. // A collision with other container shorthands is very unlikely, but possible.
  601. // In case of a collision a lookup with Runtime.Get() will fail, and the caller
  602. // will need to use a langer prefix, or the full-length container Id.
  603. func (container *Container) ShortId() string {
  604. return TruncateId(container.Id)
  605. }
  606. func (container *Container) logPath(name string) string {
  607. return path.Join(container.root, fmt.Sprintf("%s-%s.log", container.Id, name))
  608. }
  609. func (container *Container) ReadLog(name string) (io.Reader, error) {
  610. return os.Open(container.logPath(name))
  611. }
  612. func (container *Container) jsonPath() string {
  613. return path.Join(container.root, "config.json")
  614. }
  615. func (container *Container) lxcConfigPath() string {
  616. return path.Join(container.root, "config.lxc")
  617. }
  618. // This method must be exported to be used from the lxc template
  619. func (container *Container) RootfsPath() string {
  620. return path.Join(container.root, "rootfs")
  621. }
  622. func (container *Container) rwPath() string {
  623. return path.Join(container.root, "rw")
  624. }
  625. func validateId(id string) error {
  626. if id == "" {
  627. return fmt.Errorf("Invalid empty id")
  628. }
  629. return nil
  630. }