commands.go 76 KB

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