commands.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. package docker
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "github.com/dotcloud/docker/auth"
  8. "github.com/dotcloud/docker/term"
  9. "github.com/dotcloud/docker/utils"
  10. "io"
  11. "io/ioutil"
  12. "net"
  13. "net/http"
  14. "net/http/httputil"
  15. "net/url"
  16. "os"
  17. "os/signal"
  18. "path/filepath"
  19. "reflect"
  20. "strconv"
  21. "strings"
  22. "syscall"
  23. "text/tabwriter"
  24. "time"
  25. "unicode"
  26. )
  27. const VERSION = "0.3.3"
  28. var (
  29. GIT_COMMIT string
  30. )
  31. func (cli *DockerCli) getMethod(name string) (reflect.Method, bool) {
  32. methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:])
  33. return reflect.TypeOf(cli).MethodByName(methodName)
  34. }
  35. func ParseCommands(addr string, port int, args ...string) error {
  36. cli := NewDockerCli(addr, port)
  37. if len(args) > 0 {
  38. method, exists := cli.getMethod(args[0])
  39. if !exists {
  40. fmt.Println("Error: Command not found:", args[0])
  41. return cli.CmdHelp(args[1:]...)
  42. }
  43. ret := method.Func.CallSlice([]reflect.Value{
  44. reflect.ValueOf(cli),
  45. reflect.ValueOf(args[1:]),
  46. })[0].Interface()
  47. if ret == nil {
  48. return nil
  49. }
  50. return ret.(error)
  51. }
  52. return cli.CmdHelp(args...)
  53. }
  54. func (cli *DockerCli) CmdHelp(args ...string) error {
  55. if len(args) > 0 {
  56. method, exists := cli.getMethod(args[0])
  57. if !exists {
  58. fmt.Println("Error: Command not found:", args[0])
  59. } else {
  60. method.Func.CallSlice([]reflect.Value{
  61. reflect.ValueOf(cli),
  62. reflect.ValueOf([]string{"--help"}),
  63. })[0].Interface()
  64. return nil
  65. }
  66. }
  67. help := fmt.Sprintf("Usage: docker [OPTIONS] COMMAND [arg...]\n -H=\"%s:%d\": Host:port to bind/connect to\n\nA self-sufficient runtime for linux containers.\n\nCommands:\n", cli.host, cli.port)
  68. for cmd, description := range map[string]string{
  69. "attach": "Attach to a running container",
  70. "build": "Build a container from Dockerfile or via stdin",
  71. "commit": "Create a new image from a container's changes",
  72. "diff": "Inspect changes on a container's filesystem",
  73. "export": "Stream the contents of a container as a tar archive",
  74. "history": "Show the history of an image",
  75. "images": "List images",
  76. "import": "Create a new filesystem image from the contents of a tarball",
  77. "info": "Display system-wide information",
  78. "insert": "Insert a file in an image",
  79. "inspect": "Return low-level information on a container",
  80. "kill": "Kill a running container",
  81. "login": "Register or Login to the docker registry server",
  82. "logs": "Fetch the logs of a container",
  83. "port": "Lookup the public-facing port which is NAT-ed to PRIVATE_PORT",
  84. "ps": "List containers",
  85. "pull": "Pull an image or a repository from the docker registry server",
  86. "push": "Push an image or a repository to the docker registry server",
  87. "restart": "Restart a running container",
  88. "rm": "Remove a container",
  89. "rmi": "Remove an image",
  90. "run": "Run a command in a new container",
  91. "search": "Search for an image in the docker index",
  92. "start": "Start a stopped container",
  93. "stop": "Stop a running container",
  94. "tag": "Tag an image into a repository",
  95. "version": "Show the docker version information",
  96. "wait": "Block until a container stops, then print its exit code",
  97. } {
  98. help += fmt.Sprintf(" %-10.10s%s\n", cmd, description)
  99. }
  100. fmt.Println(help)
  101. return nil
  102. }
  103. func (cli *DockerCli) CmdInsert(args ...string) error {
  104. cmd := Subcmd("insert", "IMAGE URL PATH", "Insert a file from URL in the IMAGE at PATH")
  105. if err := cmd.Parse(args); err != nil {
  106. return nil
  107. }
  108. if cmd.NArg() != 3 {
  109. cmd.Usage()
  110. return nil
  111. }
  112. v := url.Values{}
  113. v.Set("url", cmd.Arg(1))
  114. v.Set("path", cmd.Arg(2))
  115. err := cli.stream("POST", "/images/"+cmd.Arg(0)+"/insert?"+v.Encode(), nil, os.Stdout)
  116. if err != nil {
  117. return err
  118. }
  119. return nil
  120. }
  121. func (cli *DockerCli) CmdBuild(args ...string) error {
  122. cmd := Subcmd("build", "-|Dockerfile", "Build an image from Dockerfile or via stdin")
  123. if err := cmd.Parse(args); err != nil {
  124. return nil
  125. }
  126. var (
  127. file io.ReadCloser
  128. err error
  129. )
  130. if cmd.NArg() == 0 {
  131. file, err = os.Open("Dockerfile")
  132. if err != nil {
  133. return err
  134. }
  135. } else if cmd.Arg(0) == "-" {
  136. file = os.Stdin
  137. } else {
  138. file, err = os.Open(cmd.Arg(0))
  139. if err != nil {
  140. return err
  141. }
  142. }
  143. if _, err := NewBuilderClient("0.0.0.0", 4243).Build(file); err != nil {
  144. return err
  145. }
  146. return nil
  147. }
  148. // 'docker login': login / register a user to registry service.
  149. func (cli *DockerCli) CmdLogin(args ...string) error {
  150. var readStringOnRawTerminal = func(stdin io.Reader, stdout io.Writer, echo bool) string {
  151. char := make([]byte, 1)
  152. buffer := make([]byte, 64)
  153. var i = 0
  154. for i < len(buffer) {
  155. n, err := stdin.Read(char)
  156. if n > 0 {
  157. if char[0] == '\r' || char[0] == '\n' {
  158. stdout.Write([]byte{'\r', '\n'})
  159. break
  160. } else if char[0] == 127 || char[0] == '\b' {
  161. if i > 0 {
  162. if echo {
  163. stdout.Write([]byte{'\b', ' ', '\b'})
  164. }
  165. i--
  166. }
  167. } else if !unicode.IsSpace(rune(char[0])) &&
  168. !unicode.IsControl(rune(char[0])) {
  169. if echo {
  170. stdout.Write(char)
  171. }
  172. buffer[i] = char[0]
  173. i++
  174. }
  175. }
  176. if err != nil {
  177. if err != io.EOF {
  178. fmt.Fprintf(stdout, "Read error: %v\r\n", err)
  179. }
  180. break
  181. }
  182. }
  183. return string(buffer[:i])
  184. }
  185. var readAndEchoString = func(stdin io.Reader, stdout io.Writer) string {
  186. return readStringOnRawTerminal(stdin, stdout, true)
  187. }
  188. var readString = func(stdin io.Reader, stdout io.Writer) string {
  189. return readStringOnRawTerminal(stdin, stdout, false)
  190. }
  191. oldState, err := term.SetRawTerminal()
  192. if err != nil {
  193. return err
  194. } else {
  195. defer term.RestoreTerminal(oldState)
  196. }
  197. cmd := Subcmd("login", "", "Register or Login to the docker registry server")
  198. if err := cmd.Parse(args); err != nil {
  199. return nil
  200. }
  201. body, _, err := cli.call("GET", "/auth", nil)
  202. if err != nil {
  203. return err
  204. }
  205. var out auth.AuthConfig
  206. err = json.Unmarshal(body, &out)
  207. if err != nil {
  208. return err
  209. }
  210. var username string
  211. var password string
  212. var email string
  213. fmt.Print("Username (", out.Username, "): ")
  214. username = readAndEchoString(os.Stdin, os.Stdout)
  215. if username == "" {
  216. username = out.Username
  217. }
  218. if username != out.Username {
  219. fmt.Print("Password: ")
  220. password = readString(os.Stdin, os.Stdout)
  221. if password == "" {
  222. return fmt.Errorf("Error : Password Required")
  223. }
  224. fmt.Print("Email (", out.Email, "): ")
  225. email = readAndEchoString(os.Stdin, os.Stdout)
  226. if email == "" {
  227. email = out.Email
  228. }
  229. } else {
  230. email = out.Email
  231. }
  232. out.Username = username
  233. out.Password = password
  234. out.Email = email
  235. body, _, err = cli.call("POST", "/auth", out)
  236. if err != nil {
  237. return err
  238. }
  239. var out2 ApiAuth
  240. err = json.Unmarshal(body, &out2)
  241. if err != nil {
  242. return err
  243. }
  244. if out2.Status != "" {
  245. term.RestoreTerminal(oldState)
  246. fmt.Print(out2.Status)
  247. }
  248. return nil
  249. }
  250. // 'docker wait': block until a container stops
  251. func (cli *DockerCli) CmdWait(args ...string) error {
  252. cmd := Subcmd("wait", "CONTAINER [CONTAINER...]", "Block until a container stops, then print its exit code.")
  253. if err := cmd.Parse(args); err != nil {
  254. return nil
  255. }
  256. if cmd.NArg() < 1 {
  257. cmd.Usage()
  258. return nil
  259. }
  260. for _, name := range cmd.Args() {
  261. body, _, err := cli.call("POST", "/containers/"+name+"/wait", nil)
  262. if err != nil {
  263. fmt.Printf("%s", err)
  264. } else {
  265. var out ApiWait
  266. err = json.Unmarshal(body, &out)
  267. if err != nil {
  268. return err
  269. }
  270. fmt.Println(out.StatusCode)
  271. }
  272. }
  273. return nil
  274. }
  275. // 'docker version': show version information
  276. func (cli *DockerCli) CmdVersion(args ...string) error {
  277. cmd := Subcmd("version", "", "Show the docker version information.")
  278. fmt.Println(len(args))
  279. if err := cmd.Parse(args); err != nil {
  280. return nil
  281. }
  282. fmt.Println(cmd.NArg())
  283. if cmd.NArg() > 0 {
  284. cmd.Usage()
  285. return nil
  286. }
  287. body, _, err := cli.call("GET", "/version", nil)
  288. if err != nil {
  289. return err
  290. }
  291. var out ApiVersion
  292. err = json.Unmarshal(body, &out)
  293. if err != nil {
  294. utils.Debugf("Error unmarshal: body: %s, err: %s\n", body, err)
  295. return err
  296. }
  297. fmt.Println("Version:", out.Version)
  298. fmt.Println("Git Commit:", out.GitCommit)
  299. if !out.MemoryLimit {
  300. fmt.Println("WARNING: No memory limit support")
  301. }
  302. if !out.SwapLimit {
  303. fmt.Println("WARNING: No swap limit support")
  304. }
  305. return nil
  306. }
  307. // 'docker info': display system-wide information.
  308. func (cli *DockerCli) CmdInfo(args ...string) error {
  309. cmd := Subcmd("info", "", "Display system-wide information")
  310. if err := cmd.Parse(args); err != nil {
  311. return nil
  312. }
  313. if cmd.NArg() > 0 {
  314. cmd.Usage()
  315. return nil
  316. }
  317. body, _, err := cli.call("GET", "/info", nil)
  318. if err != nil {
  319. return err
  320. }
  321. var out ApiInfo
  322. err = json.Unmarshal(body, &out)
  323. if err != nil {
  324. return err
  325. }
  326. fmt.Printf("containers: %d\nversion: %s\nimages: %d\nGo version: %s\n", out.Containers, out.Version, out.Images, out.GoVersion)
  327. if out.Debug {
  328. fmt.Println("debug mode enabled")
  329. fmt.Printf("fds: %d\ngoroutines: %d\n", out.NFd, out.NGoroutines)
  330. }
  331. return nil
  332. }
  333. func (cli *DockerCli) CmdStop(args ...string) error {
  334. cmd := Subcmd("stop", "[OPTIONS] CONTAINER [CONTAINER...]", "Stop a running container")
  335. nSeconds := cmd.Int("t", 10, "wait t seconds before killing the container")
  336. if err := cmd.Parse(args); err != nil {
  337. return nil
  338. }
  339. if cmd.NArg() < 1 {
  340. cmd.Usage()
  341. return nil
  342. }
  343. v := url.Values{}
  344. v.Set("t", strconv.Itoa(*nSeconds))
  345. for _, name := range cmd.Args() {
  346. _, _, err := cli.call("POST", "/containers/"+name+"/stop?"+v.Encode(), nil)
  347. if err != nil {
  348. fmt.Printf("%s", err)
  349. } else {
  350. fmt.Println(name)
  351. }
  352. }
  353. return nil
  354. }
  355. func (cli *DockerCli) CmdRestart(args ...string) error {
  356. cmd := Subcmd("restart", "[OPTIONS] CONTAINER [CONTAINER...]", "Restart a running container")
  357. nSeconds := cmd.Int("t", 10, "wait t seconds before killing the container")
  358. if err := cmd.Parse(args); err != nil {
  359. return nil
  360. }
  361. if cmd.NArg() < 1 {
  362. cmd.Usage()
  363. return nil
  364. }
  365. v := url.Values{}
  366. v.Set("t", strconv.Itoa(*nSeconds))
  367. for _, name := range cmd.Args() {
  368. _, _, err := cli.call("POST", "/containers/"+name+"/restart?"+v.Encode(), nil)
  369. if err != nil {
  370. fmt.Printf("%s", err)
  371. } else {
  372. fmt.Println(name)
  373. }
  374. }
  375. return nil
  376. }
  377. func (cli *DockerCli) CmdStart(args ...string) error {
  378. cmd := Subcmd("start", "CONTAINER [CONTAINER...]", "Restart a stopped container")
  379. if err := cmd.Parse(args); err != nil {
  380. return nil
  381. }
  382. if cmd.NArg() < 1 {
  383. cmd.Usage()
  384. return nil
  385. }
  386. for _, name := range args {
  387. _, _, err := cli.call("POST", "/containers/"+name+"/start", nil)
  388. if err != nil {
  389. fmt.Printf("%s", err)
  390. } else {
  391. fmt.Println(name)
  392. }
  393. }
  394. return nil
  395. }
  396. func (cli *DockerCli) CmdInspect(args ...string) error {
  397. cmd := Subcmd("inspect", "CONTAINER|IMAGE", "Return low-level information on a container/image")
  398. if err := cmd.Parse(args); err != nil {
  399. return nil
  400. }
  401. if cmd.NArg() != 1 {
  402. cmd.Usage()
  403. return nil
  404. }
  405. obj, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/json", nil)
  406. if err != nil {
  407. obj, _, err = cli.call("GET", "/images/"+cmd.Arg(0)+"/json", nil)
  408. if err != nil {
  409. return err
  410. }
  411. }
  412. indented := new(bytes.Buffer)
  413. if err = json.Indent(indented, obj, "", " "); err != nil {
  414. return err
  415. }
  416. if _, err := io.Copy(os.Stdout, indented); err != nil {
  417. return err
  418. }
  419. return nil
  420. }
  421. func (cli *DockerCli) CmdPort(args ...string) error {
  422. cmd := Subcmd("port", "CONTAINER PRIVATE_PORT", "Lookup the public-facing port which is NAT-ed to PRIVATE_PORT")
  423. if err := cmd.Parse(args); err != nil {
  424. return nil
  425. }
  426. if cmd.NArg() != 2 {
  427. cmd.Usage()
  428. return nil
  429. }
  430. body, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/json", nil)
  431. if err != nil {
  432. return err
  433. }
  434. var out Container
  435. err = json.Unmarshal(body, &out)
  436. if err != nil {
  437. return err
  438. }
  439. if frontend, exists := out.NetworkSettings.PortMapping[cmd.Arg(1)]; exists {
  440. fmt.Println(frontend)
  441. } else {
  442. return fmt.Errorf("error: No private port '%s' allocated on %s", cmd.Arg(1), cmd.Arg(0))
  443. }
  444. return nil
  445. }
  446. // 'docker rmi IMAGE' removes all images with the name IMAGE
  447. func (cli *DockerCli) CmdRmi(args ...string) error {
  448. cmd := Subcmd("rmi", "IMAGE [IMAGE...]", "Remove an image")
  449. if err := cmd.Parse(args); err != nil {
  450. return nil
  451. }
  452. if cmd.NArg() < 1 {
  453. cmd.Usage()
  454. return nil
  455. }
  456. for _, name := range cmd.Args() {
  457. _, _, err := cli.call("DELETE", "/images/"+name, nil)
  458. if err != nil {
  459. fmt.Printf("%s", err)
  460. } else {
  461. fmt.Println(name)
  462. }
  463. }
  464. return nil
  465. }
  466. func (cli *DockerCli) CmdHistory(args ...string) error {
  467. cmd := Subcmd("history", "IMAGE", "Show the history of an image")
  468. if err := cmd.Parse(args); err != nil {
  469. return nil
  470. }
  471. if cmd.NArg() != 1 {
  472. cmd.Usage()
  473. return nil
  474. }
  475. body, _, err := cli.call("GET", "/images/"+cmd.Arg(0)+"/history", nil)
  476. if err != nil {
  477. return err
  478. }
  479. var outs []ApiHistory
  480. err = json.Unmarshal(body, &outs)
  481. if err != nil {
  482. return err
  483. }
  484. w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
  485. fmt.Fprintln(w, "ID\tCREATED\tCREATED BY")
  486. for _, out := range outs {
  487. fmt.Fprintf(w, "%s\t%s ago\t%s\n", out.Id, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.CreatedBy)
  488. }
  489. w.Flush()
  490. return nil
  491. }
  492. func (cli *DockerCli) CmdRm(args ...string) error {
  493. cmd := Subcmd("rm", "[OPTIONS] CONTAINER [CONTAINER...]", "Remove a container")
  494. v := cmd.Bool("v", false, "Remove the volumes associated to the container")
  495. if err := cmd.Parse(args); err != nil {
  496. return nil
  497. }
  498. if cmd.NArg() < 1 {
  499. cmd.Usage()
  500. return nil
  501. }
  502. val := url.Values{}
  503. if *v {
  504. val.Set("v", "1")
  505. }
  506. for _, name := range cmd.Args() {
  507. _, _, err := cli.call("DELETE", "/containers/"+name+"?"+val.Encode(), nil)
  508. if err != nil {
  509. fmt.Printf("%s", err)
  510. } else {
  511. fmt.Println(name)
  512. }
  513. }
  514. return nil
  515. }
  516. // 'docker kill NAME' kills a running container
  517. func (cli *DockerCli) CmdKill(args ...string) error {
  518. cmd := Subcmd("kill", "CONTAINER [CONTAINER...]", "Kill a running container")
  519. if err := cmd.Parse(args); err != nil {
  520. return nil
  521. }
  522. if cmd.NArg() < 1 {
  523. cmd.Usage()
  524. return nil
  525. }
  526. for _, name := range args {
  527. _, _, err := cli.call("POST", "/containers/"+name+"/kill", nil)
  528. if err != nil {
  529. fmt.Printf("%s", err)
  530. } else {
  531. fmt.Println(name)
  532. }
  533. }
  534. return nil
  535. }
  536. func (cli *DockerCli) CmdImport(args ...string) error {
  537. cmd := Subcmd("import", "URL|- [REPOSITORY [TAG]]", "Create a new filesystem image from the contents of a tarball")
  538. if err := cmd.Parse(args); err != nil {
  539. return nil
  540. }
  541. if cmd.NArg() < 1 {
  542. cmd.Usage()
  543. return nil
  544. }
  545. src, repository, tag := cmd.Arg(0), cmd.Arg(1), cmd.Arg(2)
  546. v := url.Values{}
  547. v.Set("repo", repository)
  548. v.Set("tag", tag)
  549. v.Set("fromSrc", src)
  550. err := cli.stream("POST", "/images/create?"+v.Encode(), os.Stdin, os.Stdout)
  551. if err != nil {
  552. return err
  553. }
  554. return nil
  555. }
  556. func (cli *DockerCli) CmdPush(args ...string) error {
  557. cmd := Subcmd("push", "[OPTION] NAME", "Push an image or a repository to the registry")
  558. registry := cmd.String("registry", "", "Registry host to push the image to")
  559. if err := cmd.Parse(args); err != nil {
  560. return nil
  561. }
  562. name := cmd.Arg(0)
  563. if name == "" {
  564. cmd.Usage()
  565. return nil
  566. }
  567. username, err := cli.checkIfLogged(*registry == "", "push")
  568. if err != nil {
  569. return err
  570. }
  571. if len(strings.SplitN(name, "/", 2)) == 1 {
  572. return fmt.Errorf("Impossible to push a \"root\" repository. Please rename your repository in <user>/<repo> (ex: %s/%s)", username, name)
  573. }
  574. v := url.Values{}
  575. v.Set("registry", *registry)
  576. if err := cli.stream("POST", "/images/"+name+"/push?"+v.Encode(), nil, os.Stdout); err != nil {
  577. return err
  578. }
  579. return nil
  580. }
  581. func (cli *DockerCli) CmdPull(args ...string) error {
  582. cmd := Subcmd("pull", "NAME", "Pull an image or a repository from the registry")
  583. tag := cmd.String("t", "", "Download tagged image in repository")
  584. registry := cmd.String("registry", "", "Registry to download from. Necessary if image is pulled by ID")
  585. if err := cmd.Parse(args); err != nil {
  586. return nil
  587. }
  588. if cmd.NArg() != 1 {
  589. cmd.Usage()
  590. return nil
  591. }
  592. remote := cmd.Arg(0)
  593. if strings.Contains(remote, ":") {
  594. remoteParts := strings.Split(remote, ":")
  595. tag = &remoteParts[1]
  596. remote = remoteParts[0]
  597. }
  598. if strings.Contains(remote, "/") {
  599. if _, err := cli.checkIfLogged(true, "pull"); err != nil {
  600. return err
  601. }
  602. }
  603. v := url.Values{}
  604. v.Set("fromImage", remote)
  605. v.Set("tag", *tag)
  606. v.Set("registry", *registry)
  607. if err := cli.stream("POST", "/images/create?"+v.Encode(), nil, os.Stdout); err != nil {
  608. return err
  609. }
  610. return nil
  611. }
  612. func (cli *DockerCli) CmdImages(args ...string) error {
  613. cmd := Subcmd("images", "[OPTIONS] [NAME]", "List images")
  614. quiet := cmd.Bool("q", false, "only show numeric IDs")
  615. all := cmd.Bool("a", false, "show all images")
  616. noTrunc := cmd.Bool("notrunc", false, "Don't truncate output")
  617. flViz := cmd.Bool("viz", false, "output graph in graphviz format")
  618. if err := cmd.Parse(args); err != nil {
  619. return nil
  620. }
  621. if cmd.NArg() > 1 {
  622. cmd.Usage()
  623. return nil
  624. }
  625. if *flViz {
  626. body, _, err := cli.call("GET", "/images/viz", false)
  627. if err != nil {
  628. return err
  629. }
  630. fmt.Printf("%s", body)
  631. } else {
  632. v := url.Values{}
  633. if cmd.NArg() == 1 {
  634. v.Set("filter", cmd.Arg(0))
  635. }
  636. if *all {
  637. v.Set("all", "1")
  638. }
  639. body, _, err := cli.call("GET", "/images/json?"+v.Encode(), nil)
  640. if err != nil {
  641. return err
  642. }
  643. var outs []ApiImages
  644. err = json.Unmarshal(body, &outs)
  645. if err != nil {
  646. return err
  647. }
  648. w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
  649. if !*quiet {
  650. fmt.Fprintln(w, "REPOSITORY\tTAG\tID\tCREATED")
  651. }
  652. for _, out := range outs {
  653. if out.Repository == "" {
  654. out.Repository = "<none>"
  655. }
  656. if out.Tag == "" {
  657. out.Tag = "<none>"
  658. }
  659. if !*quiet {
  660. fmt.Fprintf(w, "%s\t%s\t", out.Repository, out.Tag)
  661. if *noTrunc {
  662. fmt.Fprintf(w, "%s\t", out.Id)
  663. } else {
  664. fmt.Fprintf(w, "%s\t", utils.TruncateId(out.Id))
  665. }
  666. fmt.Fprintf(w, "%s ago\n", utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))))
  667. } else {
  668. if *noTrunc {
  669. fmt.Fprintln(w, out.Id)
  670. } else {
  671. fmt.Fprintln(w, utils.TruncateId(out.Id))
  672. }
  673. }
  674. }
  675. if !*quiet {
  676. w.Flush()
  677. }
  678. }
  679. return nil
  680. }
  681. func (cli *DockerCli) CmdPs(args ...string) error {
  682. cmd := Subcmd("ps", "[OPTIONS]", "List containers")
  683. quiet := cmd.Bool("q", false, "Only display numeric IDs")
  684. all := cmd.Bool("a", false, "Show all containers. Only running containers are shown by default.")
  685. noTrunc := cmd.Bool("notrunc", false, "Don't truncate output")
  686. nLatest := cmd.Bool("l", false, "Show only the latest created container, include non-running ones.")
  687. since := cmd.String("sinceId", "", "Show only containers created since Id, include non-running ones.")
  688. before := cmd.String("beforeId", "", "Show only container created before Id, include non-running ones.")
  689. last := cmd.Int("n", -1, "Show n last created containers, include non-running ones.")
  690. if err := cmd.Parse(args); err != nil {
  691. return nil
  692. }
  693. v := url.Values{}
  694. if *last == -1 && *nLatest {
  695. *last = 1
  696. }
  697. if *all {
  698. v.Set("all", "1")
  699. }
  700. if *last != -1 {
  701. v.Set("limit", strconv.Itoa(*last))
  702. }
  703. if *since != "" {
  704. v.Set("since", *since)
  705. }
  706. if *before != "" {
  707. v.Set("before", *before)
  708. }
  709. body, _, err := cli.call("GET", "/containers/json?"+v.Encode(), nil)
  710. if err != nil {
  711. return err
  712. }
  713. var outs []ApiContainers
  714. err = json.Unmarshal(body, &outs)
  715. if err != nil {
  716. return err
  717. }
  718. w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
  719. if !*quiet {
  720. fmt.Fprintln(w, "ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS")
  721. }
  722. for _, out := range outs {
  723. if !*quiet {
  724. if *noTrunc {
  725. fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", out.Id, out.Image, out.Command, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Status, out.Ports)
  726. } else {
  727. fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", utils.TruncateId(out.Id), out.Image, utils.Trunc(out.Command, 20), utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Status, out.Ports)
  728. }
  729. } else {
  730. if *noTrunc {
  731. fmt.Fprintln(w, out.Id)
  732. } else {
  733. fmt.Fprintln(w, utils.TruncateId(out.Id))
  734. }
  735. }
  736. }
  737. if !*quiet {
  738. w.Flush()
  739. }
  740. return nil
  741. }
  742. func (cli *DockerCli) CmdCommit(args ...string) error {
  743. cmd := Subcmd("commit", "[OPTIONS] CONTAINER [REPOSITORY [TAG]]", "Create a new image from a container's changes")
  744. flComment := cmd.String("m", "", "Commit message")
  745. flAuthor := cmd.String("author", "", "Author (eg. \"John Hannibal Smith <hannibal@a-team.com>\"")
  746. flConfig := cmd.String("run", "", "Config automatically applied when the image is run. "+`(ex: {"Cmd": ["cat", "/world"], "PortSpecs": ["22"]}')`)
  747. if err := cmd.Parse(args); err != nil {
  748. return nil
  749. }
  750. name, repository, tag := cmd.Arg(0), cmd.Arg(1), cmd.Arg(2)
  751. if name == "" {
  752. cmd.Usage()
  753. return nil
  754. }
  755. v := url.Values{}
  756. v.Set("container", name)
  757. v.Set("repo", repository)
  758. v.Set("tag", tag)
  759. v.Set("comment", *flComment)
  760. v.Set("author", *flAuthor)
  761. var config *Config
  762. if *flConfig != "" {
  763. config = &Config{}
  764. if err := json.Unmarshal([]byte(*flConfig), config); err != nil {
  765. return err
  766. }
  767. }
  768. body, _, err := cli.call("POST", "/commit?"+v.Encode(), config)
  769. if err != nil {
  770. return err
  771. }
  772. apiId := &ApiId{}
  773. err = json.Unmarshal(body, apiId)
  774. if err != nil {
  775. return err
  776. }
  777. fmt.Println(apiId.Id)
  778. return nil
  779. }
  780. func (cli *DockerCli) CmdExport(args ...string) error {
  781. cmd := Subcmd("export", "CONTAINER", "Export the contents of a filesystem as a tar archive")
  782. if err := cmd.Parse(args); err != nil {
  783. return nil
  784. }
  785. if cmd.NArg() != 1 {
  786. cmd.Usage()
  787. return nil
  788. }
  789. if err := cli.stream("GET", "/containers/"+cmd.Arg(0)+"/export", nil, os.Stdout); err != nil {
  790. return err
  791. }
  792. return nil
  793. }
  794. func (cli *DockerCli) CmdDiff(args ...string) error {
  795. cmd := Subcmd("diff", "CONTAINER", "Inspect changes on a container's filesystem")
  796. if err := cmd.Parse(args); err != nil {
  797. return nil
  798. }
  799. if cmd.NArg() != 1 {
  800. cmd.Usage()
  801. return nil
  802. }
  803. body, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/changes", nil)
  804. if err != nil {
  805. return err
  806. }
  807. changes := []Change{}
  808. err = json.Unmarshal(body, &changes)
  809. if err != nil {
  810. return err
  811. }
  812. for _, change := range changes {
  813. fmt.Println(change.String())
  814. }
  815. return nil
  816. }
  817. func (cli *DockerCli) CmdLogs(args ...string) error {
  818. cmd := Subcmd("logs", "CONTAINER", "Fetch the logs of a container")
  819. if err := cmd.Parse(args); err != nil {
  820. return nil
  821. }
  822. if cmd.NArg() != 1 {
  823. cmd.Usage()
  824. return nil
  825. }
  826. v := url.Values{}
  827. v.Set("logs", "1")
  828. v.Set("stdout", "1")
  829. v.Set("stderr", "1")
  830. if err := cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), false); err != nil {
  831. return err
  832. }
  833. return nil
  834. }
  835. func (cli *DockerCli) CmdAttach(args ...string) error {
  836. cmd := Subcmd("attach", "CONTAINER", "Attach to a running container")
  837. if err := cmd.Parse(args); err != nil {
  838. return nil
  839. }
  840. if cmd.NArg() != 1 {
  841. cmd.Usage()
  842. return nil
  843. }
  844. body, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/json", nil)
  845. if err != nil {
  846. return err
  847. }
  848. container := &Container{}
  849. err = json.Unmarshal(body, container)
  850. if err != nil {
  851. return err
  852. }
  853. v := url.Values{}
  854. v.Set("stream", "1")
  855. v.Set("stdout", "1")
  856. v.Set("stderr", "1")
  857. v.Set("stdin", "1")
  858. cli.monitorTtySize(cmd.Arg(0))
  859. if err := cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), container.Config.Tty); err != nil {
  860. return err
  861. }
  862. return nil
  863. }
  864. func (cli *DockerCli) CmdSearch(args ...string) error {
  865. cmd := Subcmd("search", "NAME", "Search the docker index for images")
  866. if err := cmd.Parse(args); err != nil {
  867. return nil
  868. }
  869. if cmd.NArg() != 1 {
  870. cmd.Usage()
  871. return nil
  872. }
  873. v := url.Values{}
  874. v.Set("term", cmd.Arg(0))
  875. body, _, err := cli.call("GET", "/images/search?"+v.Encode(), nil)
  876. if err != nil {
  877. return err
  878. }
  879. outs := []ApiSearch{}
  880. err = json.Unmarshal(body, &outs)
  881. if err != nil {
  882. return err
  883. }
  884. fmt.Printf("Found %d results matching your query (\"%s\")\n", len(outs), cmd.Arg(0))
  885. w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
  886. fmt.Fprintf(w, "NAME\tDESCRIPTION\n")
  887. for _, out := range outs {
  888. fmt.Fprintf(w, "%s\t%s\n", out.Name, out.Description)
  889. }
  890. w.Flush()
  891. return nil
  892. }
  893. // Ports type - Used to parse multiple -p flags
  894. type ports []int
  895. // ListOpts type
  896. type ListOpts []string
  897. func (opts *ListOpts) String() string {
  898. return fmt.Sprint(*opts)
  899. }
  900. func (opts *ListOpts) Set(value string) error {
  901. *opts = append(*opts, value)
  902. return nil
  903. }
  904. // AttachOpts stores arguments to 'docker run -a', eg. which streams to attach to
  905. type AttachOpts map[string]bool
  906. func NewAttachOpts() AttachOpts {
  907. return make(AttachOpts)
  908. }
  909. func (opts AttachOpts) String() string {
  910. // Cast to underlying map type to avoid infinite recursion
  911. return fmt.Sprintf("%v", map[string]bool(opts))
  912. }
  913. func (opts AttachOpts) Set(val string) error {
  914. if val != "stdin" && val != "stdout" && val != "stderr" {
  915. return fmt.Errorf("Unsupported stream name: %s", val)
  916. }
  917. opts[val] = true
  918. return nil
  919. }
  920. func (opts AttachOpts) Get(val string) bool {
  921. if res, exists := opts[val]; exists {
  922. return res
  923. }
  924. return false
  925. }
  926. // PathOpts stores a unique set of absolute paths
  927. type PathOpts map[string]struct{}
  928. func NewPathOpts() PathOpts {
  929. return make(PathOpts)
  930. }
  931. func (opts PathOpts) String() string {
  932. return fmt.Sprintf("%v", map[string]struct{}(opts))
  933. }
  934. func (opts PathOpts) Set(val string) error {
  935. if !filepath.IsAbs(val) {
  936. return fmt.Errorf("%s is not an absolute path", val)
  937. }
  938. opts[filepath.Clean(val)] = struct{}{}
  939. return nil
  940. }
  941. func (cli *DockerCli) CmdTag(args ...string) error {
  942. cmd := Subcmd("tag", "[OPTIONS] IMAGE REPOSITORY [TAG]", "Tag an image into a repository")
  943. force := cmd.Bool("f", false, "Force")
  944. if err := cmd.Parse(args); err != nil {
  945. return nil
  946. }
  947. if cmd.NArg() != 2 && cmd.NArg() != 3 {
  948. cmd.Usage()
  949. return nil
  950. }
  951. v := url.Values{}
  952. v.Set("repo", cmd.Arg(1))
  953. if cmd.NArg() == 3 {
  954. v.Set("tag", cmd.Arg(2))
  955. }
  956. if *force {
  957. v.Set("force", "1")
  958. }
  959. if _, _, err := cli.call("POST", "/images/"+cmd.Arg(0)+"/tag?"+v.Encode(), nil); err != nil {
  960. return err
  961. }
  962. return nil
  963. }
  964. func (cli *DockerCli) CmdRun(args ...string) error {
  965. config, cmd, err := ParseRun(args, nil)
  966. if err != nil {
  967. return err
  968. }
  969. if config.Image == "" {
  970. cmd.Usage()
  971. return nil
  972. }
  973. //create the container
  974. body, statusCode, err := cli.call("POST", "/containers/create", config)
  975. //if image not found try to pull it
  976. if statusCode == 404 {
  977. v := url.Values{}
  978. v.Set("fromImage", config.Image)
  979. err = cli.stream("POST", "/images/create?"+v.Encode(), nil, os.Stderr)
  980. if err != nil {
  981. return err
  982. }
  983. body, _, err = cli.call("POST", "/containers/create", config)
  984. if err != nil {
  985. return err
  986. }
  987. }
  988. if err != nil {
  989. return err
  990. }
  991. out := &ApiRun{}
  992. err = json.Unmarshal(body, out)
  993. if err != nil {
  994. return err
  995. }
  996. for _, warning := range out.Warnings {
  997. fmt.Fprintln(os.Stderr, "WARNING: ", warning)
  998. }
  999. v := url.Values{}
  1000. v.Set("logs", "1")
  1001. v.Set("stream", "1")
  1002. if config.AttachStdin {
  1003. v.Set("stdin", "1")
  1004. }
  1005. if config.AttachStdout {
  1006. v.Set("stdout", "1")
  1007. }
  1008. if config.AttachStderr {
  1009. v.Set("stderr", "1")
  1010. }
  1011. //start the container
  1012. _, _, err = cli.call("POST", "/containers/"+out.Id+"/start", nil)
  1013. if err != nil {
  1014. return err
  1015. }
  1016. if config.AttachStdin || config.AttachStdout || config.AttachStderr {
  1017. cli.monitorTtySize(out.Id)
  1018. if err := cli.hijack("POST", "/containers/"+out.Id+"/attach?"+v.Encode(), config.Tty); err != nil {
  1019. return err
  1020. }
  1021. }
  1022. if !config.AttachStdout && !config.AttachStderr {
  1023. fmt.Println(out.Id)
  1024. }
  1025. return nil
  1026. }
  1027. func (cli *DockerCli) checkIfLogged(condition bool, action string) (string, error) {
  1028. body, _, err := cli.call("GET", "/auth", nil)
  1029. if err != nil {
  1030. return "", err
  1031. }
  1032. var out auth.AuthConfig
  1033. err = json.Unmarshal(body, &out)
  1034. if err != nil {
  1035. return "", err
  1036. }
  1037. // If condition AND the login failed
  1038. if condition && out.Username == "" {
  1039. if err := cli.CmdLogin(""); err != nil {
  1040. return "", err
  1041. }
  1042. body, _, err = cli.call("GET", "/auth", nil)
  1043. if err != nil {
  1044. return "", err
  1045. }
  1046. err = json.Unmarshal(body, &out)
  1047. if err != nil {
  1048. return "", err
  1049. }
  1050. if out.Username == "" {
  1051. return "", fmt.Errorf("Please login prior to %s. ('docker login')", action)
  1052. }
  1053. }
  1054. return out.Username, nil
  1055. }
  1056. func (cli *DockerCli) call(method, path string, data interface{}) ([]byte, int, error) {
  1057. var params io.Reader
  1058. if data != nil {
  1059. buf, err := json.Marshal(data)
  1060. if err != nil {
  1061. return nil, -1, err
  1062. }
  1063. params = bytes.NewBuffer(buf)
  1064. }
  1065. req, err := http.NewRequest(method, fmt.Sprintf("http://%s:%d/v%g%s", cli.host, cli.port, API_VERSION, path), params)
  1066. if err != nil {
  1067. return nil, -1, err
  1068. }
  1069. req.Header.Set("User-Agent", "Docker-Client/"+VERSION)
  1070. if data != nil {
  1071. req.Header.Set("Content-Type", "application/json")
  1072. } else if method == "POST" {
  1073. req.Header.Set("Content-Type", "plain/text")
  1074. }
  1075. resp, err := http.DefaultClient.Do(req)
  1076. if err != nil {
  1077. if strings.Contains(err.Error(), "connection refused") {
  1078. return nil, -1, fmt.Errorf("Can't connect to docker daemon. Is 'docker -d' running on this host?")
  1079. }
  1080. return nil, -1, err
  1081. }
  1082. defer resp.Body.Close()
  1083. body, err := ioutil.ReadAll(resp.Body)
  1084. if err != nil {
  1085. return nil, -1, err
  1086. }
  1087. if resp.StatusCode < 200 || resp.StatusCode >= 400 {
  1088. return nil, resp.StatusCode, fmt.Errorf("error: %s", body)
  1089. }
  1090. return body, resp.StatusCode, nil
  1091. }
  1092. func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) error {
  1093. if (method == "POST" || method == "PUT") && in == nil {
  1094. in = bytes.NewReader([]byte{})
  1095. }
  1096. req, err := http.NewRequest(method, fmt.Sprintf("http://%s:%d/v%g%s", cli.host, cli.port, API_VERSION, path), in)
  1097. if err != nil {
  1098. return err
  1099. }
  1100. req.Header.Set("User-Agent", "Docker-Client/"+VERSION)
  1101. if method == "POST" {
  1102. req.Header.Set("Content-Type", "plain/text")
  1103. }
  1104. resp, err := http.DefaultClient.Do(req)
  1105. if err != nil {
  1106. if strings.Contains(err.Error(), "connection refused") {
  1107. return fmt.Errorf("Can't connect to docker daemon. Is 'docker -d' running on this host?")
  1108. }
  1109. return err
  1110. }
  1111. defer resp.Body.Close()
  1112. if resp.StatusCode < 200 || resp.StatusCode >= 400 {
  1113. body, err := ioutil.ReadAll(resp.Body)
  1114. if err != nil {
  1115. return err
  1116. }
  1117. return fmt.Errorf("error: %s", body)
  1118. }
  1119. if resp.Header.Get("Content-Type") == "application/json" {
  1120. type Message struct {
  1121. Status string `json:"status,omitempty"`
  1122. Progress string `json:"progress,omitempty"`
  1123. }
  1124. dec := json.NewDecoder(resp.Body)
  1125. for {
  1126. var m Message
  1127. if err := dec.Decode(&m); err == io.EOF {
  1128. break
  1129. } else if err != nil {
  1130. return err
  1131. }
  1132. if m.Progress != "" {
  1133. fmt.Fprintf(out, "Downloading %s\r", m.Progress)
  1134. } else {
  1135. fmt.Fprintf(out, "%s\n", m.Status)
  1136. }
  1137. }
  1138. } else {
  1139. if _, err := io.Copy(out, resp.Body); err != nil {
  1140. return err
  1141. }
  1142. }
  1143. return nil
  1144. }
  1145. func (cli *DockerCli) hijack(method, path string, setRawTerminal bool) error {
  1146. req, err := http.NewRequest(method, fmt.Sprintf("/v%g%s", API_VERSION, path), nil)
  1147. if err != nil {
  1148. return err
  1149. }
  1150. req.Header.Set("Content-Type", "plain/text")
  1151. dial, err := net.Dial("tcp", fmt.Sprintf("%s:%d", cli.host, cli.port))
  1152. if err != nil {
  1153. return err
  1154. }
  1155. clientconn := httputil.NewClientConn(dial, nil)
  1156. clientconn.Do(req)
  1157. defer clientconn.Close()
  1158. rwc, br := clientconn.Hijack()
  1159. defer rwc.Close()
  1160. receiveStdout := utils.Go(func() error {
  1161. _, err := io.Copy(os.Stdout, br)
  1162. return err
  1163. })
  1164. if setRawTerminal && term.IsTerminal(int(os.Stdin.Fd())) && os.Getenv("NORAW") == "" {
  1165. if oldState, err := term.SetRawTerminal(); err != nil {
  1166. return err
  1167. } else {
  1168. defer term.RestoreTerminal(oldState)
  1169. }
  1170. }
  1171. sendStdin := utils.Go(func() error {
  1172. _, err := io.Copy(rwc, os.Stdin)
  1173. if err := rwc.(*net.TCPConn).CloseWrite(); err != nil {
  1174. fmt.Fprintf(os.Stderr, "Couldn't send EOF: %s\n", err)
  1175. }
  1176. return err
  1177. })
  1178. if err := <-receiveStdout; err != nil {
  1179. return err
  1180. }
  1181. if !term.IsTerminal(int(os.Stdin.Fd())) {
  1182. if err := <-sendStdin; err != nil {
  1183. return err
  1184. }
  1185. }
  1186. return nil
  1187. }
  1188. func (cli *DockerCli) resizeTty(id string) {
  1189. ws, err := term.GetWinsize(os.Stdin.Fd())
  1190. if err != nil {
  1191. utils.Debugf("Error getting size: %s", err)
  1192. }
  1193. v := url.Values{}
  1194. v.Set("h", strconv.Itoa(int(ws.Height)))
  1195. v.Set("w", strconv.Itoa(int(ws.Width)))
  1196. if _, _, err := cli.call("POST", "/containers/"+id+"/resize?"+v.Encode(), nil); err != nil {
  1197. utils.Debugf("Error resize: %s", err)
  1198. }
  1199. }
  1200. func (cli *DockerCli) monitorTtySize(id string) {
  1201. cli.resizeTty(id)
  1202. c := make(chan os.Signal, 1)
  1203. signal.Notify(c, syscall.SIGWINCH)
  1204. go func() {
  1205. for sig := range c {
  1206. if sig == syscall.SIGWINCH {
  1207. cli.resizeTty(id)
  1208. }
  1209. }
  1210. }()
  1211. }
  1212. func Subcmd(name, signature, description string) *flag.FlagSet {
  1213. flags := flag.NewFlagSet(name, flag.ContinueOnError)
  1214. flags.Usage = func() {
  1215. fmt.Printf("\nUsage: docker %s %s\n\n%s\n\n", name, signature, description)
  1216. flags.PrintDefaults()
  1217. }
  1218. return flags
  1219. }
  1220. func NewDockerCli(addr string, port int) *DockerCli {
  1221. return &DockerCli{addr, port}
  1222. }
  1223. type DockerCli struct {
  1224. host string
  1225. port int
  1226. }