commands.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. package docker
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "net"
  10. "net/http"
  11. "net/http/httputil"
  12. "net/url"
  13. "os"
  14. "strconv"
  15. "text/tabwriter"
  16. )
  17. const VERSION = "0.1.4"
  18. var (
  19. GIT_COMMIT string
  20. NO_MEMORY_LIMIT bool
  21. )
  22. func ParseCommands(args []string) error {
  23. cmds := map[string]func(args []string) error{
  24. "attach": CmdAttach,
  25. "commit": CmdCommit,
  26. "diff": CmdDiff,
  27. "export": CmdExport,
  28. "images": CmdImages,
  29. "info": CmdInfo,
  30. "inspect": CmdInspect,
  31. "history": CmdHistory,
  32. "kill": CmdKill,
  33. "logs": CmdLogs,
  34. "port": CmdPort,
  35. "ps": CmdPs,
  36. "pull": CmdPull,
  37. "restart": CmdRestart,
  38. "rm": CmdRm,
  39. "rmi": CmdRmi,
  40. "run": CmdRun,
  41. "tag": CmdTag,
  42. "start": CmdStart,
  43. "stop": CmdStop,
  44. "version": CmdVersion,
  45. "wait": CmdWait,
  46. }
  47. if len(args) > 0 {
  48. cmd, exists := cmds[args[0]]
  49. if !exists {
  50. fmt.Println("Error: Command not found:", args[0])
  51. return cmdHelp(args)
  52. }
  53. return cmd(args[1:])
  54. }
  55. return cmdHelp(args)
  56. }
  57. func cmdHelp(args []string) error {
  58. help := "Usage: docker COMMAND [arg...]\n\nA self-sufficient runtime for linux containers.\n\nCommands:\n"
  59. for _, cmd := range [][]string{
  60. {"attach", "Attach to a running container"},
  61. {"commit", "Create a new image from a container's changes"},
  62. {"diff", "Inspect changes on a container's filesystem"},
  63. {"export", "Stream the contents of a container as a tar archive"},
  64. {"history", "Show the history of an image"},
  65. {"images", "List images"},
  66. // {"import", "Create a new filesystem image from the contents of a tarball"},
  67. {"info", "Display system-wide information"},
  68. {"inspect", "Return low-level information on a container/image"},
  69. {"kill", "Kill a running container"},
  70. // {"login", "Register or Login to the docker registry server"},
  71. {"logs", "Fetch the logs of a container"},
  72. {"port", "Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"},
  73. {"ps", "List containers"},
  74. {"pull", "Pull an image or a repository from the docker registry server"},
  75. // {"push", "Push an image or a repository to the docker registry server"},
  76. {"restart", "Restart a running container"},
  77. {"rm", "Remove a container"},
  78. {"rmi", "Remove an image"},
  79. {"run", "Run a command in a new container"},
  80. {"start", "Start a stopped container"},
  81. {"stop", "Stop a running container"},
  82. {"tag", "Tag an image into a repository"},
  83. {"version", "Show the docker version information"},
  84. {"wait", "Block until a container stops, then print its exit code"},
  85. } {
  86. help += fmt.Sprintf(" %-10.10s%s\n", cmd[0], cmd[1])
  87. }
  88. fmt.Println(help)
  89. return nil
  90. }
  91. /*
  92. // 'docker login': login / register a user to registry service.
  93. func (srv *Server) CmdLogin(stdin io.ReadCloser, stdout rcli.DockerConn, args ...string) error {
  94. // Read a line on raw terminal with support for simple backspace
  95. // sequences and echo.
  96. //
  97. // This function is necessary because the login command must be done in a
  98. // raw terminal for two reasons:
  99. // - we have to read a password (without echoing it);
  100. // - the rcli "protocol" only supports cannonical and raw modes and you
  101. // can't tune it once the command as been started.
  102. var readStringOnRawTerminal = func(stdin io.Reader, stdout io.Writer, echo bool) string {
  103. char := make([]byte, 1)
  104. buffer := make([]byte, 64)
  105. var i = 0
  106. for i < len(buffer) {
  107. n, err := stdin.Read(char)
  108. if n > 0 {
  109. if char[0] == '\r' || char[0] == '\n' {
  110. stdout.Write([]byte{'\r', '\n'})
  111. break
  112. } else if char[0] == 127 || char[0] == '\b' {
  113. if i > 0 {
  114. if echo {
  115. stdout.Write([]byte{'\b', ' ', '\b'})
  116. }
  117. i--
  118. }
  119. } else if !unicode.IsSpace(rune(char[0])) &&
  120. !unicode.IsControl(rune(char[0])) {
  121. if echo {
  122. stdout.Write(char)
  123. }
  124. buffer[i] = char[0]
  125. i++
  126. }
  127. }
  128. if err != nil {
  129. if err != io.EOF {
  130. fmt.Fprintf(stdout, "Read error: %v\r\n", err)
  131. }
  132. break
  133. }
  134. }
  135. return string(buffer[:i])
  136. }
  137. var readAndEchoString = func(stdin io.Reader, stdout io.Writer) string {
  138. return readStringOnRawTerminal(stdin, stdout, true)
  139. }
  140. var readString = func(stdin io.Reader, stdout io.Writer) string {
  141. return readStringOnRawTerminal(stdin, stdout, false)
  142. }
  143. stdout.SetOptionRawTerminal()
  144. cmd := rcli.Subcmd(stdout, "login", "", "Register or Login to the docker registry server")
  145. if err := cmd.Parse(args); err != nil {
  146. return nil
  147. }
  148. var username string
  149. var password string
  150. var email string
  151. fmt.Fprint(stdout, "Username (", srv.runtime.authConfig.Username, "): ")
  152. username = readAndEchoString(stdin, stdout)
  153. if username == "" {
  154. username = srv.runtime.authConfig.Username
  155. }
  156. if username != srv.runtime.authConfig.Username {
  157. fmt.Fprint(stdout, "Password: ")
  158. password = readString(stdin, stdout)
  159. if password == "" {
  160. return fmt.Errorf("Error : Password Required")
  161. }
  162. fmt.Fprint(stdout, "Email (", srv.runtime.authConfig.Email, "): ")
  163. email = readAndEchoString(stdin, stdout)
  164. if email == "" {
  165. email = srv.runtime.authConfig.Email
  166. }
  167. } else {
  168. password = srv.runtime.authConfig.Password
  169. email = srv.runtime.authConfig.Email
  170. }
  171. newAuthConfig := auth.NewAuthConfig(username, password, email, srv.runtime.root)
  172. status, err := auth.Login(newAuthConfig)
  173. if err != nil {
  174. fmt.Fprintf(stdout, "Error: %s\r\n", err)
  175. } else {
  176. srv.runtime.authConfig = newAuthConfig
  177. }
  178. if status != "" {
  179. fmt.Fprint(stdout, status)
  180. }
  181. return nil
  182. }
  183. */
  184. // 'docker wait': block until a container stops
  185. func CmdWait(args []string) error {
  186. cmd := Subcmd("wait", "CONTAINER [CONTAINER...]", "Block until a container stops, then print its exit code.")
  187. if err := cmd.Parse(args); err != nil {
  188. return nil
  189. }
  190. if cmd.NArg() < 1 {
  191. cmd.Usage()
  192. return nil
  193. }
  194. for _, name := range cmd.Args() {
  195. body, err := call("POST", "/containers/"+name+"/wait")
  196. if err != nil {
  197. fmt.Printf("%s", err)
  198. } else {
  199. var out ApiWait
  200. err = json.Unmarshal(body, &out)
  201. if err != nil {
  202. return err
  203. }
  204. fmt.Println(out.StatusCode)
  205. }
  206. }
  207. return nil
  208. }
  209. // 'docker version': show version information
  210. func CmdVersion(args []string) error {
  211. cmd := Subcmd("version", "", "Show the docker version information.")
  212. if err := cmd.Parse(args); err != nil {
  213. return nil
  214. }
  215. if cmd.NArg() > 0 {
  216. cmd.Usage()
  217. return nil
  218. }
  219. body, err := call("GET", "/version")
  220. if err != nil {
  221. return err
  222. }
  223. var out ApiVersion
  224. err = json.Unmarshal(body, &out)
  225. if err != nil {
  226. return err
  227. }
  228. fmt.Println("Version:", out.Version)
  229. fmt.Println("Git Commit:", out.GitCommit)
  230. if out.MemoryLimitDisabled {
  231. fmt.Println("Memory limit disabled")
  232. }
  233. return nil
  234. }
  235. // 'docker info': display system-wide information.
  236. func CmdInfo(args []string) error {
  237. cmd := Subcmd("info", "", "Display system-wide information")
  238. if err := cmd.Parse(args); err != nil {
  239. return nil
  240. }
  241. if cmd.NArg() > 0 {
  242. cmd.Usage()
  243. return nil
  244. }
  245. body, err := call("GET", "/info")
  246. if err != nil {
  247. return err
  248. }
  249. var out ApiInfo
  250. err = json.Unmarshal(body, &out)
  251. if err != nil {
  252. return err
  253. }
  254. fmt.Printf("containers: %d\nversion: %s\nimages: %d\n", out.Containers, out.Version, out.Images)
  255. if out.Debug {
  256. fmt.Println("debug mode enabled")
  257. fmt.Printf("fds: %d\ngoroutines: %d\n", out.NFd, out.NGoroutines)
  258. }
  259. return nil
  260. }
  261. func CmdStop(args []string) error {
  262. cmd := Subcmd("stop", "CONTAINER [CONTAINER...]", "Stop a running container")
  263. if err := cmd.Parse(args); err != nil {
  264. return nil
  265. }
  266. if cmd.NArg() < 1 {
  267. cmd.Usage()
  268. return nil
  269. }
  270. for _, name := range args {
  271. _, err := call("POST", "/containers/"+name+"/stop")
  272. if err != nil {
  273. fmt.Printf("%s", err)
  274. } else {
  275. fmt.Println(name)
  276. }
  277. }
  278. return nil
  279. }
  280. func CmdRestart(args []string) error {
  281. cmd := Subcmd("restart", "CONTAINER [CONTAINER...]", "Restart a running container")
  282. if err := cmd.Parse(args); err != nil {
  283. return nil
  284. }
  285. if cmd.NArg() < 1 {
  286. cmd.Usage()
  287. return nil
  288. }
  289. for _, name := range args {
  290. _, err := call("POST", "/containers/"+name+"/restart")
  291. if err != nil {
  292. fmt.Printf("%s", err)
  293. } else {
  294. fmt.Println(name)
  295. }
  296. }
  297. return nil
  298. }
  299. func CmdStart(args []string) error {
  300. cmd := Subcmd("start", "CONTAINER [CONTAINER...]", "Restart a stopped container")
  301. if err := cmd.Parse(args); err != nil {
  302. return nil
  303. }
  304. if cmd.NArg() < 1 {
  305. cmd.Usage()
  306. return nil
  307. }
  308. for _, name := range args {
  309. _, err := call("POST", "/containers/"+name+"/start")
  310. if err != nil {
  311. fmt.Printf("%s", err)
  312. } else {
  313. fmt.Println(name)
  314. }
  315. }
  316. return nil
  317. }
  318. func CmdInspect(args []string) error {
  319. cmd := Subcmd("inspect", "CONTAINER|IMAGE", "Return low-level information on a container/image")
  320. if err := cmd.Parse(args); err != nil {
  321. return nil
  322. }
  323. if cmd.NArg() != 1 {
  324. cmd.Usage()
  325. return nil
  326. }
  327. obj, err := call("GET", "/containers/"+cmd.Arg(0))
  328. if err != nil {
  329. obj, err = call("GET", "/images/"+cmd.Arg(0))
  330. if err != nil {
  331. return err
  332. }
  333. }
  334. fmt.Printf("%s\n", obj)
  335. return nil
  336. }
  337. func CmdPort(args []string) error {
  338. cmd := Subcmd("port", "CONTAINER PRIVATE_PORT", "Lookup the public-facing port which is NAT-ed to PRIVATE_PORT")
  339. if err := cmd.Parse(args); err != nil {
  340. return nil
  341. }
  342. if cmd.NArg() != 2 {
  343. cmd.Usage()
  344. return nil
  345. }
  346. v := url.Values{}
  347. v.Set("port", cmd.Arg(1))
  348. body, err := call("GET", "/containers/"+cmd.Arg(0)+"/port?"+v.Encode())
  349. if err != nil {
  350. return err
  351. }
  352. var out ApiPort
  353. err = json.Unmarshal(body, &out)
  354. if err != nil {
  355. return err
  356. }
  357. fmt.Println(out.Port)
  358. return nil
  359. }
  360. // 'docker rmi IMAGE' removes all images with the name IMAGE
  361. func CmdRmi(args []string) error {
  362. cmd := Subcmd("rmi", "IMAGE [IMAGE...]", "Remove an image")
  363. if err := cmd.Parse(args); err != nil {
  364. return nil
  365. }
  366. if cmd.NArg() < 1 {
  367. cmd.Usage()
  368. return nil
  369. }
  370. for _, name := range args {
  371. _, err := call("DELETE", "/images/"+name)
  372. if err != nil {
  373. fmt.Printf("%s", err)
  374. } else {
  375. fmt.Println(name)
  376. }
  377. }
  378. return nil
  379. }
  380. func CmdHistory(args []string) error {
  381. cmd := Subcmd("history", "IMAGE", "Show the history of an image")
  382. if err := cmd.Parse(args); err != nil {
  383. return nil
  384. }
  385. if cmd.NArg() != 1 {
  386. cmd.Usage()
  387. return nil
  388. }
  389. body, err := call("GET", "/images/"+cmd.Arg(0)+"/history")
  390. if err != nil {
  391. return err
  392. }
  393. var outs []ApiHistory
  394. err = json.Unmarshal(body, &outs)
  395. if err != nil {
  396. return err
  397. }
  398. w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
  399. fmt.Fprintln(w, "ID\tCREATED\tCREATED BY")
  400. for _, out := range outs {
  401. fmt.Fprintf(w, "%s\t%s\t%s\n", out.Id, out.Created, out.CreatedBy)
  402. }
  403. w.Flush()
  404. return nil
  405. }
  406. func CmdRm(args []string) error {
  407. cmd := Subcmd("rm", "CONTAINER [CONTAINER...]", "Remove a container")
  408. if err := cmd.Parse(args); err != nil {
  409. return nil
  410. }
  411. if cmd.NArg() < 1 {
  412. cmd.Usage()
  413. return nil
  414. }
  415. for _, name := range args {
  416. _, err := call("DELETE", "/containers/"+name)
  417. if err != nil {
  418. fmt.Printf("%s", err)
  419. } else {
  420. fmt.Println(name)
  421. }
  422. }
  423. return nil
  424. }
  425. // 'docker kill NAME' kills a running container
  426. func CmdKill(args []string) error {
  427. cmd := Subcmd("kill", "CONTAINER [CONTAINER...]", "Kill a running container")
  428. if err := cmd.Parse(args); err != nil {
  429. return nil
  430. }
  431. if cmd.NArg() < 1 {
  432. cmd.Usage()
  433. return nil
  434. }
  435. for _, name := range args {
  436. _, err := call("POST", "/containers/"+name+"/kill")
  437. if err != nil {
  438. fmt.Printf("%s", err)
  439. } else {
  440. fmt.Println(name)
  441. }
  442. }
  443. return nil
  444. }
  445. /*
  446. func (srv *Server) CmdImport(stdin io.ReadCloser, stdout rcli.DockerConn, args ...string) error {
  447. stdout.Flush()
  448. cmd := rcli.Subcmd(stdout, "import", "URL|- [REPOSITORY [TAG]]", "Create a new filesystem image from the contents of a tarball")
  449. var archive io.Reader
  450. var resp *http.Response
  451. if err := cmd.Parse(args); err != nil {
  452. return nil
  453. }
  454. if cmd.NArg() < 1 {
  455. cmd.Usage()
  456. return nil
  457. }
  458. src := cmd.Arg(0)
  459. if src == "-" {
  460. archive = stdin
  461. } else {
  462. u, err := url.Parse(src)
  463. if err != nil {
  464. return err
  465. }
  466. if u.Scheme == "" {
  467. u.Scheme = "http"
  468. u.Host = src
  469. u.Path = ""
  470. }
  471. fmt.Fprintln(stdout, "Downloading from", u)
  472. // Download with curl (pretty progress bar)
  473. // If curl is not available, fallback to http.Get()
  474. resp, err = Download(u.String(), stdout)
  475. if err != nil {
  476. return err
  477. }
  478. archive = ProgressReader(resp.Body, int(resp.ContentLength), stdout)
  479. }
  480. img, err := srv.runtime.graph.Create(archive, nil, "Imported from "+src)
  481. if err != nil {
  482. return err
  483. }
  484. // Optionally register the image at REPO/TAG
  485. if repository := cmd.Arg(1); repository != "" {
  486. tag := cmd.Arg(2) // Repository will handle an empty tag properly
  487. if err := srv.runtime.repositories.Set(repository, tag, img.Id, true); err != nil {
  488. return err
  489. }
  490. }
  491. fmt.Fprintln(stdout, img.ShortId())
  492. return nil
  493. }
  494. */
  495. /*
  496. func (srv *Server) CmdPush(stdin io.ReadCloser, stdout rcli.DockerConn, args ...string) error {
  497. cmd := rcli.Subcmd(stdout, "push", "NAME", "Push an image or a repository to the registry")
  498. if err := cmd.Parse(args); err != nil {
  499. return nil
  500. }
  501. local := cmd.Arg(0)
  502. if local == "" {
  503. cmd.Usage()
  504. return nil
  505. }
  506. // If the login failed, abort
  507. if srv.runtime.authConfig == nil || srv.runtime.authConfig.Username == "" {
  508. if err := srv.CmdLogin(stdin, stdout, args...); err != nil {
  509. return err
  510. }
  511. if srv.runtime.authConfig == nil || srv.runtime.authConfig.Username == "" {
  512. return fmt.Errorf("Please login prior to push. ('docker login')")
  513. }
  514. }
  515. var remote string
  516. tmp := strings.SplitN(local, "/", 2)
  517. if len(tmp) == 1 {
  518. return fmt.Errorf(
  519. "Impossible to push a \"root\" repository. Please rename your repository in <user>/<repo> (ex: %s/%s)",
  520. srv.runtime.authConfig.Username, local)
  521. } else {
  522. remote = local
  523. }
  524. Debugf("Pushing [%s] to [%s]\n", local, remote)
  525. // Try to get the image
  526. // FIXME: Handle lookup
  527. // FIXME: Also push the tags in case of ./docker push myrepo:mytag
  528. // img, err := srv.runtime.LookupImage(cmd.Arg(0))
  529. img, err := srv.runtime.graph.Get(local)
  530. if err != nil {
  531. Debugf("The push refers to a repository [%s] (len: %d)\n", local, len(srv.runtime.repositories.Repositories[local]))
  532. // If it fails, try to get the repository
  533. if localRepo, exists := srv.runtime.repositories.Repositories[local]; exists {
  534. if err := srv.runtime.graph.PushRepository(stdout, remote, localRepo, srv.runtime.authConfig); err != nil {
  535. return err
  536. }
  537. return nil
  538. }
  539. return err
  540. }
  541. err = srv.runtime.graph.PushImage(stdout, img, srv.runtime.authConfig)
  542. if err != nil {
  543. return err
  544. }
  545. return nil
  546. }
  547. */
  548. func CmdPull(args []string) error {
  549. cmd := Subcmd("pull", "NAME", "Pull an image or a repository from the registry")
  550. if err := cmd.Parse(args); err != nil {
  551. return nil
  552. }
  553. if cmd.NArg() != 1 {
  554. cmd.Usage()
  555. return nil
  556. }
  557. if err := callStream("POST", "/images/"+cmd.Arg(0)+"/pull", nil, false); err != nil {
  558. return err
  559. }
  560. return nil
  561. }
  562. func CmdImages(args []string) error {
  563. cmd := Subcmd("images", "[OPTIONS] [NAME]", "List images")
  564. quiet := cmd.Bool("q", false, "only show numeric IDs")
  565. all := cmd.Bool("a", false, "show all images")
  566. if err := cmd.Parse(args); err != nil {
  567. return nil
  568. }
  569. if cmd.NArg() > 1 {
  570. cmd.Usage()
  571. return nil
  572. }
  573. v := url.Values{}
  574. if cmd.NArg() == 1 {
  575. v.Set("filter", cmd.Arg(0))
  576. }
  577. if *quiet {
  578. v.Set("quiet", "1")
  579. }
  580. if *all {
  581. v.Set("all", "1")
  582. }
  583. body, err := call("GET", "/images?"+v.Encode())
  584. if err != nil {
  585. return err
  586. }
  587. var outs []ApiImages
  588. err = json.Unmarshal(body, &outs)
  589. if err != nil {
  590. return err
  591. }
  592. w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
  593. if !*quiet {
  594. fmt.Fprintln(w, "REPOSITORY\tTAG\tID\tCREATED")
  595. }
  596. for _, out := range outs {
  597. if !*quiet {
  598. fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", out.Repository, out.Tag, out.Id, out.Created)
  599. } else {
  600. fmt.Fprintln(w, out.Id)
  601. }
  602. }
  603. if !*quiet {
  604. w.Flush()
  605. }
  606. return nil
  607. }
  608. func CmdPs(args []string) error {
  609. cmd := Subcmd("ps", "[OPTIONS]", "List containers")
  610. quiet := cmd.Bool("q", false, "Only display numeric IDs")
  611. all := cmd.Bool("a", false, "Show all containers. Only running containers are shown by default.")
  612. noTrunc := cmd.Bool("notrunc", false, "Don't truncate output")
  613. nLatest := cmd.Bool("l", false, "Show only the latest created container, include non-running ones.")
  614. last := cmd.Int("n", -1, "Show n last created containers, include non-running ones.")
  615. if err := cmd.Parse(args); err != nil {
  616. return nil
  617. }
  618. v := url.Values{}
  619. if *last == -1 && *nLatest {
  620. *last = 1
  621. }
  622. if *quiet {
  623. v.Set("quiet", "1")
  624. }
  625. if *all {
  626. v.Set("all", "1")
  627. }
  628. if *noTrunc {
  629. v.Set("notrunc", "1")
  630. }
  631. if *last != -1 {
  632. v.Set("n", strconv.Itoa(*last))
  633. }
  634. body, err := call("GET", "/containers?"+v.Encode())
  635. if err != nil {
  636. return err
  637. }
  638. var outs []ApiContainers
  639. err = json.Unmarshal(body, &outs)
  640. if err != nil {
  641. return err
  642. }
  643. w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
  644. if !*quiet {
  645. fmt.Fprintln(w, "ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS")
  646. }
  647. for _, out := range outs {
  648. if !*quiet {
  649. fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", out.Id, out.Image, out.Command, out.Status, out.Created)
  650. } else {
  651. fmt.Fprintln(w, out.Id)
  652. }
  653. }
  654. if !*quiet {
  655. w.Flush()
  656. }
  657. return nil
  658. }
  659. func CmdCommit(args []string) error {
  660. cmd := Subcmd("commit", "[OPTIONS] CONTAINER [REPOSITORY [TAG]]", "Create a new image from a container's changes")
  661. flComment := cmd.String("m", "", "Commit message")
  662. if err := cmd.Parse(args); err != nil {
  663. return nil
  664. }
  665. name, repository, tag := cmd.Arg(0), cmd.Arg(1), cmd.Arg(2)
  666. if name == "" {
  667. cmd.Usage()
  668. return nil
  669. }
  670. v := url.Values{}
  671. v.Set("repo", repository)
  672. v.Set("tag", tag)
  673. v.Set("comment", *flComment)
  674. body, err := call("POST", "/containers/"+name+"/commit?"+v.Encode())
  675. if err != nil {
  676. return err
  677. }
  678. var out ApiCommit
  679. err = json.Unmarshal(body, &out)
  680. if err != nil {
  681. return err
  682. }
  683. fmt.Println(out.Id)
  684. return nil
  685. }
  686. func CmdExport(args []string) error {
  687. cmd := Subcmd("export", "CONTAINER", "Export the contents of a filesystem as a tar archive")
  688. if err := cmd.Parse(args); err != nil {
  689. return nil
  690. }
  691. if cmd.NArg() != 1 {
  692. cmd.Usage()
  693. return nil
  694. }
  695. if err := callStream("GET", "/containers/"+cmd.Arg(0)+"/export", nil, false); err != nil {
  696. return err
  697. }
  698. return nil
  699. }
  700. func CmdDiff(args []string) error {
  701. cmd := Subcmd("diff", "CONTAINER", "Inspect changes on a container's filesystem")
  702. if err := cmd.Parse(args); err != nil {
  703. return nil
  704. }
  705. if cmd.NArg() != 1 {
  706. cmd.Usage()
  707. return nil
  708. }
  709. body, err := call("GET", "/containers/"+cmd.Arg(0)+"/changes")
  710. if err != nil {
  711. return err
  712. }
  713. var changes []string
  714. err = json.Unmarshal(body, &changes)
  715. if err != nil {
  716. return err
  717. }
  718. for _, change := range changes {
  719. fmt.Println(change)
  720. }
  721. return nil
  722. }
  723. func CmdLogs(args []string) error {
  724. cmd := Subcmd("logs", "CONTAINER", "Fetch the logs of a container")
  725. if err := cmd.Parse(args); err != nil {
  726. return nil
  727. }
  728. if cmd.NArg() != 1 {
  729. cmd.Usage()
  730. return nil
  731. }
  732. body, err := call("GET", "/containers/"+cmd.Arg(0)+"/logs")
  733. if err != nil {
  734. return err
  735. }
  736. var out ApiLogs
  737. err = json.Unmarshal(body, &out)
  738. if err != nil {
  739. return err
  740. }
  741. fmt.Fprintln(os.Stdout, out.Stdout)
  742. fmt.Fprintln(os.Stderr, out.Stderr)
  743. return nil
  744. }
  745. func CmdAttach(args []string) error {
  746. cmd := Subcmd("attach", "CONTAINER", "Attach to a running container")
  747. if err := cmd.Parse(args); err != nil {
  748. return nil
  749. }
  750. if cmd.NArg() != 1 {
  751. cmd.Usage()
  752. return nil
  753. }
  754. if err := callStream("POST", "/containers/"+cmd.Arg(0)+"/attach", nil, true); err != nil {
  755. return err
  756. }
  757. return nil
  758. }
  759. /*
  760. // Ports type - Used to parse multiple -p flags
  761. type ports []int
  762. func (p *ports) String() string {
  763. return fmt.Sprint(*p)
  764. }
  765. func (p *ports) Set(value string) error {
  766. port, err := strconv.Atoi(value)
  767. if err != nil {
  768. return fmt.Errorf("Invalid port: %v", value)
  769. }
  770. *p = append(*p, port)
  771. return nil
  772. }
  773. */
  774. // ListOpts type
  775. type ListOpts []string
  776. func (opts *ListOpts) String() string {
  777. return fmt.Sprint(*opts)
  778. }
  779. func (opts *ListOpts) Set(value string) error {
  780. *opts = append(*opts, value)
  781. return nil
  782. }
  783. // AttachOpts stores arguments to 'docker run -a', eg. which streams to attach to
  784. type AttachOpts map[string]bool
  785. func NewAttachOpts() AttachOpts {
  786. return make(AttachOpts)
  787. }
  788. func (opts AttachOpts) String() string {
  789. // Cast to underlying map type to avoid infinite recursion
  790. return fmt.Sprintf("%v", map[string]bool(opts))
  791. }
  792. func (opts AttachOpts) Set(val string) error {
  793. if val != "stdin" && val != "stdout" && val != "stderr" {
  794. return fmt.Errorf("Unsupported stream name: %s", val)
  795. }
  796. opts[val] = true
  797. return nil
  798. }
  799. func (opts AttachOpts) Get(val string) bool {
  800. if res, exists := opts[val]; exists {
  801. return res
  802. }
  803. return false
  804. }
  805. func CmdTag(args []string) error {
  806. cmd := Subcmd("tag", "[OPTIONS] IMAGE REPOSITORY [TAG]", "Tag an image into a repository")
  807. force := cmd.Bool("f", false, "Force")
  808. if err := cmd.Parse(args); err != nil {
  809. return nil
  810. }
  811. if cmd.NArg() != 2 && cmd.NArg() != 3 {
  812. cmd.Usage()
  813. return nil
  814. }
  815. v := url.Values{}
  816. v.Set("repo", cmd.Arg(1))
  817. if cmd.NArg() == 3 {
  818. v.Set("tag", cmd.Arg(2))
  819. }
  820. if *force {
  821. v.Set("force", "1")
  822. }
  823. if err := callStream("POST", "/images/"+cmd.Arg(0)+"/tag?"+v.Encode(), nil, false); err != nil {
  824. return err
  825. }
  826. return nil
  827. }
  828. func CmdRun(args []string) error {
  829. config, cmd, err := ParseRun(args)
  830. if err != nil {
  831. return err
  832. }
  833. if config.Image == "" {
  834. cmd.Usage()
  835. return nil
  836. }
  837. if len(config.Cmd) == 0 {
  838. cmd.Usage()
  839. return nil
  840. }
  841. if err := callStream("POST", "/containers", *config, config.Tty); err != nil {
  842. return err
  843. }
  844. return nil
  845. }
  846. func call(method, path string) ([]byte, error) {
  847. req, err := http.NewRequest(method, "http://0.0.0.0:4243"+path, nil)
  848. if err != nil {
  849. return nil, err
  850. }
  851. if method == "POST" {
  852. req.Header.Set("Content-Type", "plain/text")
  853. }
  854. resp, err := http.DefaultClient.Do(req)
  855. if err != nil {
  856. return nil, err
  857. }
  858. defer resp.Body.Close()
  859. body, err := ioutil.ReadAll(resp.Body)
  860. if err != nil {
  861. return nil, err
  862. }
  863. if resp.StatusCode != 200 {
  864. return nil, fmt.Errorf("error: %s", body)
  865. }
  866. return body, nil
  867. }
  868. func callStream(method, path string, data interface{}, isTerminal bool) error {
  869. var body io.Reader
  870. if data != nil {
  871. buf, err := json.Marshal(data)
  872. if err != nil {
  873. return err
  874. }
  875. body = bytes.NewBuffer(buf)
  876. }
  877. req, err := http.NewRequest(method, path, body)
  878. if err != nil {
  879. return err
  880. }
  881. if data != nil {
  882. req.Header.Set("Content-Type", "application/json")
  883. }
  884. dial, err := net.Dial("tcp", "0.0.0.0:4243")
  885. if err != nil {
  886. return err
  887. }
  888. clientconn := httputil.NewClientConn(dial, nil)
  889. clientconn.Do(req)
  890. defer clientconn.Close()
  891. rwc, _ := clientconn.Hijack()
  892. defer rwc.Close()
  893. receiveStdout := Go(func() error {
  894. _, err := io.Copy(os.Stdout, rwc)
  895. return err
  896. })
  897. sendStdin := Go(func() error {
  898. _, err := io.Copy(rwc, os.Stdin)
  899. rwc.Close()
  900. return err
  901. })
  902. if err := <-receiveStdout; err != nil {
  903. return err
  904. }
  905. if isTerminal {
  906. if err := <-sendStdin; err != nil {
  907. return err
  908. }
  909. }
  910. return nil
  911. }
  912. func Subcmd(name, signature, description string) *flag.FlagSet {
  913. flags := flag.NewFlagSet(name, flag.ContinueOnError)
  914. flags.Usage = func() {
  915. fmt.Printf("\nUsage: docker %s %s\n\n%s\n\n", name, signature, description)
  916. flags.PrintDefaults()
  917. }
  918. return flags
  919. }