container.go 18 KB

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