container.go 18 KB

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