commands.go 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630
  1. package client
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/base64"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "net/http"
  12. "net/url"
  13. "os"
  14. "os/exec"
  15. "path"
  16. "path/filepath"
  17. "runtime"
  18. "strconv"
  19. "strings"
  20. "text/tabwriter"
  21. "text/template"
  22. "time"
  23. log "github.com/Sirupsen/logrus"
  24. "github.com/docker/docker/api"
  25. "github.com/docker/docker/dockerversion"
  26. "github.com/docker/docker/engine"
  27. "github.com/docker/docker/graph"
  28. "github.com/docker/docker/nat"
  29. "github.com/docker/docker/opts"
  30. "github.com/docker/docker/pkg/archive"
  31. "github.com/docker/docker/pkg/fileutils"
  32. flag "github.com/docker/docker/pkg/mflag"
  33. "github.com/docker/docker/pkg/parsers"
  34. "github.com/docker/docker/pkg/parsers/filters"
  35. "github.com/docker/docker/pkg/promise"
  36. "github.com/docker/docker/pkg/signal"
  37. "github.com/docker/docker/pkg/term"
  38. "github.com/docker/docker/pkg/timeutils"
  39. "github.com/docker/docker/pkg/units"
  40. "github.com/docker/docker/pkg/urlutil"
  41. "github.com/docker/docker/registry"
  42. "github.com/docker/docker/runconfig"
  43. "github.com/docker/docker/utils"
  44. "github.com/docker/libtrust"
  45. )
  46. const (
  47. tarHeaderSize = 512
  48. )
  49. var (
  50. acceptedImageFilterTags = map[string]struct{}{"dangling": {}}
  51. )
  52. func (cli *DockerCli) CmdHelp(args ...string) error {
  53. if len(args) > 1 {
  54. method, exists := cli.getMethod(args[:2]...)
  55. if exists {
  56. method("--help")
  57. return nil
  58. }
  59. }
  60. if len(args) > 0 {
  61. method, exists := cli.getMethod(args[0])
  62. if !exists {
  63. fmt.Fprintf(cli.err, "docker: '%s' is not a docker command. See 'docker --help'.\n", args[0])
  64. os.Exit(1)
  65. } else {
  66. method("--help")
  67. return nil
  68. }
  69. }
  70. flag.Usage()
  71. return nil
  72. }
  73. func (cli *DockerCli) CmdBuild(args ...string) error {
  74. cmd := cli.Subcmd("build", "PATH | URL | -", "Build a new image from the source code at PATH", true)
  75. tag := cmd.String([]string{"t", "-tag"}, "", "Repository name (and optionally a tag) to be applied to the resulting image in case of success")
  76. suppressOutput := cmd.Bool([]string{"q", "-quiet"}, false, "Suppress the verbose output generated by the containers")
  77. noCache := cmd.Bool([]string{"#no-cache", "-no-cache"}, false, "Do not use cache when building the image")
  78. rm := cmd.Bool([]string{"#rm", "-rm"}, true, "Remove intermediate containers after a successful build")
  79. forceRm := cmd.Bool([]string{"-force-rm"}, false, "Always remove intermediate containers, even after unsuccessful builds")
  80. pull := cmd.Bool([]string{"-pull"}, false, "Always attempt to pull a newer version of the image")
  81. dockerfileName := cmd.String([]string{"f", "-file"}, "", "Name of the Dockerfile(Default is 'Dockerfile' at context root)")
  82. cmd.Require(flag.Exact, 1)
  83. utils.ParseFlags(cmd, args, true)
  84. var (
  85. context archive.Archive
  86. isRemote bool
  87. err error
  88. )
  89. _, err = exec.LookPath("git")
  90. hasGit := err == nil
  91. if cmd.Arg(0) == "-" {
  92. // As a special case, 'docker build -' will build from either an empty context with the
  93. // contents of stdin as a Dockerfile, or a tar-ed context from stdin.
  94. buf := bufio.NewReader(cli.in)
  95. magic, err := buf.Peek(tarHeaderSize)
  96. if err != nil && err != io.EOF {
  97. return fmt.Errorf("failed to peek context header from STDIN: %v", err)
  98. }
  99. if !archive.IsArchive(magic) {
  100. dockerfile, err := ioutil.ReadAll(buf)
  101. if err != nil {
  102. return fmt.Errorf("failed to read Dockerfile from STDIN: %v", err)
  103. }
  104. if *dockerfileName == "" {
  105. *dockerfileName = api.DefaultDockerfileName
  106. }
  107. context, err = archive.Generate(*dockerfileName, string(dockerfile))
  108. } else {
  109. context = ioutil.NopCloser(buf)
  110. }
  111. } else if urlutil.IsURL(cmd.Arg(0)) && (!urlutil.IsGitURL(cmd.Arg(0)) || !hasGit) {
  112. isRemote = true
  113. } else {
  114. root := cmd.Arg(0)
  115. if urlutil.IsGitURL(root) {
  116. remoteURL := cmd.Arg(0)
  117. if !urlutil.IsGitTransport(remoteURL) {
  118. remoteURL = "https://" + remoteURL
  119. }
  120. root, err = ioutil.TempDir("", "docker-build-git")
  121. if err != nil {
  122. return err
  123. }
  124. defer os.RemoveAll(root)
  125. if output, err := exec.Command("git", "clone", "--recursive", remoteURL, root).CombinedOutput(); err != nil {
  126. return fmt.Errorf("Error trying to use git: %s (%s)", err, output)
  127. }
  128. }
  129. if _, err := os.Stat(root); err != nil {
  130. return err
  131. }
  132. absRoot, err := filepath.Abs(root)
  133. if err != nil {
  134. return err
  135. }
  136. var filename string // path to Dockerfile
  137. var origDockerfile string // used for error msg
  138. if *dockerfileName == "" {
  139. // No -f/--file was specified so use the default
  140. origDockerfile = api.DefaultDockerfileName
  141. *dockerfileName = origDockerfile
  142. filename = path.Join(absRoot, *dockerfileName)
  143. } else {
  144. origDockerfile = *dockerfileName
  145. if filename, err = filepath.Abs(*dockerfileName); err != nil {
  146. return err
  147. }
  148. // Verify that 'filename' is within the build context
  149. if !strings.HasSuffix(absRoot, string(os.PathSeparator)) {
  150. absRoot += string(os.PathSeparator)
  151. }
  152. if !strings.HasPrefix(filename, absRoot) {
  153. return fmt.Errorf("The Dockerfile (%s) must be within the build context (%s)", *dockerfileName, root)
  154. }
  155. // Now reset the dockerfileName to be relative to the build context
  156. *dockerfileName = filename[len(absRoot):]
  157. }
  158. if _, err = os.Stat(filename); os.IsNotExist(err) {
  159. return fmt.Errorf("Can not locate Dockerfile: %s", origDockerfile)
  160. }
  161. var includes []string = []string{"."}
  162. excludes, err := utils.ReadDockerIgnore(path.Join(root, ".dockerignore"))
  163. if err != nil {
  164. return err
  165. }
  166. // If .dockerignore mentions .dockerignore or the Dockerfile
  167. // then make sure we send both files over to the daemon
  168. // because Dockerfile is, obviously, needed no matter what, and
  169. // .dockerignore is needed to know if either one needs to be
  170. // removed. The deamon will remove them for us, if needed, after it
  171. // parses the Dockerfile.
  172. keepThem1, _ := fileutils.Matches(".dockerignore", excludes)
  173. keepThem2, _ := fileutils.Matches(*dockerfileName, excludes)
  174. if keepThem1 || keepThem2 {
  175. includes = append(includes, ".dockerignore", *dockerfileName)
  176. }
  177. if err = utils.ValidateContextDirectory(root, excludes); err != nil {
  178. return fmt.Errorf("Error checking context is accessible: '%s'. Please check permissions and try again.", err)
  179. }
  180. options := &archive.TarOptions{
  181. Compression: archive.Uncompressed,
  182. ExcludePatterns: excludes,
  183. IncludeFiles: includes,
  184. }
  185. context, err = archive.TarWithOptions(root, options)
  186. if err != nil {
  187. return err
  188. }
  189. }
  190. var body io.Reader
  191. // Setup an upload progress bar
  192. // FIXME: ProgressReader shouldn't be this annoying to use
  193. if context != nil {
  194. sf := utils.NewStreamFormatter(false)
  195. body = utils.ProgressReader(context, 0, cli.out, sf, true, "", "Sending build context to Docker daemon")
  196. }
  197. // Send the build context
  198. v := &url.Values{}
  199. //Check if the given image name can be resolved
  200. if *tag != "" {
  201. repository, tag := parsers.ParseRepositoryTag(*tag)
  202. if err := registry.ValidateRepositoryName(repository); err != nil {
  203. return err
  204. }
  205. if len(tag) > 0 {
  206. if err := graph.ValidateTagName(tag); err != nil {
  207. return err
  208. }
  209. }
  210. }
  211. v.Set("t", *tag)
  212. if *suppressOutput {
  213. v.Set("q", "1")
  214. }
  215. if isRemote {
  216. v.Set("remote", cmd.Arg(0))
  217. }
  218. if *noCache {
  219. v.Set("nocache", "1")
  220. }
  221. if *rm {
  222. v.Set("rm", "1")
  223. } else {
  224. v.Set("rm", "0")
  225. }
  226. if *forceRm {
  227. v.Set("forcerm", "1")
  228. }
  229. if *pull {
  230. v.Set("pull", "1")
  231. }
  232. v.Set("dockerfile", *dockerfileName)
  233. cli.LoadConfigFile()
  234. headers := http.Header(make(map[string][]string))
  235. buf, err := json.Marshal(cli.configFile)
  236. if err != nil {
  237. return err
  238. }
  239. headers.Add("X-Registry-Config", base64.URLEncoding.EncodeToString(buf))
  240. if context != nil {
  241. headers.Set("Content-Type", "application/tar")
  242. }
  243. err = cli.stream("POST", fmt.Sprintf("/build?%s", v.Encode()), body, cli.out, headers)
  244. if jerr, ok := err.(*utils.JSONError); ok {
  245. // If no error code is set, default to 1
  246. if jerr.Code == 0 {
  247. jerr.Code = 1
  248. }
  249. return &utils.StatusError{Status: jerr.Message, StatusCode: jerr.Code}
  250. }
  251. return err
  252. }
  253. // 'docker login': login / register a user to registry service.
  254. func (cli *DockerCli) CmdLogin(args ...string) error {
  255. cmd := cli.Subcmd("login", "[SERVER]", "Register or log in to a Docker registry server, if no server is specified \""+registry.IndexServerAddress()+"\" is the default.", true)
  256. cmd.Require(flag.Max, 1)
  257. var username, password, email string
  258. cmd.StringVar(&username, []string{"u", "-username"}, "", "Username")
  259. cmd.StringVar(&password, []string{"p", "-password"}, "", "Password")
  260. cmd.StringVar(&email, []string{"e", "-email"}, "", "Email")
  261. utils.ParseFlags(cmd, args, true)
  262. serverAddress := registry.IndexServerAddress()
  263. if len(cmd.Args()) > 0 {
  264. serverAddress = cmd.Arg(0)
  265. }
  266. promptDefault := func(prompt string, configDefault string) {
  267. if configDefault == "" {
  268. fmt.Fprintf(cli.out, "%s: ", prompt)
  269. } else {
  270. fmt.Fprintf(cli.out, "%s (%s): ", prompt, configDefault)
  271. }
  272. }
  273. readInput := func(in io.Reader, out io.Writer) string {
  274. reader := bufio.NewReader(in)
  275. line, _, err := reader.ReadLine()
  276. if err != nil {
  277. fmt.Fprintln(out, err.Error())
  278. os.Exit(1)
  279. }
  280. return string(line)
  281. }
  282. cli.LoadConfigFile()
  283. authconfig, ok := cli.configFile.Configs[serverAddress]
  284. if !ok {
  285. authconfig = registry.AuthConfig{}
  286. }
  287. if username == "" {
  288. promptDefault("Username", authconfig.Username)
  289. username = readInput(cli.in, cli.out)
  290. if username == "" {
  291. username = authconfig.Username
  292. }
  293. }
  294. // Assume that a different username means they may not want to use
  295. // the password or email from the config file, so prompt them
  296. if username != authconfig.Username {
  297. if password == "" {
  298. oldState, err := term.SaveState(cli.inFd)
  299. if err != nil {
  300. return err
  301. }
  302. fmt.Fprintf(cli.out, "Password: ")
  303. term.DisableEcho(cli.inFd, oldState)
  304. password = readInput(cli.in, cli.out)
  305. fmt.Fprint(cli.out, "\n")
  306. term.RestoreTerminal(cli.inFd, oldState)
  307. if password == "" {
  308. return fmt.Errorf("Error : Password Required")
  309. }
  310. }
  311. if email == "" {
  312. promptDefault("Email", authconfig.Email)
  313. email = readInput(cli.in, cli.out)
  314. if email == "" {
  315. email = authconfig.Email
  316. }
  317. }
  318. } else {
  319. // However, if they don't override the username use the
  320. // password or email from the cmd line if specified. IOW, allow
  321. // then to change/overide them. And if not specified, just
  322. // use what's in the config file
  323. if password == "" {
  324. password = authconfig.Password
  325. }
  326. if email == "" {
  327. email = authconfig.Email
  328. }
  329. }
  330. authconfig.Username = username
  331. authconfig.Password = password
  332. authconfig.Email = email
  333. authconfig.ServerAddress = serverAddress
  334. cli.configFile.Configs[serverAddress] = authconfig
  335. stream, statusCode, err := cli.call("POST", "/auth", cli.configFile.Configs[serverAddress], false)
  336. if statusCode == 401 {
  337. delete(cli.configFile.Configs, serverAddress)
  338. registry.SaveConfig(cli.configFile)
  339. return err
  340. }
  341. if err != nil {
  342. return err
  343. }
  344. var out2 engine.Env
  345. err = out2.Decode(stream)
  346. if err != nil {
  347. cli.configFile, _ = registry.LoadConfig(os.Getenv("HOME"))
  348. return err
  349. }
  350. registry.SaveConfig(cli.configFile)
  351. if out2.Get("Status") != "" {
  352. fmt.Fprintf(cli.out, "%s\n", out2.Get("Status"))
  353. }
  354. return nil
  355. }
  356. // log out from a Docker registry
  357. func (cli *DockerCli) CmdLogout(args ...string) error {
  358. cmd := cli.Subcmd("logout", "[SERVER]", "Log out from a Docker registry, if no server is specified \""+registry.IndexServerAddress()+"\" is the default.", true)
  359. cmd.Require(flag.Max, 1)
  360. utils.ParseFlags(cmd, args, false)
  361. serverAddress := registry.IndexServerAddress()
  362. if len(cmd.Args()) > 0 {
  363. serverAddress = cmd.Arg(0)
  364. }
  365. cli.LoadConfigFile()
  366. if _, ok := cli.configFile.Configs[serverAddress]; !ok {
  367. fmt.Fprintf(cli.out, "Not logged in to %s\n", serverAddress)
  368. } else {
  369. fmt.Fprintf(cli.out, "Remove login credentials for %s\n", serverAddress)
  370. delete(cli.configFile.Configs, serverAddress)
  371. if err := registry.SaveConfig(cli.configFile); err != nil {
  372. return fmt.Errorf("Failed to save docker config: %v", err)
  373. }
  374. }
  375. return nil
  376. }
  377. // 'docker wait': block until a container stops
  378. func (cli *DockerCli) CmdWait(args ...string) error {
  379. cmd := cli.Subcmd("wait", "CONTAINER [CONTAINER...]", "Block until a container stops, then print its exit code.", true)
  380. cmd.Require(flag.Min, 1)
  381. utils.ParseFlags(cmd, args, true)
  382. var encounteredError error
  383. for _, name := range cmd.Args() {
  384. status, err := waitForExit(cli, name)
  385. if err != nil {
  386. fmt.Fprintf(cli.err, "%s\n", err)
  387. encounteredError = fmt.Errorf("Error: failed to wait one or more containers")
  388. } else {
  389. fmt.Fprintf(cli.out, "%d\n", status)
  390. }
  391. }
  392. return encounteredError
  393. }
  394. // 'docker version': show version information
  395. func (cli *DockerCli) CmdVersion(args ...string) error {
  396. cmd := cli.Subcmd("version", "", "Show the Docker version information.", true)
  397. cmd.Require(flag.Exact, 0)
  398. utils.ParseFlags(cmd, args, false)
  399. if dockerversion.VERSION != "" {
  400. fmt.Fprintf(cli.out, "Client version: %s\n", dockerversion.VERSION)
  401. }
  402. fmt.Fprintf(cli.out, "Client API version: %s\n", api.APIVERSION)
  403. fmt.Fprintf(cli.out, "Go version (client): %s\n", runtime.Version())
  404. if dockerversion.GITCOMMIT != "" {
  405. fmt.Fprintf(cli.out, "Git commit (client): %s\n", dockerversion.GITCOMMIT)
  406. }
  407. fmt.Fprintf(cli.out, "OS/Arch (client): %s/%s\n", runtime.GOOS, runtime.GOARCH)
  408. body, _, err := readBody(cli.call("GET", "/version", nil, false))
  409. if err != nil {
  410. return err
  411. }
  412. out := engine.NewOutput()
  413. remoteVersion, err := out.AddEnv()
  414. if err != nil {
  415. log.Errorf("Error reading remote version: %s", err)
  416. return err
  417. }
  418. if _, err := out.Write(body); err != nil {
  419. log.Errorf("Error reading remote version: %s", err)
  420. return err
  421. }
  422. out.Close()
  423. fmt.Fprintf(cli.out, "Server version: %s\n", remoteVersion.Get("Version"))
  424. if apiVersion := remoteVersion.Get("ApiVersion"); apiVersion != "" {
  425. fmt.Fprintf(cli.out, "Server API version: %s\n", apiVersion)
  426. }
  427. fmt.Fprintf(cli.out, "Go version (server): %s\n", remoteVersion.Get("GoVersion"))
  428. fmt.Fprintf(cli.out, "Git commit (server): %s\n", remoteVersion.Get("GitCommit"))
  429. return nil
  430. }
  431. // 'docker info': display system-wide information.
  432. func (cli *DockerCli) CmdInfo(args ...string) error {
  433. cmd := cli.Subcmd("info", "", "Display system-wide information", true)
  434. cmd.Require(flag.Exact, 0)
  435. utils.ParseFlags(cmd, args, false)
  436. body, _, err := readBody(cli.call("GET", "/info", nil, false))
  437. if err != nil {
  438. return err
  439. }
  440. out := engine.NewOutput()
  441. remoteInfo, err := out.AddEnv()
  442. if err != nil {
  443. return err
  444. }
  445. if _, err := out.Write(body); err != nil {
  446. log.Errorf("Error reading remote info: %s", err)
  447. return err
  448. }
  449. out.Close()
  450. if remoteInfo.Exists("Containers") {
  451. fmt.Fprintf(cli.out, "Containers: %d\n", remoteInfo.GetInt("Containers"))
  452. }
  453. if remoteInfo.Exists("Images") {
  454. fmt.Fprintf(cli.out, "Images: %d\n", remoteInfo.GetInt("Images"))
  455. }
  456. if remoteInfo.Exists("Driver") {
  457. fmt.Fprintf(cli.out, "Storage Driver: %s\n", remoteInfo.Get("Driver"))
  458. }
  459. if remoteInfo.Exists("DriverStatus") {
  460. var driverStatus [][2]string
  461. if err := remoteInfo.GetJson("DriverStatus", &driverStatus); err != nil {
  462. return err
  463. }
  464. for _, pair := range driverStatus {
  465. fmt.Fprintf(cli.out, " %s: %s\n", pair[0], pair[1])
  466. }
  467. }
  468. if remoteInfo.Exists("ExecutionDriver") {
  469. fmt.Fprintf(cli.out, "Execution Driver: %s\n", remoteInfo.Get("ExecutionDriver"))
  470. }
  471. if remoteInfo.Exists("KernelVersion") {
  472. fmt.Fprintf(cli.out, "Kernel Version: %s\n", remoteInfo.Get("KernelVersion"))
  473. }
  474. if remoteInfo.Exists("OperatingSystem") {
  475. fmt.Fprintf(cli.out, "Operating System: %s\n", remoteInfo.Get("OperatingSystem"))
  476. }
  477. if remoteInfo.Exists("NCPU") {
  478. fmt.Fprintf(cli.out, "CPUs: %d\n", remoteInfo.GetInt("NCPU"))
  479. }
  480. if remoteInfo.Exists("MemTotal") {
  481. fmt.Fprintf(cli.out, "Total Memory: %s\n", units.BytesSize(float64(remoteInfo.GetInt64("MemTotal"))))
  482. }
  483. if remoteInfo.Exists("Name") {
  484. fmt.Fprintf(cli.out, "Name: %s\n", remoteInfo.Get("Name"))
  485. }
  486. if remoteInfo.Exists("ID") {
  487. fmt.Fprintf(cli.out, "ID: %s\n", remoteInfo.Get("ID"))
  488. }
  489. if remoteInfo.GetBool("Debug") || os.Getenv("DEBUG") != "" {
  490. if remoteInfo.Exists("Debug") {
  491. fmt.Fprintf(cli.out, "Debug mode (server): %v\n", remoteInfo.GetBool("Debug"))
  492. }
  493. fmt.Fprintf(cli.out, "Debug mode (client): %v\n", os.Getenv("DEBUG") != "")
  494. if remoteInfo.Exists("NFd") {
  495. fmt.Fprintf(cli.out, "Fds: %d\n", remoteInfo.GetInt("NFd"))
  496. }
  497. if remoteInfo.Exists("NGoroutines") {
  498. fmt.Fprintf(cli.out, "Goroutines: %d\n", remoteInfo.GetInt("NGoroutines"))
  499. }
  500. if remoteInfo.Exists("NEventsListener") {
  501. fmt.Fprintf(cli.out, "EventsListeners: %d\n", remoteInfo.GetInt("NEventsListener"))
  502. }
  503. if initSha1 := remoteInfo.Get("InitSha1"); initSha1 != "" {
  504. fmt.Fprintf(cli.out, "Init SHA1: %s\n", initSha1)
  505. }
  506. if initPath := remoteInfo.Get("InitPath"); initPath != "" {
  507. fmt.Fprintf(cli.out, "Init Path: %s\n", initPath)
  508. }
  509. if root := remoteInfo.Get("DockerRootDir"); root != "" {
  510. fmt.Fprintf(cli.out, "Docker Root Dir: %s\n", root)
  511. }
  512. }
  513. if len(remoteInfo.GetList("IndexServerAddress")) != 0 {
  514. cli.LoadConfigFile()
  515. u := cli.configFile.Configs[remoteInfo.Get("IndexServerAddress")].Username
  516. if len(u) > 0 {
  517. fmt.Fprintf(cli.out, "Username: %v\n", u)
  518. fmt.Fprintf(cli.out, "Registry: %v\n", remoteInfo.GetList("IndexServerAddress"))
  519. }
  520. }
  521. if remoteInfo.Exists("MemoryLimit") && !remoteInfo.GetBool("MemoryLimit") {
  522. fmt.Fprintf(cli.err, "WARNING: No memory limit support\n")
  523. }
  524. if remoteInfo.Exists("SwapLimit") && !remoteInfo.GetBool("SwapLimit") {
  525. fmt.Fprintf(cli.err, "WARNING: No swap limit support\n")
  526. }
  527. if remoteInfo.Exists("IPv4Forwarding") && !remoteInfo.GetBool("IPv4Forwarding") {
  528. fmt.Fprintf(cli.err, "WARNING: IPv4 forwarding is disabled.\n")
  529. }
  530. if remoteInfo.Exists("Labels") {
  531. fmt.Fprintln(cli.out, "Labels:")
  532. for _, attribute := range remoteInfo.GetList("Labels") {
  533. fmt.Fprintf(cli.out, " %s\n", attribute)
  534. }
  535. }
  536. return nil
  537. }
  538. func (cli *DockerCli) CmdStop(args ...string) error {
  539. cmd := cli.Subcmd("stop", "CONTAINER [CONTAINER...]", "Stop a running container by sending SIGTERM and then SIGKILL after a grace period", true)
  540. nSeconds := cmd.Int([]string{"t", "-time"}, 10, "Number of seconds to wait for the container to stop before killing it. Default is 10 seconds.")
  541. cmd.Require(flag.Min, 1)
  542. utils.ParseFlags(cmd, args, true)
  543. v := url.Values{}
  544. v.Set("t", strconv.Itoa(*nSeconds))
  545. var encounteredError error
  546. for _, name := range cmd.Args() {
  547. _, _, err := readBody(cli.call("POST", "/containers/"+name+"/stop?"+v.Encode(), nil, false))
  548. if err != nil {
  549. fmt.Fprintf(cli.err, "%s\n", err)
  550. encounteredError = fmt.Errorf("Error: failed to stop one or more containers")
  551. } else {
  552. fmt.Fprintf(cli.out, "%s\n", name)
  553. }
  554. }
  555. return encounteredError
  556. }
  557. func (cli *DockerCli) CmdRestart(args ...string) error {
  558. cmd := cli.Subcmd("restart", "CONTAINER [CONTAINER...]", "Restart a running container", true)
  559. nSeconds := cmd.Int([]string{"t", "-time"}, 10, "Number of seconds to try to stop for before killing the container. Once killed it will then be restarted. Default is 10 seconds.")
  560. cmd.Require(flag.Min, 1)
  561. utils.ParseFlags(cmd, args, true)
  562. v := url.Values{}
  563. v.Set("t", strconv.Itoa(*nSeconds))
  564. var encounteredError error
  565. for _, name := range cmd.Args() {
  566. _, _, err := readBody(cli.call("POST", "/containers/"+name+"/restart?"+v.Encode(), nil, false))
  567. if err != nil {
  568. fmt.Fprintf(cli.err, "%s\n", err)
  569. encounteredError = fmt.Errorf("Error: failed to restart one or more containers")
  570. } else {
  571. fmt.Fprintf(cli.out, "%s\n", name)
  572. }
  573. }
  574. return encounteredError
  575. }
  576. func (cli *DockerCli) forwardAllSignals(cid string) chan os.Signal {
  577. sigc := make(chan os.Signal, 128)
  578. signal.CatchAll(sigc)
  579. go func() {
  580. for s := range sigc {
  581. if s == signal.SIGCHLD {
  582. continue
  583. }
  584. var sig string
  585. for sigStr, sigN := range signal.SignalMap {
  586. if sigN == s {
  587. sig = sigStr
  588. break
  589. }
  590. }
  591. if sig == "" {
  592. log.Errorf("Unsupported signal: %v. Discarding.", s)
  593. }
  594. if _, _, err := readBody(cli.call("POST", fmt.Sprintf("/containers/%s/kill?signal=%s", cid, sig), nil, false)); err != nil {
  595. log.Debugf("Error sending signal: %s", err)
  596. }
  597. }
  598. }()
  599. return sigc
  600. }
  601. func (cli *DockerCli) CmdStart(args ...string) error {
  602. var (
  603. cErr chan error
  604. tty bool
  605. cmd = cli.Subcmd("start", "CONTAINER [CONTAINER...]", "Restart a stopped container", true)
  606. attach = cmd.Bool([]string{"a", "-attach"}, false, "Attach container's STDOUT and STDERR and forward all signals to the process")
  607. openStdin = cmd.Bool([]string{"i", "-interactive"}, false, "Attach container's STDIN")
  608. )
  609. cmd.Require(flag.Min, 1)
  610. utils.ParseFlags(cmd, args, true)
  611. hijacked := make(chan io.Closer)
  612. if *attach || *openStdin {
  613. if cmd.NArg() > 1 {
  614. return fmt.Errorf("You cannot start and attach multiple containers at once.")
  615. }
  616. stream, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/json", nil, false)
  617. if err != nil {
  618. return err
  619. }
  620. env := engine.Env{}
  621. if err := env.Decode(stream); err != nil {
  622. return err
  623. }
  624. config := env.GetSubEnv("Config")
  625. tty = config.GetBool("Tty")
  626. if !tty {
  627. sigc := cli.forwardAllSignals(cmd.Arg(0))
  628. defer signal.StopCatch(sigc)
  629. }
  630. var in io.ReadCloser
  631. v := url.Values{}
  632. v.Set("stream", "1")
  633. if *openStdin && config.GetBool("OpenStdin") {
  634. v.Set("stdin", "1")
  635. in = cli.in
  636. }
  637. v.Set("stdout", "1")
  638. v.Set("stderr", "1")
  639. cErr = promise.Go(func() error {
  640. return cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), tty, in, cli.out, cli.err, hijacked, nil)
  641. })
  642. } else {
  643. close(hijacked)
  644. }
  645. // Acknowledge the hijack before starting
  646. select {
  647. case closer := <-hijacked:
  648. // Make sure that the hijack gets closed when returning (results
  649. // in closing the hijack chan and freeing server's goroutines)
  650. if closer != nil {
  651. defer closer.Close()
  652. }
  653. case err := <-cErr:
  654. if err != nil {
  655. return err
  656. }
  657. }
  658. var encounteredError error
  659. for _, name := range cmd.Args() {
  660. _, _, err := readBody(cli.call("POST", "/containers/"+name+"/start", nil, false))
  661. if err != nil {
  662. if !*attach && !*openStdin {
  663. fmt.Fprintf(cli.err, "%s\n", err)
  664. }
  665. encounteredError = fmt.Errorf("Error: failed to start one or more containers")
  666. } else {
  667. if !*attach && !*openStdin {
  668. fmt.Fprintf(cli.out, "%s\n", name)
  669. }
  670. }
  671. }
  672. if encounteredError != nil {
  673. if *openStdin || *attach {
  674. cli.in.Close()
  675. }
  676. return encounteredError
  677. }
  678. if *openStdin || *attach {
  679. if tty && cli.isTerminalOut {
  680. if err := cli.monitorTtySize(cmd.Arg(0), false); err != nil {
  681. log.Errorf("Error monitoring TTY size: %s", err)
  682. }
  683. }
  684. if attchErr := <-cErr; attchErr != nil {
  685. return attchErr
  686. }
  687. _, status, err := getExitCode(cli, cmd.Arg(0))
  688. if err != nil {
  689. return err
  690. }
  691. if status != 0 {
  692. return &utils.StatusError{StatusCode: status}
  693. }
  694. }
  695. return nil
  696. }
  697. func (cli *DockerCli) CmdUnpause(args ...string) error {
  698. cmd := cli.Subcmd("unpause", "CONTAINER", "Unpause all processes within a container", true)
  699. cmd.Require(flag.Exact, 1)
  700. utils.ParseFlags(cmd, args, false)
  701. var encounteredError error
  702. for _, name := range cmd.Args() {
  703. if _, _, err := readBody(cli.call("POST", fmt.Sprintf("/containers/%s/unpause", name), nil, false)); err != nil {
  704. fmt.Fprintf(cli.err, "%s\n", err)
  705. encounteredError = fmt.Errorf("Error: failed to unpause container named %s", name)
  706. } else {
  707. fmt.Fprintf(cli.out, "%s\n", name)
  708. }
  709. }
  710. return encounteredError
  711. }
  712. func (cli *DockerCli) CmdPause(args ...string) error {
  713. cmd := cli.Subcmd("pause", "CONTAINER", "Pause all processes within a container", true)
  714. cmd.Require(flag.Exact, 1)
  715. utils.ParseFlags(cmd, args, false)
  716. var encounteredError error
  717. for _, name := range cmd.Args() {
  718. if _, _, err := readBody(cli.call("POST", fmt.Sprintf("/containers/%s/pause", name), nil, false)); err != nil {
  719. fmt.Fprintf(cli.err, "%s\n", err)
  720. encounteredError = fmt.Errorf("Error: failed to pause container named %s", name)
  721. } else {
  722. fmt.Fprintf(cli.out, "%s\n", name)
  723. }
  724. }
  725. return encounteredError
  726. }
  727. func (cli *DockerCli) CmdRename(args ...string) error {
  728. cmd := cli.Subcmd("rename", "OLD_NAME NEW_NAME", "Rename a container", true)
  729. if err := cmd.Parse(args); err != nil {
  730. return nil
  731. }
  732. if cmd.NArg() != 2 {
  733. cmd.Usage()
  734. return nil
  735. }
  736. old_name := cmd.Arg(0)
  737. new_name := cmd.Arg(1)
  738. if _, _, err := readBody(cli.call("POST", fmt.Sprintf("/containers/%s/rename?name=%s", old_name, new_name), nil, false)); err != nil {
  739. fmt.Fprintf(cli.err, "%s\n", err)
  740. return fmt.Errorf("Error: failed to rename container named %s", old_name)
  741. }
  742. return nil
  743. }
  744. func (cli *DockerCli) CmdInspect(args ...string) error {
  745. cmd := cli.Subcmd("inspect", "CONTAINER|IMAGE [CONTAINER|IMAGE...]", "Return low-level information on a container or image", true)
  746. tmplStr := cmd.String([]string{"f", "#format", "-format"}, "", "Format the output using the given go template.")
  747. cmd.Require(flag.Min, 1)
  748. utils.ParseFlags(cmd, args, true)
  749. var tmpl *template.Template
  750. if *tmplStr != "" {
  751. var err error
  752. if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
  753. fmt.Fprintf(cli.err, "Template parsing error: %v\n", err)
  754. return &utils.StatusError{StatusCode: 64,
  755. Status: "Template parsing error: " + err.Error()}
  756. }
  757. }
  758. indented := new(bytes.Buffer)
  759. indented.WriteByte('[')
  760. status := 0
  761. for _, name := range cmd.Args() {
  762. obj, _, err := readBody(cli.call("GET", "/containers/"+name+"/json", nil, false))
  763. if err != nil {
  764. obj, _, err = readBody(cli.call("GET", "/images/"+name+"/json", nil, false))
  765. if err != nil {
  766. if strings.Contains(err.Error(), "No such") {
  767. fmt.Fprintf(cli.err, "Error: No such image or container: %s\n", name)
  768. } else {
  769. fmt.Fprintf(cli.err, "%s", err)
  770. }
  771. status = 1
  772. continue
  773. }
  774. }
  775. if tmpl == nil {
  776. if err = json.Indent(indented, obj, "", " "); err != nil {
  777. fmt.Fprintf(cli.err, "%s\n", err)
  778. status = 1
  779. continue
  780. }
  781. } else {
  782. // Has template, will render
  783. var value interface{}
  784. if err := json.Unmarshal(obj, &value); err != nil {
  785. fmt.Fprintf(cli.err, "%s\n", err)
  786. status = 1
  787. continue
  788. }
  789. if err := tmpl.Execute(cli.out, value); err != nil {
  790. return err
  791. }
  792. cli.out.Write([]byte{'\n'})
  793. }
  794. indented.WriteString(",")
  795. }
  796. if indented.Len() > 1 {
  797. // Remove trailing ','
  798. indented.Truncate(indented.Len() - 1)
  799. }
  800. indented.WriteString("]\n")
  801. if tmpl == nil {
  802. if _, err := io.Copy(cli.out, indented); err != nil {
  803. return err
  804. }
  805. }
  806. if status != 0 {
  807. return &utils.StatusError{StatusCode: status}
  808. }
  809. return nil
  810. }
  811. func (cli *DockerCli) CmdTop(args ...string) error {
  812. cmd := cli.Subcmd("top", "CONTAINER [ps OPTIONS]", "Display the running processes of a container", true)
  813. cmd.Require(flag.Min, 1)
  814. utils.ParseFlags(cmd, args, true)
  815. val := url.Values{}
  816. if cmd.NArg() > 1 {
  817. val.Set("ps_args", strings.Join(cmd.Args()[1:], " "))
  818. }
  819. stream, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/top?"+val.Encode(), nil, false)
  820. if err != nil {
  821. return err
  822. }
  823. var procs engine.Env
  824. if err := procs.Decode(stream); err != nil {
  825. return err
  826. }
  827. w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
  828. fmt.Fprintln(w, strings.Join(procs.GetList("Titles"), "\t"))
  829. processes := [][]string{}
  830. if err := procs.GetJson("Processes", &processes); err != nil {
  831. return err
  832. }
  833. for _, proc := range processes {
  834. fmt.Fprintln(w, strings.Join(proc, "\t"))
  835. }
  836. w.Flush()
  837. return nil
  838. }
  839. func (cli *DockerCli) CmdPort(args ...string) error {
  840. cmd := cli.Subcmd("port", "CONTAINER [PRIVATE_PORT[/PROTO]]", "List port mappings for the CONTAINER, or lookup the public-facing port that is NAT-ed to the PRIVATE_PORT", true)
  841. cmd.Require(flag.Min, 1)
  842. utils.ParseFlags(cmd, args, true)
  843. stream, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/json", nil, false)
  844. if err != nil {
  845. return err
  846. }
  847. env := engine.Env{}
  848. if err := env.Decode(stream); err != nil {
  849. return err
  850. }
  851. ports := nat.PortMap{}
  852. if err := env.GetSubEnv("NetworkSettings").GetJson("Ports", &ports); err != nil {
  853. return err
  854. }
  855. if cmd.NArg() == 2 {
  856. var (
  857. port = cmd.Arg(1)
  858. proto = "tcp"
  859. parts = strings.SplitN(port, "/", 2)
  860. )
  861. if len(parts) == 2 && len(parts[1]) != 0 {
  862. port = parts[0]
  863. proto = parts[1]
  864. }
  865. natPort := port + "/" + proto
  866. if frontends, exists := ports[nat.Port(port+"/"+proto)]; exists && frontends != nil {
  867. for _, frontend := range frontends {
  868. fmt.Fprintf(cli.out, "%s:%s\n", frontend.HostIp, frontend.HostPort)
  869. }
  870. return nil
  871. }
  872. return fmt.Errorf("Error: No public port '%s' published for %s", natPort, cmd.Arg(0))
  873. }
  874. for from, frontends := range ports {
  875. for _, frontend := range frontends {
  876. fmt.Fprintf(cli.out, "%s -> %s:%s\n", from, frontend.HostIp, frontend.HostPort)
  877. }
  878. }
  879. return nil
  880. }
  881. // 'docker rmi IMAGE' removes all images with the name IMAGE
  882. func (cli *DockerCli) CmdRmi(args ...string) error {
  883. var (
  884. cmd = cli.Subcmd("rmi", "IMAGE [IMAGE...]", "Remove one or more images", true)
  885. force = cmd.Bool([]string{"f", "-force"}, false, "Force removal of the image")
  886. noprune = cmd.Bool([]string{"-no-prune"}, false, "Do not delete untagged parents")
  887. )
  888. cmd.Require(flag.Min, 1)
  889. utils.ParseFlags(cmd, args, true)
  890. v := url.Values{}
  891. if *force {
  892. v.Set("force", "1")
  893. }
  894. if *noprune {
  895. v.Set("noprune", "1")
  896. }
  897. var encounteredError error
  898. for _, name := range cmd.Args() {
  899. body, _, err := readBody(cli.call("DELETE", "/images/"+name+"?"+v.Encode(), nil, false))
  900. if err != nil {
  901. fmt.Fprintf(cli.err, "%s\n", err)
  902. encounteredError = fmt.Errorf("Error: failed to remove one or more images")
  903. } else {
  904. outs := engine.NewTable("Created", 0)
  905. if _, err := outs.ReadListFrom(body); err != nil {
  906. fmt.Fprintf(cli.err, "%s\n", err)
  907. encounteredError = fmt.Errorf("Error: failed to remove one or more images")
  908. continue
  909. }
  910. for _, out := range outs.Data {
  911. if out.Get("Deleted") != "" {
  912. fmt.Fprintf(cli.out, "Deleted: %s\n", out.Get("Deleted"))
  913. } else {
  914. fmt.Fprintf(cli.out, "Untagged: %s\n", out.Get("Untagged"))
  915. }
  916. }
  917. }
  918. }
  919. return encounteredError
  920. }
  921. func (cli *DockerCli) CmdHistory(args ...string) error {
  922. cmd := cli.Subcmd("history", "IMAGE", "Show the history of an image", true)
  923. quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only show numeric IDs")
  924. noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
  925. cmd.Require(flag.Exact, 1)
  926. utils.ParseFlags(cmd, args, true)
  927. body, _, err := readBody(cli.call("GET", "/images/"+cmd.Arg(0)+"/history", nil, false))
  928. if err != nil {
  929. return err
  930. }
  931. outs := engine.NewTable("Created", 0)
  932. if _, err := outs.ReadListFrom(body); err != nil {
  933. return err
  934. }
  935. w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
  936. if !*quiet {
  937. fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE")
  938. }
  939. for _, out := range outs.Data {
  940. outID := out.Get("Id")
  941. if !*quiet {
  942. if *noTrunc {
  943. fmt.Fprintf(w, "%s\t", outID)
  944. } else {
  945. fmt.Fprintf(w, "%s\t", utils.TruncateID(outID))
  946. }
  947. fmt.Fprintf(w, "%s ago\t", units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))))
  948. if *noTrunc {
  949. fmt.Fprintf(w, "%s\t", out.Get("CreatedBy"))
  950. } else {
  951. fmt.Fprintf(w, "%s\t", utils.Trunc(out.Get("CreatedBy"), 45))
  952. }
  953. fmt.Fprintf(w, "%s\n", units.HumanSize(float64(out.GetInt64("Size"))))
  954. } else {
  955. if *noTrunc {
  956. fmt.Fprintln(w, outID)
  957. } else {
  958. fmt.Fprintln(w, utils.TruncateID(outID))
  959. }
  960. }
  961. }
  962. w.Flush()
  963. return nil
  964. }
  965. func (cli *DockerCli) CmdRm(args ...string) error {
  966. cmd := cli.Subcmd("rm", "CONTAINER [CONTAINER...]", "Remove one or more containers", true)
  967. v := cmd.Bool([]string{"v", "-volumes"}, false, "Remove the volumes associated with the container")
  968. link := cmd.Bool([]string{"l", "#link", "-link"}, false, "Remove the specified link and not the underlying container")
  969. force := cmd.Bool([]string{"f", "-force"}, false, "Force the removal of a running container (uses SIGKILL)")
  970. cmd.Require(flag.Min, 1)
  971. utils.ParseFlags(cmd, args, true)
  972. val := url.Values{}
  973. if *v {
  974. val.Set("v", "1")
  975. }
  976. if *link {
  977. val.Set("link", "1")
  978. }
  979. if *force {
  980. val.Set("force", "1")
  981. }
  982. var encounteredError error
  983. for _, name := range cmd.Args() {
  984. _, _, err := readBody(cli.call("DELETE", "/containers/"+name+"?"+val.Encode(), nil, false))
  985. if err != nil {
  986. fmt.Fprintf(cli.err, "%s\n", err)
  987. encounteredError = fmt.Errorf("Error: failed to remove one or more containers")
  988. } else {
  989. fmt.Fprintf(cli.out, "%s\n", name)
  990. }
  991. }
  992. return encounteredError
  993. }
  994. // 'docker kill NAME' kills a running container
  995. func (cli *DockerCli) CmdKill(args ...string) error {
  996. cmd := cli.Subcmd("kill", "CONTAINER [CONTAINER...]", "Kill a running container using SIGKILL or a specified signal", true)
  997. signal := cmd.String([]string{"s", "-signal"}, "KILL", "Signal to send to the container")
  998. cmd.Require(flag.Min, 1)
  999. utils.ParseFlags(cmd, args, true)
  1000. var encounteredError error
  1001. for _, name := range cmd.Args() {
  1002. if _, _, err := readBody(cli.call("POST", fmt.Sprintf("/containers/%s/kill?signal=%s", name, *signal), nil, false)); err != nil {
  1003. fmt.Fprintf(cli.err, "%s\n", err)
  1004. encounteredError = fmt.Errorf("Error: failed to kill one or more containers")
  1005. } else {
  1006. fmt.Fprintf(cli.out, "%s\n", name)
  1007. }
  1008. }
  1009. return encounteredError
  1010. }
  1011. func (cli *DockerCli) CmdImport(args ...string) error {
  1012. cmd := cli.Subcmd("import", "URL|- [REPOSITORY[:TAG]]", "Create an empty filesystem image and import the contents of the tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) into it, then optionally tag it.", true)
  1013. cmd.Require(flag.Min, 1)
  1014. utils.ParseFlags(cmd, args, true)
  1015. var (
  1016. v = url.Values{}
  1017. src = cmd.Arg(0)
  1018. repository = cmd.Arg(1)
  1019. )
  1020. v.Set("fromSrc", src)
  1021. v.Set("repo", repository)
  1022. if cmd.NArg() == 3 {
  1023. fmt.Fprintf(cli.err, "[DEPRECATED] The format 'URL|- [REPOSITORY [TAG]]' as been deprecated. Please use URL|- [REPOSITORY[:TAG]]\n")
  1024. v.Set("tag", cmd.Arg(2))
  1025. }
  1026. if repository != "" {
  1027. //Check if the given image name can be resolved
  1028. repo, _ := parsers.ParseRepositoryTag(repository)
  1029. if err := registry.ValidateRepositoryName(repo); err != nil {
  1030. return err
  1031. }
  1032. }
  1033. var in io.Reader
  1034. if src == "-" {
  1035. in = cli.in
  1036. }
  1037. return cli.stream("POST", "/images/create?"+v.Encode(), in, cli.out, nil)
  1038. }
  1039. func (cli *DockerCli) CmdPush(args ...string) error {
  1040. cmd := cli.Subcmd("push", "NAME[:TAG]", "Push an image or a repository to the registry", true)
  1041. cmd.Require(flag.Exact, 1)
  1042. utils.ParseFlags(cmd, args, true)
  1043. name := cmd.Arg(0)
  1044. cli.LoadConfigFile()
  1045. remote, tag := parsers.ParseRepositoryTag(name)
  1046. // Resolve the Repository name from fqn to RepositoryInfo
  1047. repoInfo, err := registry.ParseRepositoryInfo(remote)
  1048. if err != nil {
  1049. return err
  1050. }
  1051. // Resolve the Auth config relevant for this server
  1052. authConfig := cli.configFile.ResolveAuthConfig(repoInfo.Index)
  1053. // If we're not using a custom registry, we know the restrictions
  1054. // applied to repository names and can warn the user in advance.
  1055. // Custom repositories can have different rules, and we must also
  1056. // allow pushing by image ID.
  1057. if repoInfo.Official {
  1058. username := authConfig.Username
  1059. if username == "" {
  1060. username = "<user>"
  1061. }
  1062. return fmt.Errorf("You cannot push a \"root\" repository. Please rename your repository to <user>/<repo> (ex: %s/%s)", username, repoInfo.LocalName)
  1063. }
  1064. v := url.Values{}
  1065. v.Set("tag", tag)
  1066. body, _, err := readBody(cli.call("GET", "/images/"+remote+"/manifest?"+v.Encode(), nil, false))
  1067. if err != nil {
  1068. return err
  1069. }
  1070. js, err := libtrust.NewJSONSignature(body)
  1071. if err != nil {
  1072. return err
  1073. }
  1074. err = js.Sign(cli.key)
  1075. if err != nil {
  1076. return err
  1077. }
  1078. signedBody, err := js.PrettySignature("signatures")
  1079. if err != nil {
  1080. return err
  1081. }
  1082. push := func(authConfig registry.AuthConfig) error {
  1083. buf, err := json.Marshal(authConfig)
  1084. if err != nil {
  1085. return err
  1086. }
  1087. registryAuthHeader := []string{
  1088. base64.URLEncoding.EncodeToString(buf),
  1089. }
  1090. return cli.stream("POST", "/images/"+remote+"/push?"+v.Encode(), bytes.NewReader(signedBody), cli.out, map[string][]string{
  1091. "X-Registry-Auth": registryAuthHeader,
  1092. })
  1093. }
  1094. if err := push(authConfig); err != nil {
  1095. if strings.Contains(err.Error(), "Status 401") {
  1096. fmt.Fprintln(cli.out, "\nPlease login prior to push:")
  1097. if err := cli.CmdLogin(repoInfo.Index.GetAuthConfigKey()); err != nil {
  1098. return err
  1099. }
  1100. authConfig := cli.configFile.ResolveAuthConfig(repoInfo.Index)
  1101. return push(authConfig)
  1102. }
  1103. return err
  1104. }
  1105. return nil
  1106. }
  1107. func (cli *DockerCli) CmdPull(args ...string) error {
  1108. cmd := cli.Subcmd("pull", "NAME[:TAG]", "Pull an image or a repository from the registry", true)
  1109. allTags := cmd.Bool([]string{"a", "-all-tags"}, false, "Download all tagged images in the repository")
  1110. cmd.Require(flag.Exact, 1)
  1111. utils.ParseFlags(cmd, args, true)
  1112. var (
  1113. v = url.Values{}
  1114. remote = cmd.Arg(0)
  1115. newRemote = remote
  1116. )
  1117. taglessRemote, tag := parsers.ParseRepositoryTag(remote)
  1118. if tag == "" && !*allTags {
  1119. newRemote = taglessRemote + ":" + graph.DEFAULTTAG
  1120. }
  1121. if tag != "" && *allTags {
  1122. return fmt.Errorf("tag can't be used with --all-tags/-a")
  1123. }
  1124. v.Set("fromImage", newRemote)
  1125. // Resolve the Repository name from fqn to RepositoryInfo
  1126. repoInfo, err := registry.ParseRepositoryInfo(taglessRemote)
  1127. if err != nil {
  1128. return err
  1129. }
  1130. cli.LoadConfigFile()
  1131. // Resolve the Auth config relevant for this server
  1132. authConfig := cli.configFile.ResolveAuthConfig(repoInfo.Index)
  1133. pull := func(authConfig registry.AuthConfig) error {
  1134. buf, err := json.Marshal(authConfig)
  1135. if err != nil {
  1136. return err
  1137. }
  1138. registryAuthHeader := []string{
  1139. base64.URLEncoding.EncodeToString(buf),
  1140. }
  1141. return cli.stream("POST", "/images/create?"+v.Encode(), nil, cli.out, map[string][]string{
  1142. "X-Registry-Auth": registryAuthHeader,
  1143. })
  1144. }
  1145. if err := pull(authConfig); err != nil {
  1146. if strings.Contains(err.Error(), "Status 401") {
  1147. fmt.Fprintln(cli.out, "\nPlease login prior to pull:")
  1148. if err := cli.CmdLogin(repoInfo.Index.GetAuthConfigKey()); err != nil {
  1149. return err
  1150. }
  1151. authConfig := cli.configFile.ResolveAuthConfig(repoInfo.Index)
  1152. return pull(authConfig)
  1153. }
  1154. return err
  1155. }
  1156. return nil
  1157. }
  1158. func (cli *DockerCli) CmdImages(args ...string) error {
  1159. cmd := cli.Subcmd("images", "[REPOSITORY]", "List images", true)
  1160. quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only show numeric IDs")
  1161. all := cmd.Bool([]string{"a", "-all"}, false, "Show all images (by default filter out the intermediate image layers)")
  1162. noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
  1163. // FIXME: --viz and --tree are deprecated. Remove them in a future version.
  1164. flViz := cmd.Bool([]string{"#v", "#viz", "#-viz"}, false, "Output graph in graphviz format")
  1165. flTree := cmd.Bool([]string{"#t", "#tree", "#-tree"}, false, "Output graph in tree format")
  1166. flFilter := opts.NewListOpts(nil)
  1167. cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values (i.e. 'dangling=true')")
  1168. cmd.Require(flag.Max, 1)
  1169. utils.ParseFlags(cmd, args, true)
  1170. // Consolidate all filter flags, and sanity check them early.
  1171. // They'll get process in the daemon/server.
  1172. imageFilterArgs := filters.Args{}
  1173. for _, f := range flFilter.GetAll() {
  1174. var err error
  1175. imageFilterArgs, err = filters.ParseFlag(f, imageFilterArgs)
  1176. if err != nil {
  1177. return err
  1178. }
  1179. }
  1180. for name := range imageFilterArgs {
  1181. if _, ok := acceptedImageFilterTags[name]; !ok {
  1182. return fmt.Errorf("Invalid filter '%s'", name)
  1183. }
  1184. }
  1185. matchName := cmd.Arg(0)
  1186. // FIXME: --viz and --tree are deprecated. Remove them in a future version.
  1187. if *flViz || *flTree {
  1188. v := url.Values{
  1189. "all": []string{"1"},
  1190. }
  1191. if len(imageFilterArgs) > 0 {
  1192. filterJson, err := filters.ToParam(imageFilterArgs)
  1193. if err != nil {
  1194. return err
  1195. }
  1196. v.Set("filters", filterJson)
  1197. }
  1198. body, _, err := readBody(cli.call("GET", "/images/json?"+v.Encode(), nil, false))
  1199. if err != nil {
  1200. return err
  1201. }
  1202. outs := engine.NewTable("Created", 0)
  1203. if _, err := outs.ReadListFrom(body); err != nil {
  1204. return err
  1205. }
  1206. var (
  1207. printNode func(cli *DockerCli, noTrunc bool, image *engine.Env, prefix string)
  1208. startImage *engine.Env
  1209. roots = engine.NewTable("Created", outs.Len())
  1210. byParent = make(map[string]*engine.Table)
  1211. )
  1212. for _, image := range outs.Data {
  1213. if image.Get("ParentId") == "" {
  1214. roots.Add(image)
  1215. } else {
  1216. if children, exists := byParent[image.Get("ParentId")]; exists {
  1217. children.Add(image)
  1218. } else {
  1219. byParent[image.Get("ParentId")] = engine.NewTable("Created", 1)
  1220. byParent[image.Get("ParentId")].Add(image)
  1221. }
  1222. }
  1223. if matchName != "" {
  1224. if matchName == image.Get("Id") || matchName == utils.TruncateID(image.Get("Id")) {
  1225. startImage = image
  1226. }
  1227. for _, repotag := range image.GetList("RepoTags") {
  1228. if repotag == matchName {
  1229. startImage = image
  1230. }
  1231. }
  1232. }
  1233. }
  1234. if *flViz {
  1235. fmt.Fprintf(cli.out, "digraph docker {\n")
  1236. printNode = (*DockerCli).printVizNode
  1237. } else {
  1238. printNode = (*DockerCli).printTreeNode
  1239. }
  1240. if startImage != nil {
  1241. root := engine.NewTable("Created", 1)
  1242. root.Add(startImage)
  1243. cli.WalkTree(*noTrunc, root, byParent, "", printNode)
  1244. } else if matchName == "" {
  1245. cli.WalkTree(*noTrunc, roots, byParent, "", printNode)
  1246. }
  1247. if *flViz {
  1248. fmt.Fprintf(cli.out, " base [style=invisible]\n}\n")
  1249. }
  1250. } else {
  1251. v := url.Values{}
  1252. if len(imageFilterArgs) > 0 {
  1253. filterJson, err := filters.ToParam(imageFilterArgs)
  1254. if err != nil {
  1255. return err
  1256. }
  1257. v.Set("filters", filterJson)
  1258. }
  1259. if cmd.NArg() == 1 {
  1260. // FIXME rename this parameter, to not be confused with the filters flag
  1261. v.Set("filter", matchName)
  1262. }
  1263. if *all {
  1264. v.Set("all", "1")
  1265. }
  1266. body, _, err := readBody(cli.call("GET", "/images/json?"+v.Encode(), nil, false))
  1267. if err != nil {
  1268. return err
  1269. }
  1270. outs := engine.NewTable("Created", 0)
  1271. if _, err := outs.ReadListFrom(body); err != nil {
  1272. return err
  1273. }
  1274. w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
  1275. if !*quiet {
  1276. fmt.Fprintln(w, "REPOSITORY\tTAG\tIMAGE ID\tCREATED\tVIRTUAL SIZE")
  1277. }
  1278. for _, out := range outs.Data {
  1279. for _, repotag := range out.GetList("RepoTags") {
  1280. repo, tag := parsers.ParseRepositoryTag(repotag)
  1281. outID := out.Get("Id")
  1282. if !*noTrunc {
  1283. outID = utils.TruncateID(outID)
  1284. }
  1285. if !*quiet {
  1286. fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\n", repo, tag, outID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))), units.HumanSize(float64(out.GetInt64("VirtualSize"))))
  1287. } else {
  1288. fmt.Fprintln(w, outID)
  1289. }
  1290. }
  1291. }
  1292. if !*quiet {
  1293. w.Flush()
  1294. }
  1295. }
  1296. return nil
  1297. }
  1298. // FIXME: --viz and --tree are deprecated. Remove them in a future version.
  1299. func (cli *DockerCli) WalkTree(noTrunc bool, images *engine.Table, byParent map[string]*engine.Table, prefix string, printNode func(cli *DockerCli, noTrunc bool, image *engine.Env, prefix string)) {
  1300. length := images.Len()
  1301. if length > 1 {
  1302. for index, image := range images.Data {
  1303. if index+1 == length {
  1304. printNode(cli, noTrunc, image, prefix+"└─")
  1305. if subimages, exists := byParent[image.Get("Id")]; exists {
  1306. cli.WalkTree(noTrunc, subimages, byParent, prefix+" ", printNode)
  1307. }
  1308. } else {
  1309. printNode(cli, noTrunc, image, prefix+"\u251C─")
  1310. if subimages, exists := byParent[image.Get("Id")]; exists {
  1311. cli.WalkTree(noTrunc, subimages, byParent, prefix+"\u2502 ", printNode)
  1312. }
  1313. }
  1314. }
  1315. } else {
  1316. for _, image := range images.Data {
  1317. printNode(cli, noTrunc, image, prefix+"└─")
  1318. if subimages, exists := byParent[image.Get("Id")]; exists {
  1319. cli.WalkTree(noTrunc, subimages, byParent, prefix+" ", printNode)
  1320. }
  1321. }
  1322. }
  1323. }
  1324. // FIXME: --viz and --tree are deprecated. Remove them in a future version.
  1325. func (cli *DockerCli) printVizNode(noTrunc bool, image *engine.Env, prefix string) {
  1326. var (
  1327. imageID string
  1328. parentID string
  1329. )
  1330. if noTrunc {
  1331. imageID = image.Get("Id")
  1332. parentID = image.Get("ParentId")
  1333. } else {
  1334. imageID = utils.TruncateID(image.Get("Id"))
  1335. parentID = utils.TruncateID(image.Get("ParentId"))
  1336. }
  1337. if parentID == "" {
  1338. fmt.Fprintf(cli.out, " base -> \"%s\" [style=invis]\n", imageID)
  1339. } else {
  1340. fmt.Fprintf(cli.out, " \"%s\" -> \"%s\"\n", parentID, imageID)
  1341. }
  1342. if image.GetList("RepoTags")[0] != "<none>:<none>" {
  1343. fmt.Fprintf(cli.out, " \"%s\" [label=\"%s\\n%s\",shape=box,fillcolor=\"paleturquoise\",style=\"filled,rounded\"];\n",
  1344. imageID, imageID, strings.Join(image.GetList("RepoTags"), "\\n"))
  1345. }
  1346. }
  1347. // FIXME: --viz and --tree are deprecated. Remove them in a future version.
  1348. func (cli *DockerCli) printTreeNode(noTrunc bool, image *engine.Env, prefix string) {
  1349. var imageID string
  1350. if noTrunc {
  1351. imageID = image.Get("Id")
  1352. } else {
  1353. imageID = utils.TruncateID(image.Get("Id"))
  1354. }
  1355. fmt.Fprintf(cli.out, "%s%s Virtual Size: %s", prefix, imageID, units.HumanSize(float64(image.GetInt64("VirtualSize"))))
  1356. if image.GetList("RepoTags")[0] != "<none>:<none>" {
  1357. fmt.Fprintf(cli.out, " Tags: %s\n", strings.Join(image.GetList("RepoTags"), ", "))
  1358. } else {
  1359. fmt.Fprint(cli.out, "\n")
  1360. }
  1361. }
  1362. func (cli *DockerCli) CmdPs(args ...string) error {
  1363. var (
  1364. err error
  1365. psFilterArgs = filters.Args{}
  1366. v = url.Values{}
  1367. cmd = cli.Subcmd("ps", "", "List containers", true)
  1368. quiet = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs")
  1369. size = cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes")
  1370. all = cmd.Bool([]string{"a", "-all"}, false, "Show all containers. Only running containers are shown by default.")
  1371. noTrunc = cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
  1372. nLatest = cmd.Bool([]string{"l", "-latest"}, false, "Show only the latest created container, include non-running ones.")
  1373. since = cmd.String([]string{"#sinceId", "#-since-id", "-since"}, "", "Show only containers created since Id or Name, include non-running ones.")
  1374. before = cmd.String([]string{"#beforeId", "#-before-id", "-before"}, "", "Show only container created before Id or Name, include non-running ones.")
  1375. last = cmd.Int([]string{"n"}, -1, "Show n last created containers, include non-running ones.")
  1376. flFilter = opts.NewListOpts(nil)
  1377. )
  1378. cmd.Require(flag.Exact, 0)
  1379. cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values. Valid filters:\nexited=<int> - containers with exit code of <int>\nstatus=(restarting|running|paused|exited)")
  1380. utils.ParseFlags(cmd, args, true)
  1381. if *last == -1 && *nLatest {
  1382. *last = 1
  1383. }
  1384. if *all {
  1385. v.Set("all", "1")
  1386. }
  1387. if *last != -1 {
  1388. v.Set("limit", strconv.Itoa(*last))
  1389. }
  1390. if *since != "" {
  1391. v.Set("since", *since)
  1392. }
  1393. if *before != "" {
  1394. v.Set("before", *before)
  1395. }
  1396. if *size {
  1397. v.Set("size", "1")
  1398. }
  1399. // Consolidate all filter flags, and sanity check them.
  1400. // They'll get processed in the daemon/server.
  1401. for _, f := range flFilter.GetAll() {
  1402. if psFilterArgs, err = filters.ParseFlag(f, psFilterArgs); err != nil {
  1403. return err
  1404. }
  1405. }
  1406. if len(psFilterArgs) > 0 {
  1407. filterJson, err := filters.ToParam(psFilterArgs)
  1408. if err != nil {
  1409. return err
  1410. }
  1411. v.Set("filters", filterJson)
  1412. }
  1413. body, _, err := readBody(cli.call("GET", "/containers/json?"+v.Encode(), nil, false))
  1414. if err != nil {
  1415. return err
  1416. }
  1417. outs := engine.NewTable("Created", 0)
  1418. if _, err := outs.ReadListFrom(body); err != nil {
  1419. return err
  1420. }
  1421. w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
  1422. if !*quiet {
  1423. fmt.Fprint(w, "CONTAINER ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS\tNAMES")
  1424. if *size {
  1425. fmt.Fprintln(w, "\tSIZE")
  1426. } else {
  1427. fmt.Fprint(w, "\n")
  1428. }
  1429. }
  1430. stripNamePrefix := func(ss []string) []string {
  1431. for i, s := range ss {
  1432. ss[i] = s[1:]
  1433. }
  1434. return ss
  1435. }
  1436. for _, out := range outs.Data {
  1437. outID := out.Get("Id")
  1438. if !*noTrunc {
  1439. outID = utils.TruncateID(outID)
  1440. }
  1441. if *quiet {
  1442. fmt.Fprintln(w, outID)
  1443. continue
  1444. }
  1445. var (
  1446. outNames = stripNamePrefix(out.GetList("Names"))
  1447. outCommand = strconv.Quote(out.Get("Command"))
  1448. ports = engine.NewTable("", 0)
  1449. )
  1450. if !*noTrunc {
  1451. outCommand = utils.Trunc(outCommand, 20)
  1452. // only display the default name for the container with notrunc is passed
  1453. for _, name := range outNames {
  1454. if len(strings.Split(name, "/")) == 1 {
  1455. outNames = []string{name}
  1456. break
  1457. }
  1458. }
  1459. }
  1460. ports.ReadListFrom([]byte(out.Get("Ports")))
  1461. image := out.Get("Image")
  1462. if image == "" {
  1463. image = "<no image>"
  1464. }
  1465. fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\t%s\t%s\t", outID, image, outCommand,
  1466. units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))),
  1467. out.Get("Status"), api.DisplayablePorts(ports), strings.Join(outNames, ","))
  1468. if *size {
  1469. if out.GetInt("SizeRootFs") > 0 {
  1470. fmt.Fprintf(w, "%s (virtual %s)\n", units.HumanSize(float64(out.GetInt64("SizeRw"))), units.HumanSize(float64(out.GetInt64("SizeRootFs"))))
  1471. } else {
  1472. fmt.Fprintf(w, "%s\n", units.HumanSize(float64(out.GetInt64("SizeRw"))))
  1473. }
  1474. continue
  1475. }
  1476. fmt.Fprint(w, "\n")
  1477. }
  1478. if !*quiet {
  1479. w.Flush()
  1480. }
  1481. return nil
  1482. }
  1483. func (cli *DockerCli) CmdCommit(args ...string) error {
  1484. cmd := cli.Subcmd("commit", "CONTAINER [REPOSITORY[:TAG]]", "Create a new image from a container's changes", true)
  1485. flPause := cmd.Bool([]string{"p", "-pause"}, true, "Pause container during commit")
  1486. flComment := cmd.String([]string{"m", "-message"}, "", "Commit message")
  1487. flAuthor := cmd.String([]string{"a", "#author", "-author"}, "", "Author (e.g., \"John Hannibal Smith <hannibal@a-team.com>\")")
  1488. // FIXME: --run is deprecated, it will be replaced with inline Dockerfile commands.
  1489. flConfig := cmd.String([]string{"#run", "#-run"}, "", "This option is deprecated and will be removed in a future version in favor of inline Dockerfile-compatible commands")
  1490. cmd.Require(flag.Max, 2)
  1491. cmd.Require(flag.Min, 1)
  1492. utils.ParseFlags(cmd, args, true)
  1493. var (
  1494. name = cmd.Arg(0)
  1495. repository, tag = parsers.ParseRepositoryTag(cmd.Arg(1))
  1496. )
  1497. //Check if the given image name can be resolved
  1498. if repository != "" {
  1499. if err := registry.ValidateRepositoryName(repository); err != nil {
  1500. return err
  1501. }
  1502. }
  1503. v := url.Values{}
  1504. v.Set("container", name)
  1505. v.Set("repo", repository)
  1506. v.Set("tag", tag)
  1507. v.Set("comment", *flComment)
  1508. v.Set("author", *flAuthor)
  1509. if *flPause != true {
  1510. v.Set("pause", "0")
  1511. }
  1512. var (
  1513. config *runconfig.Config
  1514. env engine.Env
  1515. )
  1516. if *flConfig != "" {
  1517. config = &runconfig.Config{}
  1518. if err := json.Unmarshal([]byte(*flConfig), config); err != nil {
  1519. return err
  1520. }
  1521. }
  1522. stream, _, err := cli.call("POST", "/commit?"+v.Encode(), config, false)
  1523. if err != nil {
  1524. return err
  1525. }
  1526. if err := env.Decode(stream); err != nil {
  1527. return err
  1528. }
  1529. fmt.Fprintf(cli.out, "%s\n", env.Get("Id"))
  1530. return nil
  1531. }
  1532. func (cli *DockerCli) CmdEvents(args ...string) error {
  1533. cmd := cli.Subcmd("events", "", "Get real time events from the server", true)
  1534. since := cmd.String([]string{"#since", "-since"}, "", "Show all events created since timestamp")
  1535. until := cmd.String([]string{"-until"}, "", "Stream events until this timestamp")
  1536. flFilter := opts.NewListOpts(nil)
  1537. cmd.Var(&flFilter, []string{"f", "-filter"}, "Provide filter values (i.e. 'event=stop')")
  1538. cmd.Require(flag.Exact, 0)
  1539. utils.ParseFlags(cmd, args, true)
  1540. var (
  1541. v = url.Values{}
  1542. loc = time.FixedZone(time.Now().Zone())
  1543. eventFilterArgs = filters.Args{}
  1544. )
  1545. // Consolidate all filter flags, and sanity check them early.
  1546. // They'll get process in the daemon/server.
  1547. for _, f := range flFilter.GetAll() {
  1548. var err error
  1549. eventFilterArgs, err = filters.ParseFlag(f, eventFilterArgs)
  1550. if err != nil {
  1551. return err
  1552. }
  1553. }
  1554. var setTime = func(key, value string) {
  1555. format := timeutils.RFC3339NanoFixed
  1556. if len(value) < len(format) {
  1557. format = format[:len(value)]
  1558. }
  1559. if t, err := time.ParseInLocation(format, value, loc); err == nil {
  1560. v.Set(key, strconv.FormatInt(t.Unix(), 10))
  1561. } else {
  1562. v.Set(key, value)
  1563. }
  1564. }
  1565. if *since != "" {
  1566. setTime("since", *since)
  1567. }
  1568. if *until != "" {
  1569. setTime("until", *until)
  1570. }
  1571. if len(eventFilterArgs) > 0 {
  1572. filterJson, err := filters.ToParam(eventFilterArgs)
  1573. if err != nil {
  1574. return err
  1575. }
  1576. v.Set("filters", filterJson)
  1577. }
  1578. if err := cli.stream("GET", "/events?"+v.Encode(), nil, cli.out, nil); err != nil {
  1579. return err
  1580. }
  1581. return nil
  1582. }
  1583. func (cli *DockerCli) CmdExport(args ...string) error {
  1584. cmd := cli.Subcmd("export", "CONTAINER", "Export the contents of a filesystem as a tar archive to STDOUT", true)
  1585. cmd.Require(flag.Exact, 1)
  1586. utils.ParseFlags(cmd, args, true)
  1587. if err := cli.stream("GET", "/containers/"+cmd.Arg(0)+"/export", nil, cli.out, nil); err != nil {
  1588. return err
  1589. }
  1590. return nil
  1591. }
  1592. func (cli *DockerCli) CmdDiff(args ...string) error {
  1593. cmd := cli.Subcmd("diff", "CONTAINER", "Inspect changes on a container's filesystem", true)
  1594. cmd.Require(flag.Exact, 1)
  1595. utils.ParseFlags(cmd, args, true)
  1596. body, _, err := readBody(cli.call("GET", "/containers/"+cmd.Arg(0)+"/changes", nil, false))
  1597. if err != nil {
  1598. return err
  1599. }
  1600. outs := engine.NewTable("", 0)
  1601. if _, err := outs.ReadListFrom(body); err != nil {
  1602. return err
  1603. }
  1604. for _, change := range outs.Data {
  1605. var kind string
  1606. switch change.GetInt("Kind") {
  1607. case archive.ChangeModify:
  1608. kind = "C"
  1609. case archive.ChangeAdd:
  1610. kind = "A"
  1611. case archive.ChangeDelete:
  1612. kind = "D"
  1613. }
  1614. fmt.Fprintf(cli.out, "%s %s\n", kind, change.Get("Path"))
  1615. }
  1616. return nil
  1617. }
  1618. func (cli *DockerCli) CmdLogs(args ...string) error {
  1619. var (
  1620. cmd = cli.Subcmd("logs", "CONTAINER", "Fetch the logs of a container", true)
  1621. follow = cmd.Bool([]string{"f", "-follow"}, false, "Follow log output")
  1622. times = cmd.Bool([]string{"t", "-timestamps"}, false, "Show timestamps")
  1623. tail = cmd.String([]string{"-tail"}, "all", "Output the specified number of lines at the end of logs (defaults to all logs)")
  1624. )
  1625. cmd.Require(flag.Exact, 1)
  1626. utils.ParseFlags(cmd, args, true)
  1627. name := cmd.Arg(0)
  1628. stream, _, err := cli.call("GET", "/containers/"+name+"/json", nil, false)
  1629. if err != nil {
  1630. return err
  1631. }
  1632. env := engine.Env{}
  1633. if err := env.Decode(stream); err != nil {
  1634. return err
  1635. }
  1636. v := url.Values{}
  1637. v.Set("stdout", "1")
  1638. v.Set("stderr", "1")
  1639. if *times {
  1640. v.Set("timestamps", "1")
  1641. }
  1642. if *follow {
  1643. v.Set("follow", "1")
  1644. }
  1645. v.Set("tail", *tail)
  1646. return cli.streamHelper("GET", "/containers/"+name+"/logs?"+v.Encode(), env.GetSubEnv("Config").GetBool("Tty"), nil, cli.out, cli.err, nil)
  1647. }
  1648. func (cli *DockerCli) CmdAttach(args ...string) error {
  1649. var (
  1650. cmd = cli.Subcmd("attach", "CONTAINER", "Attach to a running container", true)
  1651. noStdin = cmd.Bool([]string{"#nostdin", "-no-stdin"}, false, "Do not attach STDIN")
  1652. proxy = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxy all received signals to the process (non-TTY mode only). SIGCHLD, SIGKILL, and SIGSTOP are not proxied.")
  1653. )
  1654. cmd.Require(flag.Exact, 1)
  1655. utils.ParseFlags(cmd, args, true)
  1656. name := cmd.Arg(0)
  1657. stream, _, err := cli.call("GET", "/containers/"+name+"/json", nil, false)
  1658. if err != nil {
  1659. return err
  1660. }
  1661. env := engine.Env{}
  1662. if err := env.Decode(stream); err != nil {
  1663. return err
  1664. }
  1665. if !env.GetSubEnv("State").GetBool("Running") {
  1666. return fmt.Errorf("You cannot attach to a stopped container, start it first")
  1667. }
  1668. var (
  1669. config = env.GetSubEnv("Config")
  1670. tty = config.GetBool("Tty")
  1671. )
  1672. if err := cli.CheckTtyInput(!*noStdin, tty); err != nil {
  1673. return err
  1674. }
  1675. if tty && cli.isTerminalOut {
  1676. if err := cli.monitorTtySize(cmd.Arg(0), false); err != nil {
  1677. log.Debugf("Error monitoring TTY size: %s", err)
  1678. }
  1679. }
  1680. var in io.ReadCloser
  1681. v := url.Values{}
  1682. v.Set("stream", "1")
  1683. if !*noStdin && config.GetBool("OpenStdin") {
  1684. v.Set("stdin", "1")
  1685. in = cli.in
  1686. }
  1687. v.Set("stdout", "1")
  1688. v.Set("stderr", "1")
  1689. if *proxy && !tty {
  1690. sigc := cli.forwardAllSignals(cmd.Arg(0))
  1691. defer signal.StopCatch(sigc)
  1692. }
  1693. if err := cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), tty, in, cli.out, cli.err, nil, nil); err != nil {
  1694. return err
  1695. }
  1696. _, status, err := getExitCode(cli, cmd.Arg(0))
  1697. if err != nil {
  1698. return err
  1699. }
  1700. if status != 0 {
  1701. return &utils.StatusError{StatusCode: status}
  1702. }
  1703. return nil
  1704. }
  1705. func (cli *DockerCli) CmdSearch(args ...string) error {
  1706. cmd := cli.Subcmd("search", "TERM", "Search the Docker Hub for images", true)
  1707. noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
  1708. trusted := cmd.Bool([]string{"#t", "#trusted", "#-trusted"}, false, "Only show trusted builds")
  1709. automated := cmd.Bool([]string{"-automated"}, false, "Only show automated builds")
  1710. stars := cmd.Int([]string{"s", "#stars", "-stars"}, 0, "Only displays with at least x stars")
  1711. cmd.Require(flag.Exact, 1)
  1712. utils.ParseFlags(cmd, args, true)
  1713. v := url.Values{}
  1714. v.Set("term", cmd.Arg(0))
  1715. body, _, err := readBody(cli.call("GET", "/images/search?"+v.Encode(), nil, true))
  1716. if err != nil {
  1717. return err
  1718. }
  1719. outs := engine.NewTable("star_count", 0)
  1720. if _, err := outs.ReadListFrom(body); err != nil {
  1721. return err
  1722. }
  1723. w := tabwriter.NewWriter(cli.out, 10, 1, 3, ' ', 0)
  1724. fmt.Fprintf(w, "NAME\tDESCRIPTION\tSTARS\tOFFICIAL\tAUTOMATED\n")
  1725. for _, out := range outs.Data {
  1726. if ((*automated || *trusted) && (!out.GetBool("is_trusted") && !out.GetBool("is_automated"))) || (*stars > out.GetInt("star_count")) {
  1727. continue
  1728. }
  1729. desc := strings.Replace(out.Get("description"), "\n", " ", -1)
  1730. desc = strings.Replace(desc, "\r", " ", -1)
  1731. if !*noTrunc && len(desc) > 45 {
  1732. desc = utils.Trunc(desc, 42) + "..."
  1733. }
  1734. fmt.Fprintf(w, "%s\t%s\t%d\t", out.Get("name"), desc, out.GetInt("star_count"))
  1735. if out.GetBool("is_official") {
  1736. fmt.Fprint(w, "[OK]")
  1737. }
  1738. fmt.Fprint(w, "\t")
  1739. if out.GetBool("is_automated") || out.GetBool("is_trusted") {
  1740. fmt.Fprint(w, "[OK]")
  1741. }
  1742. fmt.Fprint(w, "\n")
  1743. }
  1744. w.Flush()
  1745. return nil
  1746. }
  1747. // Ports type - Used to parse multiple -p flags
  1748. type ports []int
  1749. func (cli *DockerCli) CmdTag(args ...string) error {
  1750. cmd := cli.Subcmd("tag", "IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]", "Tag an image into a repository", true)
  1751. force := cmd.Bool([]string{"f", "#force", "-force"}, false, "Force")
  1752. cmd.Require(flag.Exact, 2)
  1753. utils.ParseFlags(cmd, args, true)
  1754. var (
  1755. repository, tag = parsers.ParseRepositoryTag(cmd.Arg(1))
  1756. v = url.Values{}
  1757. )
  1758. //Check if the given image name can be resolved
  1759. if err := registry.ValidateRepositoryName(repository); err != nil {
  1760. return err
  1761. }
  1762. v.Set("repo", repository)
  1763. v.Set("tag", tag)
  1764. if *force {
  1765. v.Set("force", "1")
  1766. }
  1767. if _, _, err := readBody(cli.call("POST", "/images/"+cmd.Arg(0)+"/tag?"+v.Encode(), nil, false)); err != nil {
  1768. return err
  1769. }
  1770. return nil
  1771. }
  1772. func (cli *DockerCli) pullImage(image string) error {
  1773. return cli.pullImageCustomOut(image, cli.out)
  1774. }
  1775. func (cli *DockerCli) pullImageCustomOut(image string, out io.Writer) error {
  1776. v := url.Values{}
  1777. repos, tag := parsers.ParseRepositoryTag(image)
  1778. // pull only the image tagged 'latest' if no tag was specified
  1779. if tag == "" {
  1780. tag = graph.DEFAULTTAG
  1781. }
  1782. v.Set("fromImage", repos)
  1783. v.Set("tag", tag)
  1784. // Resolve the Repository name from fqn to RepositoryInfo
  1785. repoInfo, err := registry.ParseRepositoryInfo(repos)
  1786. if err != nil {
  1787. return err
  1788. }
  1789. // Load the auth config file, to be able to pull the image
  1790. cli.LoadConfigFile()
  1791. // Resolve the Auth config relevant for this server
  1792. authConfig := cli.configFile.ResolveAuthConfig(repoInfo.Index)
  1793. buf, err := json.Marshal(authConfig)
  1794. if err != nil {
  1795. return err
  1796. }
  1797. registryAuthHeader := []string{
  1798. base64.URLEncoding.EncodeToString(buf),
  1799. }
  1800. if err = cli.stream("POST", "/images/create?"+v.Encode(), nil, out, map[string][]string{"X-Registry-Auth": registryAuthHeader}); err != nil {
  1801. return err
  1802. }
  1803. return nil
  1804. }
  1805. type cidFile struct {
  1806. path string
  1807. file *os.File
  1808. written bool
  1809. }
  1810. func newCIDFile(path string) (*cidFile, error) {
  1811. if _, err := os.Stat(path); err == nil {
  1812. return nil, fmt.Errorf("Container ID file found, make sure the other container isn't running or delete %s", path)
  1813. }
  1814. f, err := os.Create(path)
  1815. if err != nil {
  1816. return nil, fmt.Errorf("Failed to create the container ID file: %s", err)
  1817. }
  1818. return &cidFile{path: path, file: f}, nil
  1819. }
  1820. func (cid *cidFile) Close() error {
  1821. cid.file.Close()
  1822. if !cid.written {
  1823. if err := os.Remove(cid.path); err != nil {
  1824. return fmt.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err)
  1825. }
  1826. }
  1827. return nil
  1828. }
  1829. func (cid *cidFile) Write(id string) error {
  1830. if _, err := cid.file.Write([]byte(id)); err != nil {
  1831. return fmt.Errorf("Failed to write the container ID to the file: %s", err)
  1832. }
  1833. cid.written = true
  1834. return nil
  1835. }
  1836. func (cli *DockerCli) createContainer(config *runconfig.Config, hostConfig *runconfig.HostConfig, cidfile, name string) (engine.Env, error) {
  1837. containerValues := url.Values{}
  1838. if name != "" {
  1839. containerValues.Set("name", name)
  1840. }
  1841. mergedConfig := runconfig.MergeConfigs(config, hostConfig)
  1842. var containerIDFile *cidFile
  1843. if cidfile != "" {
  1844. var err error
  1845. if containerIDFile, err = newCIDFile(cidfile); err != nil {
  1846. return nil, err
  1847. }
  1848. defer containerIDFile.Close()
  1849. }
  1850. //create the container
  1851. stream, statusCode, err := cli.call("POST", "/containers/create?"+containerValues.Encode(), mergedConfig, false)
  1852. //if image not found try to pull it
  1853. if statusCode == 404 {
  1854. repo, tag := parsers.ParseRepositoryTag(config.Image)
  1855. if tag == "" {
  1856. tag = graph.DEFAULTTAG
  1857. }
  1858. fmt.Fprintf(cli.err, "Unable to find image '%s:%s' locally\n", repo, tag)
  1859. // we don't want to write to stdout anything apart from container.ID
  1860. if err = cli.pullImageCustomOut(config.Image, cli.err); err != nil {
  1861. return nil, err
  1862. }
  1863. // Retry
  1864. if stream, _, err = cli.call("POST", "/containers/create?"+containerValues.Encode(), mergedConfig, false); err != nil {
  1865. return nil, err
  1866. }
  1867. } else if err != nil {
  1868. return nil, err
  1869. }
  1870. var result engine.Env
  1871. if err := result.Decode(stream); err != nil {
  1872. return nil, err
  1873. }
  1874. for _, warning := range result.GetList("Warnings") {
  1875. fmt.Fprintf(cli.err, "WARNING: %s\n", warning)
  1876. }
  1877. if containerIDFile != nil {
  1878. if err = containerIDFile.Write(result.Get("Id")); err != nil {
  1879. return nil, err
  1880. }
  1881. }
  1882. return result, nil
  1883. }
  1884. func (cli *DockerCli) CmdCreate(args ...string) error {
  1885. cmd := cli.Subcmd("create", "IMAGE [COMMAND] [ARG...]", "Create a new container", true)
  1886. // These are flags not stored in Config/HostConfig
  1887. var (
  1888. flName = cmd.String([]string{"-name"}, "", "Assign a name to the container")
  1889. )
  1890. config, hostConfig, cmd, err := runconfig.Parse(cmd, args)
  1891. if err != nil {
  1892. utils.ReportError(cmd, err.Error(), true)
  1893. }
  1894. if config.Image == "" {
  1895. cmd.Usage()
  1896. return nil
  1897. }
  1898. createResult, err := cli.createContainer(config, hostConfig, hostConfig.ContainerIDFile, *flName)
  1899. if err != nil {
  1900. return err
  1901. }
  1902. fmt.Fprintf(cli.out, "%s\n", createResult.Get("Id"))
  1903. return nil
  1904. }
  1905. func (cli *DockerCli) CmdRun(args ...string) error {
  1906. // FIXME: just use runconfig.Parse already
  1907. cmd := cli.Subcmd("run", "IMAGE [COMMAND] [ARG...]", "Run a command in a new container", true)
  1908. // These are flags not stored in Config/HostConfig
  1909. var (
  1910. flAutoRemove = cmd.Bool([]string{"#rm", "-rm"}, false, "Automatically remove the container when it exits (incompatible with -d)")
  1911. flDetach = cmd.Bool([]string{"d", "-detach"}, false, "Detached mode: run the container in the background and print the new container ID")
  1912. flSigProxy = cmd.Bool([]string{"#sig-proxy", "-sig-proxy"}, true, "Proxy received signals to the process (non-TTY mode only). SIGCHLD, SIGSTOP, and SIGKILL are not proxied.")
  1913. flName = cmd.String([]string{"#name", "-name"}, "", "Assign a name to the container")
  1914. flAttach *opts.ListOpts
  1915. ErrConflictAttachDetach = fmt.Errorf("Conflicting options: -a and -d")
  1916. ErrConflictRestartPolicyAndAutoRemove = fmt.Errorf("Conflicting options: --restart and --rm")
  1917. ErrConflictDetachAutoRemove = fmt.Errorf("Conflicting options: --rm and -d")
  1918. )
  1919. config, hostConfig, cmd, err := runconfig.Parse(cmd, args)
  1920. // just in case the Parse does not exit
  1921. if err != nil {
  1922. utils.ReportError(cmd, err.Error(), true)
  1923. }
  1924. if config.Image == "" {
  1925. cmd.Usage()
  1926. return nil
  1927. }
  1928. if !*flDetach {
  1929. if err := cli.CheckTtyInput(config.AttachStdin, config.Tty); err != nil {
  1930. return err
  1931. }
  1932. } else {
  1933. if fl := cmd.Lookup("attach"); fl != nil {
  1934. flAttach = fl.Value.(*opts.ListOpts)
  1935. if flAttach.Len() != 0 {
  1936. return ErrConflictAttachDetach
  1937. }
  1938. }
  1939. if *flAutoRemove {
  1940. return ErrConflictDetachAutoRemove
  1941. }
  1942. config.AttachStdin = false
  1943. config.AttachStdout = false
  1944. config.AttachStderr = false
  1945. config.StdinOnce = false
  1946. }
  1947. // Disable flSigProxy when in TTY mode
  1948. sigProxy := *flSigProxy
  1949. if config.Tty {
  1950. sigProxy = false
  1951. }
  1952. runResult, err := cli.createContainer(config, hostConfig, hostConfig.ContainerIDFile, *flName)
  1953. if err != nil {
  1954. return err
  1955. }
  1956. if sigProxy {
  1957. sigc := cli.forwardAllSignals(runResult.Get("Id"))
  1958. defer signal.StopCatch(sigc)
  1959. }
  1960. var (
  1961. waitDisplayId chan struct{}
  1962. errCh chan error
  1963. )
  1964. if !config.AttachStdout && !config.AttachStderr {
  1965. // Make this asynchronous to allow the client to write to stdin before having to read the ID
  1966. waitDisplayId = make(chan struct{})
  1967. go func() {
  1968. defer close(waitDisplayId)
  1969. fmt.Fprintf(cli.out, "%s\n", runResult.Get("Id"))
  1970. }()
  1971. }
  1972. if *flAutoRemove && (hostConfig.RestartPolicy.Name == "always" || hostConfig.RestartPolicy.Name == "on-failure") {
  1973. return ErrConflictRestartPolicyAndAutoRemove
  1974. }
  1975. // We need to instantiate the chan because the select needs it. It can
  1976. // be closed but can't be uninitialized.
  1977. hijacked := make(chan io.Closer)
  1978. // Block the return until the chan gets closed
  1979. defer func() {
  1980. log.Debugf("End of CmdRun(), Waiting for hijack to finish.")
  1981. if _, ok := <-hijacked; ok {
  1982. log.Errorf("Hijack did not finish (chan still open)")
  1983. }
  1984. }()
  1985. if config.AttachStdin || config.AttachStdout || config.AttachStderr {
  1986. var (
  1987. out, stderr io.Writer
  1988. in io.ReadCloser
  1989. v = url.Values{}
  1990. )
  1991. v.Set("stream", "1")
  1992. if config.AttachStdin {
  1993. v.Set("stdin", "1")
  1994. in = cli.in
  1995. }
  1996. if config.AttachStdout {
  1997. v.Set("stdout", "1")
  1998. out = cli.out
  1999. }
  2000. if config.AttachStderr {
  2001. v.Set("stderr", "1")
  2002. if config.Tty {
  2003. stderr = cli.out
  2004. } else {
  2005. stderr = cli.err
  2006. }
  2007. }
  2008. errCh = promise.Go(func() error {
  2009. return cli.hijack("POST", "/containers/"+runResult.Get("Id")+"/attach?"+v.Encode(), config.Tty, in, out, stderr, hijacked, nil)
  2010. })
  2011. } else {
  2012. close(hijacked)
  2013. }
  2014. // Acknowledge the hijack before starting
  2015. select {
  2016. case closer := <-hijacked:
  2017. // Make sure that the hijack gets closed when returning (results
  2018. // in closing the hijack chan and freeing server's goroutines)
  2019. if closer != nil {
  2020. defer closer.Close()
  2021. }
  2022. case err := <-errCh:
  2023. if err != nil {
  2024. log.Debugf("Error hijack: %s", err)
  2025. return err
  2026. }
  2027. }
  2028. //start the container
  2029. if _, _, err = readBody(cli.call("POST", "/containers/"+runResult.Get("Id")+"/start", nil, false)); err != nil {
  2030. return err
  2031. }
  2032. if (config.AttachStdin || config.AttachStdout || config.AttachStderr) && config.Tty && cli.isTerminalOut {
  2033. if err := cli.monitorTtySize(runResult.Get("Id"), false); err != nil {
  2034. log.Errorf("Error monitoring TTY size: %s", err)
  2035. }
  2036. }
  2037. if errCh != nil {
  2038. if err := <-errCh; err != nil {
  2039. log.Debugf("Error hijack: %s", err)
  2040. return err
  2041. }
  2042. }
  2043. // Detached mode: wait for the id to be displayed and return.
  2044. if !config.AttachStdout && !config.AttachStderr {
  2045. // Detached mode
  2046. <-waitDisplayId
  2047. return nil
  2048. }
  2049. var status int
  2050. // Attached mode
  2051. if *flAutoRemove {
  2052. // Autoremove: wait for the container to finish, retrieve
  2053. // the exit code and remove the container
  2054. if _, _, err := readBody(cli.call("POST", "/containers/"+runResult.Get("Id")+"/wait", nil, false)); err != nil {
  2055. return err
  2056. }
  2057. if _, status, err = getExitCode(cli, runResult.Get("Id")); err != nil {
  2058. return err
  2059. }
  2060. if _, _, err := readBody(cli.call("DELETE", "/containers/"+runResult.Get("Id")+"?v=1", nil, false)); err != nil {
  2061. return err
  2062. }
  2063. } else {
  2064. // No Autoremove: Simply retrieve the exit code
  2065. if !config.Tty {
  2066. // In non-TTY mode, we can't detach, so we must wait for container exit
  2067. if status, err = waitForExit(cli, runResult.Get("Id")); err != nil {
  2068. return err
  2069. }
  2070. } else {
  2071. // In TTY mode, there is a race: if the process dies too slowly, the state could
  2072. // be updated after the getExitCode call and result in the wrong exit code being reported
  2073. if _, status, err = getExitCode(cli, runResult.Get("Id")); err != nil {
  2074. return err
  2075. }
  2076. }
  2077. }
  2078. if status != 0 {
  2079. return &utils.StatusError{StatusCode: status}
  2080. }
  2081. return nil
  2082. }
  2083. func (cli *DockerCli) CmdCp(args ...string) error {
  2084. cmd := cli.Subcmd("cp", "CONTAINER:PATH HOSTPATH", "Copy files/folders from the PATH to the HOSTPATH", true)
  2085. cmd.Require(flag.Exact, 2)
  2086. utils.ParseFlags(cmd, args, true)
  2087. var copyData engine.Env
  2088. info := strings.Split(cmd.Arg(0), ":")
  2089. if len(info) != 2 {
  2090. return fmt.Errorf("Error: Path not specified")
  2091. }
  2092. copyData.Set("Resource", info[1])
  2093. copyData.Set("HostPath", cmd.Arg(1))
  2094. stream, statusCode, err := cli.call("POST", "/containers/"+info[0]+"/copy", copyData, false)
  2095. if stream != nil {
  2096. defer stream.Close()
  2097. }
  2098. if statusCode == 404 {
  2099. return fmt.Errorf("No such container: %v", info[0])
  2100. }
  2101. if err != nil {
  2102. return err
  2103. }
  2104. if statusCode == 200 {
  2105. if err := archive.Untar(stream, copyData.Get("HostPath"), &archive.TarOptions{NoLchown: true}); err != nil {
  2106. return err
  2107. }
  2108. }
  2109. return nil
  2110. }
  2111. func (cli *DockerCli) CmdSave(args ...string) error {
  2112. cmd := cli.Subcmd("save", "IMAGE [IMAGE...]", "Save an image(s) to a tar archive (streamed to STDOUT by default)", true)
  2113. outfile := cmd.String([]string{"o", "-output"}, "", "Write to an file, instead of STDOUT")
  2114. cmd.Require(flag.Min, 1)
  2115. utils.ParseFlags(cmd, args, true)
  2116. var (
  2117. output io.Writer = cli.out
  2118. err error
  2119. )
  2120. if *outfile != "" {
  2121. output, err = os.Create(*outfile)
  2122. if err != nil {
  2123. return err
  2124. }
  2125. } else if cli.isTerminalOut {
  2126. return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
  2127. }
  2128. if len(cmd.Args()) == 1 {
  2129. image := cmd.Arg(0)
  2130. if err := cli.stream("GET", "/images/"+image+"/get", nil, output, nil); err != nil {
  2131. return err
  2132. }
  2133. } else {
  2134. v := url.Values{}
  2135. for _, arg := range cmd.Args() {
  2136. v.Add("names", arg)
  2137. }
  2138. if err := cli.stream("GET", "/images/get?"+v.Encode(), nil, output, nil); err != nil {
  2139. return err
  2140. }
  2141. }
  2142. return nil
  2143. }
  2144. func (cli *DockerCli) CmdLoad(args ...string) error {
  2145. cmd := cli.Subcmd("load", "", "Load an image from a tar archive on STDIN", true)
  2146. infile := cmd.String([]string{"i", "-input"}, "", "Read from a tar archive file, instead of STDIN")
  2147. cmd.Require(flag.Exact, 0)
  2148. utils.ParseFlags(cmd, args, true)
  2149. var (
  2150. input io.Reader = cli.in
  2151. err error
  2152. )
  2153. if *infile != "" {
  2154. input, err = os.Open(*infile)
  2155. if err != nil {
  2156. return err
  2157. }
  2158. }
  2159. if err := cli.stream("POST", "/images/load", input, cli.out, nil); err != nil {
  2160. return err
  2161. }
  2162. return nil
  2163. }
  2164. func (cli *DockerCli) CmdExec(args ...string) error {
  2165. cmd := cli.Subcmd("exec", "CONTAINER COMMAND [ARG...]", "Run a command in a running container", true)
  2166. execConfig, err := runconfig.ParseExec(cmd, args)
  2167. // just in case the ParseExec does not exit
  2168. if execConfig.Container == "" || err != nil {
  2169. return &utils.StatusError{StatusCode: 1}
  2170. }
  2171. stream, _, err := cli.call("POST", "/containers/"+execConfig.Container+"/exec", execConfig, false)
  2172. if err != nil {
  2173. return err
  2174. }
  2175. var execResult engine.Env
  2176. if err := execResult.Decode(stream); err != nil {
  2177. return err
  2178. }
  2179. execID := execResult.Get("Id")
  2180. if execID == "" {
  2181. fmt.Fprintf(cli.out, "exec ID empty")
  2182. return nil
  2183. }
  2184. if !execConfig.Detach {
  2185. if err := cli.CheckTtyInput(execConfig.AttachStdin, execConfig.Tty); err != nil {
  2186. return err
  2187. }
  2188. } else {
  2189. if _, _, err := readBody(cli.call("POST", "/exec/"+execID+"/start", execConfig, false)); err != nil {
  2190. return err
  2191. }
  2192. // For now don't print this - wait for when we support exec wait()
  2193. // fmt.Fprintf(cli.out, "%s\n", execID)
  2194. return nil
  2195. }
  2196. // Interactive exec requested.
  2197. var (
  2198. out, stderr io.Writer
  2199. in io.ReadCloser
  2200. hijacked = make(chan io.Closer)
  2201. errCh chan error
  2202. )
  2203. // Block the return until the chan gets closed
  2204. defer func() {
  2205. log.Debugf("End of CmdExec(), Waiting for hijack to finish.")
  2206. if _, ok := <-hijacked; ok {
  2207. log.Errorf("Hijack did not finish (chan still open)")
  2208. }
  2209. }()
  2210. if execConfig.AttachStdin {
  2211. in = cli.in
  2212. }
  2213. if execConfig.AttachStdout {
  2214. out = cli.out
  2215. }
  2216. if execConfig.AttachStderr {
  2217. if execConfig.Tty {
  2218. stderr = cli.out
  2219. } else {
  2220. stderr = cli.err
  2221. }
  2222. }
  2223. errCh = promise.Go(func() error {
  2224. return cli.hijack("POST", "/exec/"+execID+"/start", execConfig.Tty, in, out, stderr, hijacked, execConfig)
  2225. })
  2226. // Acknowledge the hijack before starting
  2227. select {
  2228. case closer := <-hijacked:
  2229. // Make sure that hijack gets closed when returning. (result
  2230. // in closing hijack chan and freeing server's goroutines.
  2231. if closer != nil {
  2232. defer closer.Close()
  2233. }
  2234. case err := <-errCh:
  2235. if err != nil {
  2236. log.Debugf("Error hijack: %s", err)
  2237. return err
  2238. }
  2239. }
  2240. if execConfig.Tty && cli.isTerminalIn {
  2241. if err := cli.monitorTtySize(execID, true); err != nil {
  2242. log.Errorf("Error monitoring TTY size: %s", err)
  2243. }
  2244. }
  2245. if err := <-errCh; err != nil {
  2246. log.Debugf("Error hijack: %s", err)
  2247. return err
  2248. }
  2249. var status int
  2250. if _, status, err = getExecExitCode(cli, execID); err != nil {
  2251. return err
  2252. }
  2253. if status != 0 {
  2254. return &utils.StatusError{StatusCode: status}
  2255. }
  2256. return nil
  2257. }