commands.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. package docker
  2. import (
  3. "archive/tar"
  4. "bufio"
  5. "bytes"
  6. "encoding/base64"
  7. "encoding/json"
  8. "flag"
  9. "fmt"
  10. "github.com/dotcloud/docker/auth"
  11. "github.com/dotcloud/docker/registry"
  12. "github.com/dotcloud/docker/term"
  13. "github.com/dotcloud/docker/utils"
  14. "io"
  15. "io/ioutil"
  16. "net"
  17. "net/http"
  18. "net/http/httputil"
  19. "net/url"
  20. "os"
  21. "os/signal"
  22. "path/filepath"
  23. "reflect"
  24. "runtime"
  25. "sort"
  26. "strconv"
  27. "strings"
  28. "syscall"
  29. "text/tabwriter"
  30. "time"
  31. )
  32. var (
  33. GITCOMMIT string
  34. VERSION string
  35. )
  36. func (cli *DockerCli) getMethod(name string) (reflect.Method, bool) {
  37. methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:])
  38. return reflect.TypeOf(cli).MethodByName(methodName)
  39. }
  40. func ParseCommands(proto, addr string, args ...string) error {
  41. cli := NewDockerCli(os.Stdin, os.Stdout, os.Stderr, proto, addr)
  42. if len(args) > 0 {
  43. method, exists := cli.getMethod(args[0])
  44. if !exists {
  45. fmt.Println("Error: Command not found:", args[0])
  46. return cli.CmdHelp(args[1:]...)
  47. }
  48. ret := method.Func.CallSlice([]reflect.Value{
  49. reflect.ValueOf(cli),
  50. reflect.ValueOf(args[1:]),
  51. })[0].Interface()
  52. if ret == nil {
  53. return nil
  54. }
  55. return ret.(error)
  56. }
  57. return cli.CmdHelp(args...)
  58. }
  59. func (cli *DockerCli) CmdHelp(args ...string) error {
  60. if len(args) > 0 {
  61. method, exists := cli.getMethod(args[0])
  62. if !exists {
  63. fmt.Fprintf(cli.err, "Error: Command not found: %s\n", args[0])
  64. } else {
  65. method.Func.CallSlice([]reflect.Value{
  66. reflect.ValueOf(cli),
  67. reflect.ValueOf([]string{"--help"}),
  68. })[0].Interface()
  69. return nil
  70. }
  71. }
  72. help := fmt.Sprintf("Usage: docker [OPTIONS] COMMAND [arg...]\n -H=[unix://%s]: tcp://host:port to bind/connect to or unix://path/to/socket to use\n\nA self-sufficient runtime for linux containers.\n\nCommands:\n", DEFAULTUNIXSOCKET)
  73. for _, command := range [][]string{
  74. {"attach", "Attach to a running container"},
  75. {"build", "Build a container from a Dockerfile"},
  76. {"commit", "Create a new image from a container's changes"},
  77. {"cp", "Copy files/folders from the containers filesystem to the host path"},
  78. {"diff", "Inspect changes on a container's filesystem"},
  79. {"events", "Get real time events from the server"},
  80. {"export", "Stream the contents of a container as a tar archive"},
  81. {"history", "Show the history of an image"},
  82. {"images", "List images"},
  83. {"import", "Create a new filesystem image from the contents of a tarball"},
  84. {"info", "Display system-wide information"},
  85. {"insert", "Insert a file in an image"},
  86. {"inspect", "Return low-level information on a container"},
  87. {"kill", "Kill a running container"},
  88. {"login", "Register or Login to the docker registry server"},
  89. {"logs", "Fetch the logs of a container"},
  90. {"port", "Lookup the public-facing port which is NAT-ed to PRIVATE_PORT"},
  91. {"top", "Lookup the running processes of a container"},
  92. {"ps", "List containers"},
  93. {"pull", "Pull an image or a repository from the docker registry server"},
  94. {"push", "Push an image or a repository to the docker registry server"},
  95. {"restart", "Restart a running container"},
  96. {"rm", "Remove one or more containers"},
  97. {"rmi", "Remove one or more images"},
  98. {"run", "Run a command in a new container"},
  99. {"search", "Search for an image in the docker index"},
  100. {"start", "Start a stopped container"},
  101. {"stop", "Stop a running container"},
  102. {"tag", "Tag an image into a repository"},
  103. {"version", "Show the docker version information"},
  104. {"wait", "Block until a container stops, then print its exit code"},
  105. } {
  106. help += fmt.Sprintf(" %-10.10s%s\n", command[0], command[1])
  107. }
  108. fmt.Fprintf(cli.err, "%s\n", help)
  109. return nil
  110. }
  111. func (cli *DockerCli) CmdInsert(args ...string) error {
  112. cmd := Subcmd("insert", "IMAGE URL PATH", "Insert a file from URL in the IMAGE at PATH")
  113. if err := cmd.Parse(args); err != nil {
  114. return nil
  115. }
  116. if cmd.NArg() != 3 {
  117. cmd.Usage()
  118. return nil
  119. }
  120. v := url.Values{}
  121. v.Set("url", cmd.Arg(1))
  122. v.Set("path", cmd.Arg(2))
  123. if err := cli.stream("POST", "/images/"+cmd.Arg(0)+"/insert?"+v.Encode(), nil, cli.out, nil); err != nil {
  124. return err
  125. }
  126. return nil
  127. }
  128. // mkBuildContext returns an archive of an empty context with the contents
  129. // of `dockerfile` at the path ./Dockerfile
  130. func mkBuildContext(dockerfile string, files [][2]string) (Archive, error) {
  131. buf := new(bytes.Buffer)
  132. tw := tar.NewWriter(buf)
  133. files = append(files, [2]string{"Dockerfile", dockerfile})
  134. for _, file := range files {
  135. name, content := file[0], file[1]
  136. hdr := &tar.Header{
  137. Name: name,
  138. Size: int64(len(content)),
  139. }
  140. if err := tw.WriteHeader(hdr); err != nil {
  141. return nil, err
  142. }
  143. if _, err := tw.Write([]byte(content)); err != nil {
  144. return nil, err
  145. }
  146. }
  147. if err := tw.Close(); err != nil {
  148. return nil, err
  149. }
  150. return buf, nil
  151. }
  152. func (cli *DockerCli) CmdBuild(args ...string) error {
  153. cmd := Subcmd("build", "[OPTIONS] PATH | URL | -", "Build a new container image from the source code at PATH")
  154. tag := cmd.String("t", "", "Repository name (and optionally a tag) to be applied to the resulting image in case of success")
  155. suppressOutput := cmd.Bool("q", false, "Suppress verbose build output")
  156. noCache := cmd.Bool("no-cache", false, "Do not use cache when building the image")
  157. rm := cmd.Bool("rm", false, "Remove intermediate containers after a successful build")
  158. if err := cmd.Parse(args); err != nil {
  159. return nil
  160. }
  161. if cmd.NArg() != 1 {
  162. cmd.Usage()
  163. return nil
  164. }
  165. var (
  166. context Archive
  167. isRemote bool
  168. err error
  169. )
  170. if cmd.Arg(0) == "-" {
  171. // As a special case, 'docker build -' will build from an empty context with the
  172. // contents of stdin as a Dockerfile
  173. dockerfile, err := ioutil.ReadAll(cli.in)
  174. if err != nil {
  175. return err
  176. }
  177. context, err = mkBuildContext(string(dockerfile), nil)
  178. } else if utils.IsURL(cmd.Arg(0)) || utils.IsGIT(cmd.Arg(0)) {
  179. isRemote = true
  180. } else {
  181. if _, err := os.Stat(cmd.Arg(0)); err != nil {
  182. return err
  183. }
  184. context, err = Tar(cmd.Arg(0), Uncompressed)
  185. }
  186. var body io.Reader
  187. // Setup an upload progress bar
  188. // FIXME: ProgressReader shouldn't be this annoying to use
  189. if context != nil {
  190. sf := utils.NewStreamFormatter(false)
  191. body = utils.ProgressReader(ioutil.NopCloser(context), 0, cli.err, sf.FormatProgress("", "Uploading context", "%v bytes%0.0s%0.0s"), sf, true)
  192. }
  193. // Upload the build context
  194. v := &url.Values{}
  195. v.Set("t", *tag)
  196. if *suppressOutput {
  197. v.Set("q", "1")
  198. }
  199. if isRemote {
  200. v.Set("remote", cmd.Arg(0))
  201. }
  202. if *noCache {
  203. v.Set("nocache", "1")
  204. }
  205. if *rm {
  206. v.Set("rm", "1")
  207. }
  208. req, err := http.NewRequest("POST", fmt.Sprintf("/v%g/build?%s", APIVERSION, v.Encode()), body)
  209. if err != nil {
  210. return err
  211. }
  212. if context != nil {
  213. req.Header.Set("Content-Type", "application/tar")
  214. }
  215. dial, err := net.Dial(cli.proto, cli.addr)
  216. if err != nil {
  217. return err
  218. }
  219. clientconn := httputil.NewClientConn(dial, nil)
  220. resp, err := clientconn.Do(req)
  221. defer clientconn.Close()
  222. if err != nil {
  223. return err
  224. }
  225. defer resp.Body.Close()
  226. // Check for errors
  227. if resp.StatusCode < 200 || resp.StatusCode >= 400 {
  228. body, err := ioutil.ReadAll(resp.Body)
  229. if err != nil {
  230. return err
  231. }
  232. if len(body) == 0 {
  233. return fmt.Errorf("Error: %s", http.StatusText(resp.StatusCode))
  234. }
  235. return fmt.Errorf("Error: %s", body)
  236. }
  237. // Output the result
  238. if _, err := io.Copy(cli.out, resp.Body); err != nil {
  239. return err
  240. }
  241. return nil
  242. }
  243. // 'docker login': login / register a user to registry service.
  244. func (cli *DockerCli) CmdLogin(args ...string) error {
  245. cmd := Subcmd("login", "[OPTIONS] [SERVER]", "Register or Login to a docker registry server, if no server is specified \""+auth.IndexServerAddress()+"\" is the default.")
  246. var username, password, email string
  247. cmd.StringVar(&username, "u", "", "username")
  248. cmd.StringVar(&password, "p", "", "password")
  249. cmd.StringVar(&email, "e", "", "email")
  250. err := cmd.Parse(args)
  251. if err != nil {
  252. return nil
  253. }
  254. serverAddress := auth.IndexServerAddress()
  255. if len(cmd.Args()) > 0 {
  256. serverAddress, err = registry.ExpandAndVerifyRegistryUrl(cmd.Arg(0))
  257. if err != nil {
  258. return err
  259. }
  260. fmt.Fprintf(cli.out, "Login against server at %s\n", serverAddress)
  261. }
  262. promptDefault := func(prompt string, configDefault string) {
  263. if configDefault == "" {
  264. fmt.Fprintf(cli.out, "%s: ", prompt)
  265. } else {
  266. fmt.Fprintf(cli.out, "%s (%s): ", prompt, configDefault)
  267. }
  268. }
  269. readInput := func(in io.Reader, out io.Writer) string {
  270. reader := bufio.NewReader(in)
  271. line, _, err := reader.ReadLine()
  272. if err != nil {
  273. fmt.Fprintln(out, err.Error())
  274. os.Exit(1)
  275. }
  276. return string(line)
  277. }
  278. cli.LoadConfigFile()
  279. authconfig, ok := cli.configFile.Configs[auth.IndexServerAddress()]
  280. if !ok {
  281. authconfig = auth.AuthConfig{}
  282. }
  283. if username == "" {
  284. promptDefault("Username", authconfig.Username)
  285. username = readInput(cli.in, cli.out)
  286. if username == "" {
  287. username = authconfig.Username
  288. }
  289. }
  290. if username != authconfig.Username {
  291. if password == "" {
  292. oldState, _ := term.SaveState(cli.terminalFd)
  293. fmt.Fprintf(cli.out, "Password: ")
  294. term.DisableEcho(cli.terminalFd, oldState)
  295. password = readInput(cli.in, cli.out)
  296. fmt.Fprint(cli.out, "\n")
  297. term.RestoreTerminal(cli.terminalFd, oldState)
  298. if password == "" {
  299. return fmt.Errorf("Error : Password Required")
  300. }
  301. }
  302. if email == "" {
  303. promptDefault("Email", authconfig.Email)
  304. email = readInput(cli.in, cli.out)
  305. if email == "" {
  306. email = authconfig.Email
  307. }
  308. }
  309. } else {
  310. password = authconfig.Password
  311. email = authconfig.Email
  312. }
  313. authconfig.Username = username
  314. authconfig.Password = password
  315. authconfig.Email = email
  316. authconfig.ServerAddress = serverAddress
  317. cli.configFile.Configs[serverAddress] = authconfig
  318. body, statusCode, err := cli.call("POST", "/auth", cli.configFile.Configs[serverAddress])
  319. if statusCode == 401 {
  320. delete(cli.configFile.Configs, serverAddress)
  321. auth.SaveConfig(cli.configFile)
  322. return err
  323. }
  324. if err != nil {
  325. return err
  326. }
  327. var out2 APIAuth
  328. err = json.Unmarshal(body, &out2)
  329. if err != nil {
  330. cli.configFile, _ = auth.LoadConfig(os.Getenv("HOME"))
  331. return err
  332. }
  333. auth.SaveConfig(cli.configFile)
  334. if out2.Status != "" {
  335. fmt.Fprintf(cli.out, "%s\n", out2.Status)
  336. }
  337. return nil
  338. }
  339. // 'docker wait': block until a container stops
  340. func (cli *DockerCli) CmdWait(args ...string) error {
  341. cmd := Subcmd("wait", "CONTAINER [CONTAINER...]", "Block until a container stops, then print its exit code.")
  342. if err := cmd.Parse(args); err != nil {
  343. return nil
  344. }
  345. if cmd.NArg() < 1 {
  346. cmd.Usage()
  347. return nil
  348. }
  349. for _, name := range cmd.Args() {
  350. status, err := waitForExit(cli, name)
  351. if err != nil {
  352. fmt.Fprintf(cli.err, "%s", err)
  353. } else {
  354. fmt.Fprintf(cli.out, "%d\n", status)
  355. }
  356. }
  357. return nil
  358. }
  359. // 'docker version': show version information
  360. func (cli *DockerCli) CmdVersion(args ...string) error {
  361. cmd := Subcmd("version", "", "Show the docker version information.")
  362. if err := cmd.Parse(args); err != nil {
  363. return nil
  364. }
  365. if cmd.NArg() > 0 {
  366. cmd.Usage()
  367. return nil
  368. }
  369. if VERSION != "" {
  370. fmt.Fprintf(cli.out, "Client version: %s\n", VERSION)
  371. }
  372. fmt.Fprintf(cli.out, "Go version (client): %s\n", runtime.Version())
  373. if GITCOMMIT != "" {
  374. fmt.Fprintf(cli.out, "Git commit (client): %s\n", GITCOMMIT)
  375. }
  376. body, _, err := cli.call("GET", "/version", nil)
  377. if err != nil {
  378. return err
  379. }
  380. var out APIVersion
  381. err = json.Unmarshal(body, &out)
  382. if err != nil {
  383. utils.Debugf("Error unmarshal: body: %s, err: %s\n", body, err)
  384. return err
  385. }
  386. if out.Version != "" {
  387. fmt.Fprintf(cli.out, "Server version: %s\n", out.Version)
  388. }
  389. if out.GitCommit != "" {
  390. fmt.Fprintf(cli.out, "Git commit (server): %s\n", out.GitCommit)
  391. }
  392. if out.GoVersion != "" {
  393. fmt.Fprintf(cli.out, "Go version (server): %s\n", out.GoVersion)
  394. }
  395. release := utils.GetReleaseVersion()
  396. if release != "" {
  397. fmt.Fprintf(cli.out, "Last stable version: %s", release)
  398. if (VERSION != "" || out.Version != "") && (strings.Trim(VERSION, "-dev") != release || strings.Trim(out.Version, "-dev") != release) {
  399. fmt.Fprintf(cli.out, ", please update docker")
  400. }
  401. fmt.Fprintf(cli.out, "\n")
  402. }
  403. return nil
  404. }
  405. // 'docker info': display system-wide information.
  406. func (cli *DockerCli) CmdInfo(args ...string) error {
  407. cmd := Subcmd("info", "", "Display system-wide information")
  408. if err := cmd.Parse(args); err != nil {
  409. return nil
  410. }
  411. if cmd.NArg() > 0 {
  412. cmd.Usage()
  413. return nil
  414. }
  415. body, _, err := cli.call("GET", "/info", nil)
  416. if err != nil {
  417. return err
  418. }
  419. var out APIInfo
  420. if err := json.Unmarshal(body, &out); err != nil {
  421. return err
  422. }
  423. fmt.Fprintf(cli.out, "Containers: %d\n", out.Containers)
  424. fmt.Fprintf(cli.out, "Images: %d\n", out.Images)
  425. if out.Debug || os.Getenv("DEBUG") != "" {
  426. fmt.Fprintf(cli.out, "Debug mode (server): %v\n", out.Debug)
  427. fmt.Fprintf(cli.out, "Debug mode (client): %v\n", os.Getenv("DEBUG") != "")
  428. fmt.Fprintf(cli.out, "Fds: %d\n", out.NFd)
  429. fmt.Fprintf(cli.out, "Goroutines: %d\n", out.NGoroutines)
  430. fmt.Fprintf(cli.out, "LXC Version: %s\n", out.LXCVersion)
  431. fmt.Fprintf(cli.out, "EventsListeners: %d\n", out.NEventsListener)
  432. fmt.Fprintf(cli.out, "Kernel Version: %s\n", out.KernelVersion)
  433. }
  434. if len(out.IndexServerAddress) != 0 {
  435. cli.LoadConfigFile()
  436. u := cli.configFile.Configs[out.IndexServerAddress].Username
  437. if len(u) > 0 {
  438. fmt.Fprintf(cli.out, "Username: %v\n", u)
  439. fmt.Fprintf(cli.out, "Registry: %v\n", out.IndexServerAddress)
  440. }
  441. }
  442. if !out.MemoryLimit {
  443. fmt.Fprintf(cli.err, "WARNING: No memory limit support\n")
  444. }
  445. if !out.SwapLimit {
  446. fmt.Fprintf(cli.err, "WARNING: No swap limit support\n")
  447. }
  448. if !out.IPv4Forwarding {
  449. fmt.Fprintf(cli.err, "WARNING: IPv4 forwarding is disabled.\n")
  450. }
  451. return nil
  452. }
  453. func (cli *DockerCli) CmdStop(args ...string) error {
  454. cmd := Subcmd("stop", "[OPTIONS] CONTAINER [CONTAINER...]", "Stop a running container")
  455. nSeconds := cmd.Int("t", 10, "Number of seconds to wait for the container to stop before killing it.")
  456. if err := cmd.Parse(args); err != nil {
  457. return nil
  458. }
  459. if cmd.NArg() < 1 {
  460. cmd.Usage()
  461. return nil
  462. }
  463. v := url.Values{}
  464. v.Set("t", strconv.Itoa(*nSeconds))
  465. for _, name := range cmd.Args() {
  466. _, _, err := cli.call("POST", "/containers/"+name+"/stop?"+v.Encode(), nil)
  467. if err != nil {
  468. fmt.Fprintf(cli.err, "%s\n", err)
  469. } else {
  470. fmt.Fprintf(cli.out, "%s\n", name)
  471. }
  472. }
  473. return nil
  474. }
  475. func (cli *DockerCli) CmdRestart(args ...string) error {
  476. cmd := Subcmd("restart", "[OPTIONS] CONTAINER [CONTAINER...]", "Restart a running container")
  477. nSeconds := cmd.Int("t", 10, "Number of seconds to try to stop for before killing the container. Once killed it will then be restarted. Default=10")
  478. if err := cmd.Parse(args); err != nil {
  479. return nil
  480. }
  481. if cmd.NArg() < 1 {
  482. cmd.Usage()
  483. return nil
  484. }
  485. v := url.Values{}
  486. v.Set("t", strconv.Itoa(*nSeconds))
  487. for _, name := range cmd.Args() {
  488. _, _, err := cli.call("POST", "/containers/"+name+"/restart?"+v.Encode(), nil)
  489. if err != nil {
  490. fmt.Fprintf(cli.err, "%s\n", err)
  491. } else {
  492. fmt.Fprintf(cli.out, "%s\n", name)
  493. }
  494. }
  495. return nil
  496. }
  497. func (cli *DockerCli) CmdStart(args ...string) error {
  498. cmd := Subcmd("start", "CONTAINER [CONTAINER...]", "Restart a stopped container")
  499. if err := cmd.Parse(args); err != nil {
  500. return nil
  501. }
  502. if cmd.NArg() < 1 {
  503. cmd.Usage()
  504. return nil
  505. }
  506. var encounteredError error
  507. for _, name := range args {
  508. _, _, err := cli.call("POST", "/containers/"+name+"/start", nil)
  509. if err != nil {
  510. fmt.Fprintf(cli.err, "%s\n", err)
  511. encounteredError = fmt.Errorf("Error: failed to start one or more containers")
  512. } else {
  513. fmt.Fprintf(cli.out, "%s\n", name)
  514. }
  515. }
  516. return encounteredError
  517. }
  518. func (cli *DockerCli) CmdInspect(args ...string) error {
  519. cmd := Subcmd("inspect", "CONTAINER|IMAGE [CONTAINER|IMAGE...]", "Return low-level information on a container/image")
  520. if err := cmd.Parse(args); err != nil {
  521. return nil
  522. }
  523. if cmd.NArg() < 1 {
  524. cmd.Usage()
  525. return nil
  526. }
  527. fmt.Fprintf(cli.out, "[")
  528. for i, name := range args {
  529. if i > 0 {
  530. fmt.Fprintf(cli.out, ",")
  531. }
  532. obj, _, err := cli.call("GET", "/containers/"+name+"/json", nil)
  533. if err != nil {
  534. obj, _, err = cli.call("GET", "/images/"+name+"/json", nil)
  535. if err != nil {
  536. fmt.Fprintf(cli.err, "No such image or container: %s\n", name)
  537. continue
  538. }
  539. }
  540. indented := new(bytes.Buffer)
  541. if err = json.Indent(indented, obj, "", " "); err != nil {
  542. fmt.Fprintf(cli.err, "%s\n", err)
  543. continue
  544. }
  545. if _, err := io.Copy(cli.out, indented); err != nil {
  546. fmt.Fprintf(cli.err, "%s\n", err)
  547. }
  548. }
  549. fmt.Fprintf(cli.out, "]")
  550. return nil
  551. }
  552. func (cli *DockerCli) CmdTop(args ...string) error {
  553. cmd := Subcmd("top", "CONTAINER", "Lookup the running processes of a container")
  554. if err := cmd.Parse(args); err != nil {
  555. return nil
  556. }
  557. if cmd.NArg() == 0 {
  558. cmd.Usage()
  559. return nil
  560. }
  561. val := url.Values{}
  562. if cmd.NArg() > 1 {
  563. val.Set("ps_args", strings.Join(cmd.Args()[1:], " "))
  564. }
  565. body, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/top?"+val.Encode(), nil)
  566. if err != nil {
  567. return err
  568. }
  569. procs := APITop{}
  570. err = json.Unmarshal(body, &procs)
  571. if err != nil {
  572. return err
  573. }
  574. w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
  575. fmt.Fprintln(w, strings.Join(procs.Titles, "\t"))
  576. for _, proc := range procs.Processes {
  577. fmt.Fprintln(w, strings.Join(proc, "\t"))
  578. }
  579. w.Flush()
  580. return nil
  581. }
  582. func (cli *DockerCli) CmdPort(args ...string) error {
  583. cmd := Subcmd("port", "CONTAINER PRIVATE_PORT", "Lookup the public-facing port which is NAT-ed to PRIVATE_PORT")
  584. if err := cmd.Parse(args); err != nil {
  585. return nil
  586. }
  587. if cmd.NArg() != 2 {
  588. cmd.Usage()
  589. return nil
  590. }
  591. port := cmd.Arg(1)
  592. proto := "Tcp"
  593. parts := strings.SplitN(port, "/", 2)
  594. if len(parts) == 2 && len(parts[1]) != 0 {
  595. port = parts[0]
  596. proto = strings.ToUpper(parts[1][:1]) + strings.ToLower(parts[1][1:])
  597. }
  598. body, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/json", nil)
  599. if err != nil {
  600. return err
  601. }
  602. var out Container
  603. err = json.Unmarshal(body, &out)
  604. if err != nil {
  605. return err
  606. }
  607. if frontend, exists := out.NetworkSettings.PortMapping[proto][port]; exists {
  608. fmt.Fprintf(cli.out, "%s\n", frontend)
  609. } else {
  610. return fmt.Errorf("Error: No private port '%s' allocated on %s", cmd.Arg(1), cmd.Arg(0))
  611. }
  612. return nil
  613. }
  614. // 'docker rmi IMAGE' removes all images with the name IMAGE
  615. func (cli *DockerCli) CmdRmi(args ...string) error {
  616. cmd := Subcmd("rmi", "IMAGE [IMAGE...]", "Remove one or more images")
  617. if err := cmd.Parse(args); err != nil {
  618. return nil
  619. }
  620. if cmd.NArg() < 1 {
  621. cmd.Usage()
  622. return nil
  623. }
  624. for _, name := range cmd.Args() {
  625. body, _, err := cli.call("DELETE", "/images/"+name, nil)
  626. if err != nil {
  627. fmt.Fprintf(cli.err, "%s", err)
  628. } else {
  629. var outs []APIRmi
  630. err = json.Unmarshal(body, &outs)
  631. if err != nil {
  632. return err
  633. }
  634. for _, out := range outs {
  635. if out.Deleted != "" {
  636. fmt.Fprintf(cli.out, "Deleted: %s\n", out.Deleted)
  637. } else {
  638. fmt.Fprintf(cli.out, "Untagged: %s\n", out.Untagged)
  639. }
  640. }
  641. }
  642. }
  643. return nil
  644. }
  645. func (cli *DockerCli) CmdHistory(args ...string) error {
  646. cmd := Subcmd("history", "IMAGE", "Show the history of an image")
  647. if err := cmd.Parse(args); err != nil {
  648. return nil
  649. }
  650. if cmd.NArg() != 1 {
  651. cmd.Usage()
  652. return nil
  653. }
  654. body, _, err := cli.call("GET", "/images/"+cmd.Arg(0)+"/history", nil)
  655. if err != nil {
  656. return err
  657. }
  658. var outs []APIHistory
  659. err = json.Unmarshal(body, &outs)
  660. if err != nil {
  661. return err
  662. }
  663. w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
  664. fmt.Fprintln(w, "ID\tCREATED\tCREATED BY")
  665. for _, out := range outs {
  666. if out.Tags != nil {
  667. out.ID = out.Tags[0]
  668. }
  669. fmt.Fprintf(w, "%s \t%s ago\t%s\n", out.ID, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.CreatedBy)
  670. }
  671. w.Flush()
  672. return nil
  673. }
  674. func (cli *DockerCli) CmdRm(args ...string) error {
  675. cmd := Subcmd("rm", "[OPTIONS] CONTAINER [CONTAINER...]", "Remove one or more containers")
  676. v := cmd.Bool("v", false, "Remove the volumes associated to the container")
  677. if err := cmd.Parse(args); err != nil {
  678. return nil
  679. }
  680. if cmd.NArg() < 1 {
  681. cmd.Usage()
  682. return nil
  683. }
  684. val := url.Values{}
  685. if *v {
  686. val.Set("v", "1")
  687. }
  688. for _, name := range cmd.Args() {
  689. _, _, err := cli.call("DELETE", "/containers/"+name+"?"+val.Encode(), nil)
  690. if err != nil {
  691. fmt.Fprintf(cli.err, "%s\n", err)
  692. } else {
  693. fmt.Fprintf(cli.out, "%s\n", name)
  694. }
  695. }
  696. return nil
  697. }
  698. // 'docker kill NAME' kills a running container
  699. func (cli *DockerCli) CmdKill(args ...string) error {
  700. cmd := Subcmd("kill", "CONTAINER [CONTAINER...]", "Kill a running container")
  701. if err := cmd.Parse(args); err != nil {
  702. return nil
  703. }
  704. if cmd.NArg() < 1 {
  705. cmd.Usage()
  706. return nil
  707. }
  708. for _, name := range args {
  709. _, _, err := cli.call("POST", "/containers/"+name+"/kill", nil)
  710. if err != nil {
  711. fmt.Fprintf(cli.err, "%s\n", err)
  712. } else {
  713. fmt.Fprintf(cli.out, "%s\n", name)
  714. }
  715. }
  716. return nil
  717. }
  718. func (cli *DockerCli) CmdImport(args ...string) error {
  719. cmd := Subcmd("import", "URL|- [REPOSITORY [TAG]]", "Create a new filesystem image from the contents of a tarball(.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz).")
  720. if err := cmd.Parse(args); err != nil {
  721. return nil
  722. }
  723. if cmd.NArg() < 1 {
  724. cmd.Usage()
  725. return nil
  726. }
  727. src, repository, tag := cmd.Arg(0), cmd.Arg(1), cmd.Arg(2)
  728. v := url.Values{}
  729. v.Set("repo", repository)
  730. v.Set("tag", tag)
  731. v.Set("fromSrc", src)
  732. err := cli.stream("POST", "/images/create?"+v.Encode(), cli.in, cli.out, nil)
  733. if err != nil {
  734. return err
  735. }
  736. return nil
  737. }
  738. func (cli *DockerCli) CmdPush(args ...string) error {
  739. cmd := Subcmd("push", "NAME", "Push an image or a repository to the registry")
  740. if err := cmd.Parse(args); err != nil {
  741. return nil
  742. }
  743. name := cmd.Arg(0)
  744. if name == "" {
  745. cmd.Usage()
  746. return nil
  747. }
  748. cli.LoadConfigFile()
  749. // Resolve the Repository name from fqn to endpoint + name
  750. endpoint, _, err := registry.ResolveRepositoryName(name)
  751. if err != nil {
  752. return err
  753. }
  754. // Resolve the Auth config relevant for this server
  755. authConfig := cli.configFile.ResolveAuthConfig(endpoint)
  756. // If we're not using a custom registry, we know the restrictions
  757. // applied to repository names and can warn the user in advance.
  758. // Custom repositories can have different rules, and we must also
  759. // allow pushing by image ID.
  760. if len(strings.SplitN(name, "/", 2)) == 1 {
  761. username := cli.configFile.Configs[auth.IndexServerAddress()].Username
  762. if username == "" {
  763. username = "<user>"
  764. }
  765. return fmt.Errorf("Impossible to push a \"root\" repository. Please rename your repository in <user>/<repo> (ex: %s/%s)", username, name)
  766. }
  767. v := url.Values{}
  768. push := func(authConfig auth.AuthConfig) error {
  769. buf, err := json.Marshal(authConfig)
  770. if err != nil {
  771. return err
  772. }
  773. registryAuthHeader := []string{
  774. base64.URLEncoding.EncodeToString(buf),
  775. }
  776. return cli.stream("POST", "/images/"+name+"/push?"+v.Encode(), nil, cli.out, map[string][]string{
  777. "X-Registry-Auth": registryAuthHeader,
  778. })
  779. }
  780. if err := push(authConfig); err != nil {
  781. if err.Error() == registry.ErrLoginRequired.Error() {
  782. fmt.Fprintln(cli.out, "\nPlease login prior to push:")
  783. if err := cli.CmdLogin(endpoint); err != nil {
  784. return err
  785. }
  786. authConfig := cli.configFile.ResolveAuthConfig(endpoint)
  787. return push(authConfig)
  788. }
  789. return err
  790. }
  791. return nil
  792. }
  793. func (cli *DockerCli) CmdPull(args ...string) error {
  794. cmd := Subcmd("pull", "NAME", "Pull an image or a repository from the registry")
  795. tag := cmd.String("t", "", "Download tagged image in repository")
  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. remote, parsedTag := utils.ParseRepositoryTag(cmd.Arg(0))
  804. if *tag == "" {
  805. *tag = parsedTag
  806. }
  807. // Resolve the Repository name from fqn to endpoint + name
  808. endpoint, _, err := registry.ResolveRepositoryName(remote)
  809. if err != nil {
  810. return err
  811. }
  812. cli.LoadConfigFile()
  813. // Resolve the Auth config relevant for this server
  814. authConfig := cli.configFile.ResolveAuthConfig(endpoint)
  815. v := url.Values{}
  816. v.Set("fromImage", remote)
  817. v.Set("tag", *tag)
  818. pull := func(authConfig auth.AuthConfig) error {
  819. buf, err := json.Marshal(authConfig)
  820. if err != nil {
  821. return err
  822. }
  823. registryAuthHeader := []string{
  824. base64.URLEncoding.EncodeToString(buf),
  825. }
  826. return cli.stream("POST", "/images/create?"+v.Encode(), nil, cli.out, map[string][]string{
  827. "X-Registry-Auth": registryAuthHeader,
  828. })
  829. }
  830. if err := pull(authConfig); err != nil {
  831. if err.Error() == registry.ErrLoginRequired.Error() {
  832. fmt.Fprintln(cli.out, "\nPlease login prior to push:")
  833. if err := cli.CmdLogin(endpoint); err != nil {
  834. return err
  835. }
  836. authConfig := cli.configFile.ResolveAuthConfig(endpoint)
  837. return pull(authConfig)
  838. }
  839. return err
  840. }
  841. return nil
  842. }
  843. func (cli *DockerCli) CmdImages(args ...string) error {
  844. cmd := Subcmd("images", "[OPTIONS] [NAME]", "List images")
  845. quiet := cmd.Bool("q", false, "only show numeric IDs")
  846. all := cmd.Bool("a", false, "show all images")
  847. noTrunc := cmd.Bool("notrunc", false, "Don't truncate output")
  848. flViz := cmd.Bool("viz", false, "output graph in graphviz format")
  849. if err := cmd.Parse(args); err != nil {
  850. return nil
  851. }
  852. if cmd.NArg() > 1 {
  853. cmd.Usage()
  854. return nil
  855. }
  856. if *flViz {
  857. body, _, err := cli.call("GET", "/images/viz", false)
  858. if err != nil {
  859. return err
  860. }
  861. fmt.Fprintf(cli.out, "%s", body)
  862. } else {
  863. v := url.Values{}
  864. if cmd.NArg() == 1 {
  865. v.Set("filter", cmd.Arg(0))
  866. }
  867. if *all {
  868. v.Set("all", "1")
  869. }
  870. body, _, err := cli.call("GET", "/images/json?"+v.Encode(), nil)
  871. if err != nil {
  872. return err
  873. }
  874. var outs []APIImages
  875. err = json.Unmarshal(body, &outs)
  876. if err != nil {
  877. return err
  878. }
  879. w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
  880. if !*quiet {
  881. fmt.Fprintln(w, "REPOSITORY\tTAG\tID\tCREATED\tSIZE")
  882. }
  883. for _, out := range outs {
  884. if out.Repository == "" {
  885. out.Repository = "<none>"
  886. }
  887. if out.Tag == "" {
  888. out.Tag = "<none>"
  889. }
  890. if !*quiet {
  891. fmt.Fprintf(w, "%s\t%s\t", out.Repository, out.Tag)
  892. if *noTrunc {
  893. fmt.Fprintf(w, "%s\t", out.ID)
  894. } else {
  895. fmt.Fprintf(w, "%s\t", utils.TruncateID(out.ID))
  896. }
  897. fmt.Fprintf(w, "%s ago\t", utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))))
  898. if out.VirtualSize > 0 {
  899. fmt.Fprintf(w, "%s (virtual %s)\n", utils.HumanSize(out.Size), utils.HumanSize(out.VirtualSize))
  900. } else {
  901. fmt.Fprintf(w, "%s\n", utils.HumanSize(out.Size))
  902. }
  903. } else {
  904. if *noTrunc {
  905. fmt.Fprintln(w, out.ID)
  906. } else {
  907. fmt.Fprintln(w, utils.TruncateID(out.ID))
  908. }
  909. }
  910. }
  911. if !*quiet {
  912. w.Flush()
  913. }
  914. }
  915. return nil
  916. }
  917. func displayablePorts(ports []APIPort) string {
  918. result := []string{}
  919. for _, port := range ports {
  920. if port.Type == "tcp" {
  921. result = append(result, fmt.Sprintf("%d->%d", port.PublicPort, port.PrivatePort))
  922. } else {
  923. result = append(result, fmt.Sprintf("%d->%d/%s", port.PublicPort, port.PrivatePort, port.Type))
  924. }
  925. }
  926. sort.Strings(result)
  927. return strings.Join(result, ", ")
  928. }
  929. func (cli *DockerCli) CmdPs(args ...string) error {
  930. cmd := Subcmd("ps", "[OPTIONS]", "List containers")
  931. quiet := cmd.Bool("q", false, "Only display numeric IDs")
  932. size := cmd.Bool("s", false, "Display sizes")
  933. all := cmd.Bool("a", false, "Show all containers. Only running containers are shown by default.")
  934. noTrunc := cmd.Bool("notrunc", false, "Don't truncate output")
  935. nLatest := cmd.Bool("l", false, "Show only the latest created container, include non-running ones.")
  936. since := cmd.String("sinceId", "", "Show only containers created since Id, include non-running ones.")
  937. before := cmd.String("beforeId", "", "Show only container created before Id, include non-running ones.")
  938. last := cmd.Int("n", -1, "Show n last created containers, include non-running ones.")
  939. if err := cmd.Parse(args); err != nil {
  940. return nil
  941. }
  942. v := url.Values{}
  943. if *last == -1 && *nLatest {
  944. *last = 1
  945. }
  946. if *all {
  947. v.Set("all", "1")
  948. }
  949. if *last != -1 {
  950. v.Set("limit", strconv.Itoa(*last))
  951. }
  952. if *since != "" {
  953. v.Set("since", *since)
  954. }
  955. if *before != "" {
  956. v.Set("before", *before)
  957. }
  958. if *size {
  959. v.Set("size", "1")
  960. }
  961. body, _, err := cli.call("GET", "/containers/json?"+v.Encode(), nil)
  962. if err != nil {
  963. return err
  964. }
  965. var outs []APIContainers
  966. err = json.Unmarshal(body, &outs)
  967. if err != nil {
  968. return err
  969. }
  970. w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
  971. if !*quiet {
  972. fmt.Fprint(w, "ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS")
  973. if *size {
  974. fmt.Fprintln(w, "\tSIZE")
  975. } else {
  976. fmt.Fprint(w, "\n")
  977. }
  978. }
  979. for _, out := range outs {
  980. if !*quiet {
  981. if *noTrunc {
  982. fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t", out.ID, out.Image, out.Command, utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Status, displayablePorts(out.Ports))
  983. } else {
  984. fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t", utils.TruncateID(out.ID), out.Image, utils.Trunc(out.Command, 20), utils.HumanDuration(time.Now().Sub(time.Unix(out.Created, 0))), out.Status, displayablePorts(out.Ports))
  985. }
  986. if *size {
  987. if out.SizeRootFs > 0 {
  988. fmt.Fprintf(w, "%s (virtual %s)\n", utils.HumanSize(out.SizeRw), utils.HumanSize(out.SizeRootFs))
  989. } else {
  990. fmt.Fprintf(w, "%s\n", utils.HumanSize(out.SizeRw))
  991. }
  992. } else {
  993. fmt.Fprint(w, "\n")
  994. }
  995. } else {
  996. if *noTrunc {
  997. fmt.Fprintln(w, out.ID)
  998. } else {
  999. fmt.Fprintln(w, utils.TruncateID(out.ID))
  1000. }
  1001. }
  1002. }
  1003. if !*quiet {
  1004. w.Flush()
  1005. }
  1006. return nil
  1007. }
  1008. func (cli *DockerCli) CmdCommit(args ...string) error {
  1009. cmd := Subcmd("commit", "[OPTIONS] CONTAINER [REPOSITORY [TAG]]", "Create a new image from a container's changes")
  1010. flComment := cmd.String("m", "", "Commit message")
  1011. flAuthor := cmd.String("author", "", "Author (eg. \"John Hannibal Smith <hannibal@a-team.com>\"")
  1012. flConfig := cmd.String("run", "", "Config automatically applied when the image is run. "+`(ex: {"Cmd": ["cat", "/world"], "PortSpecs": ["22"]}')`)
  1013. if err := cmd.Parse(args); err != nil {
  1014. return nil
  1015. }
  1016. name, repository, tag := cmd.Arg(0), cmd.Arg(1), cmd.Arg(2)
  1017. if name == "" {
  1018. cmd.Usage()
  1019. return nil
  1020. }
  1021. v := url.Values{}
  1022. v.Set("container", name)
  1023. v.Set("repo", repository)
  1024. v.Set("tag", tag)
  1025. v.Set("comment", *flComment)
  1026. v.Set("author", *flAuthor)
  1027. var config *Config
  1028. if *flConfig != "" {
  1029. config = &Config{}
  1030. if err := json.Unmarshal([]byte(*flConfig), config); err != nil {
  1031. return err
  1032. }
  1033. }
  1034. body, _, err := cli.call("POST", "/commit?"+v.Encode(), config)
  1035. if err != nil {
  1036. return err
  1037. }
  1038. apiID := &APIID{}
  1039. err = json.Unmarshal(body, apiID)
  1040. if err != nil {
  1041. return err
  1042. }
  1043. fmt.Fprintf(cli.out, "%s\n", apiID.ID)
  1044. return nil
  1045. }
  1046. func (cli *DockerCli) CmdEvents(args ...string) error {
  1047. cmd := Subcmd("events", "[OPTIONS]", "Get real time events from the server")
  1048. since := cmd.String("since", "", "Show events previously created (used for polling).")
  1049. if err := cmd.Parse(args); err != nil {
  1050. return nil
  1051. }
  1052. if cmd.NArg() != 0 {
  1053. cmd.Usage()
  1054. return nil
  1055. }
  1056. v := url.Values{}
  1057. if *since != "" {
  1058. v.Set("since", *since)
  1059. }
  1060. if err := cli.stream("GET", "/events?"+v.Encode(), nil, cli.out, nil); err != nil {
  1061. return err
  1062. }
  1063. return nil
  1064. }
  1065. func (cli *DockerCli) CmdExport(args ...string) error {
  1066. cmd := Subcmd("export", "CONTAINER", "Export the contents of a filesystem as a tar archive")
  1067. if err := cmd.Parse(args); err != nil {
  1068. return nil
  1069. }
  1070. if cmd.NArg() != 1 {
  1071. cmd.Usage()
  1072. return nil
  1073. }
  1074. if err := cli.stream("GET", "/containers/"+cmd.Arg(0)+"/export", nil, cli.out, nil); err != nil {
  1075. return err
  1076. }
  1077. return nil
  1078. }
  1079. func (cli *DockerCli) CmdDiff(args ...string) error {
  1080. cmd := Subcmd("diff", "CONTAINER", "Inspect changes on a container's filesystem")
  1081. if err := cmd.Parse(args); err != nil {
  1082. return nil
  1083. }
  1084. if cmd.NArg() != 1 {
  1085. cmd.Usage()
  1086. return nil
  1087. }
  1088. body, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/changes", nil)
  1089. if err != nil {
  1090. return err
  1091. }
  1092. changes := []Change{}
  1093. err = json.Unmarshal(body, &changes)
  1094. if err != nil {
  1095. return err
  1096. }
  1097. for _, change := range changes {
  1098. fmt.Fprintf(cli.out, "%s\n", change.String())
  1099. }
  1100. return nil
  1101. }
  1102. func (cli *DockerCli) CmdLogs(args ...string) error {
  1103. cmd := Subcmd("logs", "CONTAINER", "Fetch the logs of a container")
  1104. if err := cmd.Parse(args); err != nil {
  1105. return nil
  1106. }
  1107. if cmd.NArg() != 1 {
  1108. cmd.Usage()
  1109. return nil
  1110. }
  1111. if err := cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?logs=1&stdout=1&stderr=1", false, nil, cli.out); err != nil {
  1112. return err
  1113. }
  1114. return nil
  1115. }
  1116. func (cli *DockerCli) CmdAttach(args ...string) error {
  1117. cmd := Subcmd("attach", "CONTAINER", "Attach to a running container")
  1118. if err := cmd.Parse(args); err != nil {
  1119. return nil
  1120. }
  1121. if cmd.NArg() != 1 {
  1122. cmd.Usage()
  1123. return nil
  1124. }
  1125. body, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/json", nil)
  1126. if err != nil {
  1127. return err
  1128. }
  1129. container := &Container{}
  1130. err = json.Unmarshal(body, container)
  1131. if err != nil {
  1132. return err
  1133. }
  1134. if !container.State.Running {
  1135. return fmt.Errorf("Impossible to attach to a stopped container, start it first")
  1136. }
  1137. if container.Config.Tty {
  1138. if err := cli.monitorTtySize(cmd.Arg(0)); err != nil {
  1139. return err
  1140. }
  1141. }
  1142. v := url.Values{}
  1143. v.Set("stream", "1")
  1144. v.Set("stdin", "1")
  1145. v.Set("stdout", "1")
  1146. v.Set("stderr", "1")
  1147. if err := cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), container.Config.Tty, cli.in, cli.out); err != nil {
  1148. return err
  1149. }
  1150. return nil
  1151. }
  1152. func (cli *DockerCli) CmdSearch(args ...string) error {
  1153. cmd := Subcmd("search", "NAME", "Search the docker index for images")
  1154. noTrunc := cmd.Bool("notrunc", false, "Don't truncate output")
  1155. if err := cmd.Parse(args); err != nil {
  1156. return nil
  1157. }
  1158. if cmd.NArg() != 1 {
  1159. cmd.Usage()
  1160. return nil
  1161. }
  1162. v := url.Values{}
  1163. v.Set("term", cmd.Arg(0))
  1164. body, _, err := cli.call("GET", "/images/search?"+v.Encode(), nil)
  1165. if err != nil {
  1166. return err
  1167. }
  1168. outs := []APISearch{}
  1169. err = json.Unmarshal(body, &outs)
  1170. if err != nil {
  1171. return err
  1172. }
  1173. fmt.Fprintf(cli.out, "Found %d results matching your query (\"%s\")\n", len(outs), cmd.Arg(0))
  1174. w := tabwriter.NewWriter(cli.out, 33, 1, 3, ' ', 0)
  1175. fmt.Fprintf(w, "NAME\tDESCRIPTION\n")
  1176. _, width := cli.getTtySize()
  1177. if width == 0 {
  1178. width = 45
  1179. } else {
  1180. width = width - 33 //remote the first column
  1181. }
  1182. for _, out := range outs {
  1183. desc := strings.Replace(out.Description, "\n", " ", -1)
  1184. desc = strings.Replace(desc, "\r", " ", -1)
  1185. if !*noTrunc && len(desc) > width {
  1186. desc = utils.Trunc(desc, width-3) + "..."
  1187. }
  1188. fmt.Fprintf(w, "%s\t%s\n", out.Name, desc)
  1189. }
  1190. w.Flush()
  1191. return nil
  1192. }
  1193. // Ports type - Used to parse multiple -p flags
  1194. type ports []int
  1195. // ListOpts type
  1196. type ListOpts []string
  1197. func (opts *ListOpts) String() string {
  1198. return fmt.Sprint(*opts)
  1199. }
  1200. func (opts *ListOpts) Set(value string) error {
  1201. *opts = append(*opts, value)
  1202. return nil
  1203. }
  1204. // AttachOpts stores arguments to 'docker run -a', eg. which streams to attach to
  1205. type AttachOpts map[string]bool
  1206. func NewAttachOpts() AttachOpts {
  1207. return make(AttachOpts)
  1208. }
  1209. func (opts AttachOpts) String() string {
  1210. // Cast to underlying map type to avoid infinite recursion
  1211. return fmt.Sprintf("%v", map[string]bool(opts))
  1212. }
  1213. func (opts AttachOpts) Set(val string) error {
  1214. if val != "stdin" && val != "stdout" && val != "stderr" {
  1215. return fmt.Errorf("Unsupported stream name: %s", val)
  1216. }
  1217. opts[val] = true
  1218. return nil
  1219. }
  1220. func (opts AttachOpts) Get(val string) bool {
  1221. if res, exists := opts[val]; exists {
  1222. return res
  1223. }
  1224. return false
  1225. }
  1226. // PathOpts stores a unique set of absolute paths
  1227. type PathOpts map[string]struct{}
  1228. func NewPathOpts() PathOpts {
  1229. return make(PathOpts)
  1230. }
  1231. func (opts PathOpts) String() string {
  1232. return fmt.Sprintf("%v", map[string]struct{}(opts))
  1233. }
  1234. func (opts PathOpts) Set(val string) error {
  1235. var containerPath string
  1236. splited := strings.SplitN(val, ":", 2)
  1237. if len(splited) == 1 {
  1238. containerPath = splited[0]
  1239. val = filepath.Clean(splited[0])
  1240. } else {
  1241. containerPath = splited[1]
  1242. val = fmt.Sprintf("%s:%s", splited[0], filepath.Clean(splited[1]))
  1243. }
  1244. if !filepath.IsAbs(containerPath) {
  1245. utils.Debugf("%s is not an absolute path", containerPath)
  1246. return fmt.Errorf("%s is not an absolute path", containerPath)
  1247. }
  1248. opts[val] = struct{}{}
  1249. return nil
  1250. }
  1251. func (cli *DockerCli) CmdTag(args ...string) error {
  1252. cmd := Subcmd("tag", "[OPTIONS] IMAGE REPOSITORY [TAG]", "Tag an image into a repository")
  1253. force := cmd.Bool("f", false, "Force")
  1254. if err := cmd.Parse(args); err != nil {
  1255. return nil
  1256. }
  1257. if cmd.NArg() != 2 && cmd.NArg() != 3 {
  1258. cmd.Usage()
  1259. return nil
  1260. }
  1261. v := url.Values{}
  1262. v.Set("repo", cmd.Arg(1))
  1263. if cmd.NArg() == 3 {
  1264. v.Set("tag", cmd.Arg(2))
  1265. }
  1266. if *force {
  1267. v.Set("force", "1")
  1268. }
  1269. if _, _, err := cli.call("POST", "/images/"+cmd.Arg(0)+"/tag?"+v.Encode(), nil); err != nil {
  1270. return err
  1271. }
  1272. return nil
  1273. }
  1274. func (cli *DockerCli) CmdRun(args ...string) error {
  1275. config, hostConfig, cmd, err := ParseRun(args, nil)
  1276. if err != nil {
  1277. return err
  1278. }
  1279. if config.Image == "" {
  1280. cmd.Usage()
  1281. return nil
  1282. }
  1283. var containerIDFile *os.File
  1284. if len(hostConfig.ContainerIDFile) > 0 {
  1285. if _, err := ioutil.ReadFile(hostConfig.ContainerIDFile); err == nil {
  1286. return fmt.Errorf("cid file found, make sure the other container isn't running or delete %s", hostConfig.ContainerIDFile)
  1287. }
  1288. containerIDFile, err = os.Create(hostConfig.ContainerIDFile)
  1289. if err != nil {
  1290. return fmt.Errorf("failed to create the container ID file: %s", err)
  1291. }
  1292. defer containerIDFile.Close()
  1293. }
  1294. //create the container
  1295. body, statusCode, err := cli.call("POST", "/containers/create", config)
  1296. //if image not found try to pull it
  1297. if statusCode == 404 {
  1298. _, tag := utils.ParseRepositoryTag(config.Image)
  1299. if tag == "" {
  1300. tag = DEFAULTTAG
  1301. }
  1302. fmt.Fprintf(cli.err, "Unable to find image '%s' (tag: %s) locally\n", config.Image, tag)
  1303. v := url.Values{}
  1304. repos, tag := utils.ParseRepositoryTag(config.Image)
  1305. v.Set("fromImage", repos)
  1306. v.Set("tag", tag)
  1307. // Resolve the Repository name from fqn to endpoint + name
  1308. var endpoint string
  1309. endpoint, _, err = registry.ResolveRepositoryName(repos)
  1310. if err != nil {
  1311. return err
  1312. }
  1313. // Load the auth config file, to be able to pull the image
  1314. cli.LoadConfigFile()
  1315. // Resolve the Auth config relevant for this server
  1316. authConfig := cli.configFile.ResolveAuthConfig(endpoint)
  1317. buf, err := json.Marshal(authConfig)
  1318. if err != nil {
  1319. return err
  1320. }
  1321. registryAuthHeader := []string{
  1322. base64.URLEncoding.EncodeToString(buf),
  1323. }
  1324. err = cli.stream("POST", "/images/create?"+v.Encode(), nil, cli.err, map[string][]string{
  1325. "X-Registry-Auth": registryAuthHeader,
  1326. })
  1327. if err != nil {
  1328. return err
  1329. }
  1330. body, _, err = cli.call("POST", "/containers/create", config)
  1331. if err != nil {
  1332. return err
  1333. }
  1334. }
  1335. if err != nil {
  1336. return err
  1337. }
  1338. runResult := &APIRun{}
  1339. err = json.Unmarshal(body, runResult)
  1340. if err != nil {
  1341. return err
  1342. }
  1343. for _, warning := range runResult.Warnings {
  1344. fmt.Fprintf(cli.err, "WARNING: %s\n", warning)
  1345. }
  1346. if len(hostConfig.ContainerIDFile) > 0 {
  1347. if _, err = containerIDFile.WriteString(runResult.ID); err != nil {
  1348. return fmt.Errorf("failed to write the container ID to the file: %s", err)
  1349. }
  1350. }
  1351. //start the container
  1352. if _, _, err = cli.call("POST", "/containers/"+runResult.ID+"/start", hostConfig); err != nil {
  1353. return err
  1354. }
  1355. var wait chan struct{}
  1356. if !config.AttachStdout && !config.AttachStderr {
  1357. // Make this asynchrone in order to let the client write to stdin before having to read the ID
  1358. wait = make(chan struct{})
  1359. go func() {
  1360. defer close(wait)
  1361. fmt.Fprintf(cli.out, "%s\n", runResult.ID)
  1362. }()
  1363. }
  1364. if config.AttachStdin || config.AttachStdout || config.AttachStderr {
  1365. if config.Tty {
  1366. if err := cli.monitorTtySize(runResult.ID); err != nil {
  1367. utils.Debugf("Error monitoring TTY size: %s\n", err)
  1368. }
  1369. }
  1370. v := url.Values{}
  1371. v.Set("logs", "1")
  1372. v.Set("stream", "1")
  1373. var out io.Writer
  1374. if config.AttachStdin {
  1375. v.Set("stdin", "1")
  1376. }
  1377. if config.AttachStdout {
  1378. v.Set("stdout", "1")
  1379. out = cli.out
  1380. }
  1381. if config.AttachStderr {
  1382. v.Set("stderr", "1")
  1383. out = cli.out
  1384. }
  1385. signals := make(chan os.Signal, 1)
  1386. signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
  1387. go func() {
  1388. for sig := range signals {
  1389. fmt.Printf("\nReceived signal: %s; cleaning up\n", sig)
  1390. if err := cli.CmdStop("-t", "4", runResult.ID); err != nil {
  1391. fmt.Printf("failed to stop container: %v", err)
  1392. }
  1393. }
  1394. }()
  1395. if err := cli.hijack("POST", "/containers/"+runResult.ID+"/attach?"+v.Encode(), config.Tty, cli.in, out); err != nil {
  1396. utils.Debugf("Error hijack: %s", err)
  1397. return err
  1398. }
  1399. }
  1400. if !config.AttachStdout && !config.AttachStderr {
  1401. // Detached mode
  1402. <-wait
  1403. } else {
  1404. status, err := waitForExit(cli, runResult.ID)
  1405. if err != nil {
  1406. return err
  1407. }
  1408. if status != 0 {
  1409. return &utils.StatusError{status}
  1410. }
  1411. }
  1412. return nil
  1413. }
  1414. func (cli *DockerCli) CmdCp(args ...string) error {
  1415. cmd := Subcmd("cp", "CONTAINER:RESOURCE HOSTPATH", "Copy files/folders from the RESOURCE to the HOSTPATH")
  1416. if err := cmd.Parse(args); err != nil {
  1417. return nil
  1418. }
  1419. if cmd.NArg() != 2 {
  1420. cmd.Usage()
  1421. return nil
  1422. }
  1423. var copyData APICopy
  1424. info := strings.Split(cmd.Arg(0), ":")
  1425. if len(info) != 2 {
  1426. return fmt.Errorf("Error: Resource not specified")
  1427. }
  1428. copyData.Resource = info[1]
  1429. copyData.HostPath = cmd.Arg(1)
  1430. data, statusCode, err := cli.call("POST", "/containers/"+info[0]+"/copy", copyData)
  1431. if err != nil {
  1432. return err
  1433. }
  1434. if statusCode == 200 {
  1435. r := bytes.NewReader(data)
  1436. if err := Untar(r, copyData.HostPath); err != nil {
  1437. return err
  1438. }
  1439. }
  1440. return nil
  1441. }
  1442. func (cli *DockerCli) call(method, path string, data interface{}) ([]byte, int, error) {
  1443. var params io.Reader
  1444. if data != nil {
  1445. buf, err := json.Marshal(data)
  1446. if err != nil {
  1447. return nil, -1, err
  1448. }
  1449. params = bytes.NewBuffer(buf)
  1450. }
  1451. req, err := http.NewRequest(method, fmt.Sprintf("/v%g%s", APIVERSION, path), params)
  1452. if err != nil {
  1453. return nil, -1, err
  1454. }
  1455. req.Header.Set("User-Agent", "Docker-Client/"+VERSION)
  1456. req.Host = cli.addr
  1457. if data != nil {
  1458. req.Header.Set("Content-Type", "application/json")
  1459. } else if method == "POST" {
  1460. req.Header.Set("Content-Type", "plain/text")
  1461. }
  1462. dial, err := net.Dial(cli.proto, cli.addr)
  1463. if err != nil {
  1464. if strings.Contains(err.Error(), "connection refused") {
  1465. return nil, -1, fmt.Errorf("Can't connect to docker daemon. Is 'docker -d' running on this host?")
  1466. }
  1467. return nil, -1, err
  1468. }
  1469. clientconn := httputil.NewClientConn(dial, nil)
  1470. resp, err := clientconn.Do(req)
  1471. defer clientconn.Close()
  1472. if err != nil {
  1473. if strings.Contains(err.Error(), "connection refused") {
  1474. return nil, -1, fmt.Errorf("Can't connect to docker daemon. Is 'docker -d' running on this host?")
  1475. }
  1476. return nil, -1, err
  1477. }
  1478. defer resp.Body.Close()
  1479. body, err := ioutil.ReadAll(resp.Body)
  1480. if err != nil {
  1481. return nil, -1, err
  1482. }
  1483. if resp.StatusCode < 200 || resp.StatusCode >= 400 {
  1484. if len(body) == 0 {
  1485. return nil, resp.StatusCode, fmt.Errorf("Error: %s", http.StatusText(resp.StatusCode))
  1486. }
  1487. return nil, resp.StatusCode, fmt.Errorf("Error: %s", body)
  1488. }
  1489. return body, resp.StatusCode, nil
  1490. }
  1491. func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer, headers map[string][]string) error {
  1492. if (method == "POST" || method == "PUT") && in == nil {
  1493. in = bytes.NewReader([]byte{})
  1494. }
  1495. req, err := http.NewRequest(method, fmt.Sprintf("/v%g%s", APIVERSION, path), in)
  1496. if err != nil {
  1497. return err
  1498. }
  1499. req.Header.Set("User-Agent", "Docker-Client/"+VERSION)
  1500. req.Host = cli.addr
  1501. if method == "POST" {
  1502. req.Header.Set("Content-Type", "plain/text")
  1503. }
  1504. if headers != nil {
  1505. for k, v := range headers {
  1506. req.Header[k] = v
  1507. }
  1508. }
  1509. dial, err := net.Dial(cli.proto, cli.addr)
  1510. if err != nil {
  1511. if strings.Contains(err.Error(), "connection refused") {
  1512. return fmt.Errorf("Can't connect to docker daemon. Is 'docker -d' running on this host?")
  1513. }
  1514. return err
  1515. }
  1516. clientconn := httputil.NewClientConn(dial, nil)
  1517. resp, err := clientconn.Do(req)
  1518. defer clientconn.Close()
  1519. if err != nil {
  1520. if strings.Contains(err.Error(), "connection refused") {
  1521. return fmt.Errorf("Can't connect to docker daemon. Is 'docker -d' running on this host?")
  1522. }
  1523. return err
  1524. }
  1525. defer resp.Body.Close()
  1526. if resp.StatusCode < 200 || resp.StatusCode >= 400 {
  1527. body, err := ioutil.ReadAll(resp.Body)
  1528. if err != nil {
  1529. return err
  1530. }
  1531. if len(body) == 0 {
  1532. return fmt.Errorf("Error :%s", http.StatusText(resp.StatusCode))
  1533. }
  1534. return fmt.Errorf("Error: %s", body)
  1535. }
  1536. if matchesContentType(resp.Header.Get("Content-Type"), "application/json") {
  1537. return utils.DisplayJSONMessagesStream(resp.Body, out)
  1538. } else {
  1539. if _, err := io.Copy(out, resp.Body); err != nil {
  1540. return err
  1541. }
  1542. }
  1543. return nil
  1544. }
  1545. func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in io.ReadCloser, out io.Writer) error {
  1546. req, err := http.NewRequest(method, fmt.Sprintf("/v%g%s", APIVERSION, path), nil)
  1547. if err != nil {
  1548. return err
  1549. }
  1550. req.Header.Set("User-Agent", "Docker-Client/"+VERSION)
  1551. req.Header.Set("Content-Type", "plain/text")
  1552. req.Host = cli.addr
  1553. dial, err := net.Dial(cli.proto, cli.addr)
  1554. if err != nil {
  1555. if strings.Contains(err.Error(), "connection refused") {
  1556. return fmt.Errorf("Can't connect to docker daemon. Is 'docker -d' running on this host?")
  1557. }
  1558. return err
  1559. }
  1560. clientconn := httputil.NewClientConn(dial, nil)
  1561. defer clientconn.Close()
  1562. // Server hijacks the connection, error 'connection closed' expected
  1563. clientconn.Do(req)
  1564. rwc, br := clientconn.Hijack()
  1565. defer rwc.Close()
  1566. var receiveStdout (chan error)
  1567. if out != nil {
  1568. receiveStdout = utils.Go(func() error {
  1569. _, err := io.Copy(out, br)
  1570. utils.Debugf("[hijack] End of stdout")
  1571. return err
  1572. })
  1573. }
  1574. if in != nil && setRawTerminal && cli.isTerminal && os.Getenv("NORAW") == "" {
  1575. oldState, err := term.SetRawTerminal(cli.terminalFd)
  1576. if err != nil {
  1577. return err
  1578. }
  1579. defer term.RestoreTerminal(cli.terminalFd, oldState)
  1580. }
  1581. sendStdin := utils.Go(func() error {
  1582. if in != nil {
  1583. io.Copy(rwc, in)
  1584. utils.Debugf("[hijack] End of stdin")
  1585. }
  1586. if tcpc, ok := rwc.(*net.TCPConn); ok {
  1587. if err := tcpc.CloseWrite(); err != nil {
  1588. utils.Debugf("Couldn't send EOF: %s\n", err)
  1589. }
  1590. } else if unixc, ok := rwc.(*net.UnixConn); ok {
  1591. if err := unixc.CloseWrite(); err != nil {
  1592. utils.Debugf("Couldn't send EOF: %s\n", err)
  1593. }
  1594. }
  1595. // Discard errors due to pipe interruption
  1596. return nil
  1597. })
  1598. if out != nil {
  1599. if err := <-receiveStdout; err != nil {
  1600. utils.Debugf("Error receiveStdout: %s", err)
  1601. return err
  1602. }
  1603. }
  1604. if !cli.isTerminal {
  1605. if err := <-sendStdin; err != nil {
  1606. utils.Debugf("Error sendStdin: %s", err)
  1607. return err
  1608. }
  1609. }
  1610. return nil
  1611. }
  1612. func (cli *DockerCli) getTtySize() (int, int) {
  1613. if !cli.isTerminal {
  1614. return 0, 0
  1615. }
  1616. ws, err := term.GetWinsize(cli.terminalFd)
  1617. if err != nil {
  1618. utils.Debugf("Error getting size: %s", err)
  1619. if ws == nil {
  1620. return 0, 0
  1621. }
  1622. }
  1623. return int(ws.Height), int(ws.Width)
  1624. }
  1625. func (cli *DockerCli) resizeTty(id string) {
  1626. height, width := cli.getTtySize()
  1627. if height == 0 && width == 0 {
  1628. return
  1629. }
  1630. v := url.Values{}
  1631. v.Set("h", strconv.Itoa(height))
  1632. v.Set("w", strconv.Itoa(width))
  1633. if _, _, err := cli.call("POST", "/containers/"+id+"/resize?"+v.Encode(), nil); err != nil {
  1634. utils.Debugf("Error resize: %s", err)
  1635. }
  1636. }
  1637. func (cli *DockerCli) monitorTtySize(id string) error {
  1638. if !cli.isTerminal {
  1639. return fmt.Errorf("Impossible to monitor size on non-tty")
  1640. }
  1641. cli.resizeTty(id)
  1642. sigchan := make(chan os.Signal, 1)
  1643. signal.Notify(sigchan, syscall.SIGWINCH)
  1644. go func() {
  1645. for _ = range sigchan {
  1646. cli.resizeTty(id)
  1647. }
  1648. }()
  1649. return nil
  1650. }
  1651. func Subcmd(name, signature, description string) *flag.FlagSet {
  1652. flags := flag.NewFlagSet(name, flag.ContinueOnError)
  1653. flags.Usage = func() {
  1654. // FIXME: use custom stdout or return error
  1655. fmt.Fprintf(os.Stdout, "\nUsage: docker %s %s\n\n%s\n\n", name, signature, description)
  1656. flags.PrintDefaults()
  1657. }
  1658. return flags
  1659. }
  1660. func (cli *DockerCli) LoadConfigFile() (err error) {
  1661. cli.configFile, err = auth.LoadConfig(os.Getenv("HOME"))
  1662. if err != nil {
  1663. fmt.Fprintf(cli.err, "WARNING: %s\n", err)
  1664. }
  1665. return err
  1666. }
  1667. func waitForExit(cli *DockerCli, containerId string) (int, error) {
  1668. body, _, err := cli.call("POST", "/containers/"+containerId+"/wait", nil)
  1669. if err != nil {
  1670. return -1, err
  1671. }
  1672. var out APIWait
  1673. if err := json.Unmarshal(body, &out); err != nil {
  1674. return -1, err
  1675. }
  1676. return out.StatusCode, nil
  1677. }
  1678. func NewDockerCli(in io.ReadCloser, out, err io.Writer, proto, addr string) *DockerCli {
  1679. var (
  1680. isTerminal = false
  1681. terminalFd uintptr
  1682. )
  1683. if in != nil {
  1684. if file, ok := in.(*os.File); ok {
  1685. terminalFd = file.Fd()
  1686. isTerminal = term.IsTerminal(terminalFd)
  1687. }
  1688. }
  1689. if err == nil {
  1690. err = out
  1691. }
  1692. return &DockerCli{
  1693. proto: proto,
  1694. addr: addr,
  1695. in: in,
  1696. out: out,
  1697. err: err,
  1698. isTerminal: isTerminal,
  1699. terminalFd: terminalFd,
  1700. }
  1701. }
  1702. type DockerCli struct {
  1703. proto string
  1704. addr string
  1705. configFile *auth.ConfigFile
  1706. in io.ReadCloser
  1707. out io.Writer
  1708. err io.Writer
  1709. isTerminal bool
  1710. terminalFd uintptr
  1711. }