internals.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. package dockerfile
  2. // internals for handling commands. Covers many areas and a lot of
  3. // non-contiguous functionality. Please read the comments.
  4. import (
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "net/url"
  11. "os"
  12. "path/filepath"
  13. "runtime"
  14. "sort"
  15. "strings"
  16. "time"
  17. "github.com/Sirupsen/logrus"
  18. "github.com/docker/docker/api/types"
  19. "github.com/docker/docker/api/types/backend"
  20. "github.com/docker/docker/api/types/container"
  21. "github.com/docker/docker/api/types/strslice"
  22. "github.com/docker/docker/builder"
  23. "github.com/docker/docker/builder/dockerfile/parser"
  24. "github.com/docker/docker/builder/remotecontext"
  25. "github.com/docker/docker/pkg/httputils"
  26. "github.com/docker/docker/pkg/ioutils"
  27. "github.com/docker/docker/pkg/jsonmessage"
  28. "github.com/docker/docker/pkg/progress"
  29. "github.com/docker/docker/pkg/streamformatter"
  30. "github.com/docker/docker/pkg/stringid"
  31. "github.com/docker/docker/pkg/system"
  32. "github.com/docker/docker/pkg/urlutil"
  33. "github.com/pkg/errors"
  34. )
  35. func (b *Builder) commit(comment string) error {
  36. if b.disableCommit {
  37. return nil
  38. }
  39. if !b.hasFromImage() {
  40. return errors.New("Please provide a source image with `from` prior to commit")
  41. }
  42. // TODO: why is this set here?
  43. b.runConfig.Image = b.image
  44. runConfigWithCommentCmd := copyRunConfig(b.runConfig, withCmdComment(comment))
  45. hit, err := b.probeCache(b.image, runConfigWithCommentCmd)
  46. if err != nil || hit {
  47. return err
  48. }
  49. id, err := b.create(runConfigWithCommentCmd)
  50. if err != nil {
  51. return err
  52. }
  53. return b.commitContainer(id, b.runConfig)
  54. }
  55. func (b *Builder) commitContainer(id string, runConfig *container.Config) error {
  56. if b.disableCommit {
  57. return nil
  58. }
  59. commitCfg := &backend.ContainerCommitConfig{
  60. ContainerCommitConfig: types.ContainerCommitConfig{
  61. Author: b.maintainer,
  62. Pause: true,
  63. Config: runConfig,
  64. },
  65. }
  66. // Commit the container
  67. imageID, err := b.docker.Commit(id, commitCfg)
  68. if err != nil {
  69. return err
  70. }
  71. // TODO: this function should return imageID and runConfig instead of setting
  72. // then on the builder
  73. b.image = imageID
  74. b.imageContexts.update(imageID, runConfig)
  75. return nil
  76. }
  77. type copyInfo struct {
  78. root string
  79. path string
  80. hash string
  81. decompress bool
  82. }
  83. func (b *Builder) runContextCommand(args []string, allowRemote bool, allowLocalDecompression bool, cmdName string, imageSource *imageMount) error {
  84. if len(args) < 2 {
  85. return fmt.Errorf("Invalid %s format - at least two arguments required", cmdName)
  86. }
  87. // Work in daemon-specific filepath semantics
  88. dest := filepath.FromSlash(args[len(args)-1]) // last one is always the dest
  89. b.runConfig.Image = b.image
  90. var infos []copyInfo
  91. // Loop through each src file and calculate the info we need to
  92. // do the copy (e.g. hash value if cached). Don't actually do
  93. // the copy until we've looked at all src files
  94. var err error
  95. for _, orig := range args[0 : len(args)-1] {
  96. if urlutil.IsURL(orig) {
  97. if !allowRemote {
  98. return fmt.Errorf("Source can't be a URL for %s", cmdName)
  99. }
  100. remote, path, err := b.download(orig)
  101. if err != nil {
  102. return err
  103. }
  104. defer os.RemoveAll(remote.Root())
  105. h, err := remote.Hash(path)
  106. if err != nil {
  107. return err
  108. }
  109. infos = append(infos, copyInfo{
  110. root: remote.Root(),
  111. path: path,
  112. hash: h,
  113. })
  114. continue
  115. }
  116. // not a URL
  117. subInfos, err := b.calcCopyInfo(cmdName, orig, allowLocalDecompression, true, imageSource)
  118. if err != nil {
  119. return err
  120. }
  121. infos = append(infos, subInfos...)
  122. }
  123. if len(infos) == 0 {
  124. return errors.New("No source files were specified")
  125. }
  126. if len(infos) > 1 && !strings.HasSuffix(dest, string(os.PathSeparator)) {
  127. return fmt.Errorf("When using %s with more than one source file, the destination must be a directory and end with a /", cmdName)
  128. }
  129. // For backwards compat, if there's just one info then use it as the
  130. // cache look-up string, otherwise hash 'em all into one
  131. var srcHash string
  132. if len(infos) == 1 {
  133. info := infos[0]
  134. srcHash = info.hash
  135. } else {
  136. var hashs []string
  137. var origs []string
  138. for _, info := range infos {
  139. origs = append(origs, info.path)
  140. hashs = append(hashs, info.hash)
  141. }
  142. hasher := sha256.New()
  143. hasher.Write([]byte(strings.Join(hashs, ",")))
  144. srcHash = "multi:" + hex.EncodeToString(hasher.Sum(nil))
  145. }
  146. cmd := b.runConfig.Cmd
  147. // TODO: should this have been using origPaths instead of srcHash in the comment?
  148. b.runConfig.Cmd = strslice.StrSlice(append(getShell(b.runConfig), fmt.Sprintf("#(nop) %s %s in %s ", cmdName, srcHash, dest)))
  149. defer func(cmd strslice.StrSlice) { b.runConfig.Cmd = cmd }(cmd)
  150. // TODO: this should pass a copy of runConfig
  151. if hit, err := b.probeCache(b.image, b.runConfig); err != nil || hit {
  152. return err
  153. }
  154. container, err := b.docker.ContainerCreate(types.ContainerCreateConfig{
  155. Config: b.runConfig,
  156. // Set a log config to override any default value set on the daemon
  157. HostConfig: &container.HostConfig{LogConfig: defaultLogConfig},
  158. })
  159. if err != nil {
  160. return err
  161. }
  162. b.tmpContainers[container.ID] = struct{}{}
  163. // Twiddle the destination when it's a relative path - meaning, make it
  164. // relative to the WORKINGDIR
  165. if dest, err = normaliseDest(cmdName, b.runConfig.WorkingDir, dest); err != nil {
  166. return err
  167. }
  168. for _, info := range infos {
  169. if err := b.docker.CopyOnBuild(container.ID, dest, info.root, info.path, info.decompress); err != nil {
  170. return err
  171. }
  172. }
  173. return b.commitContainer(container.ID, copyRunConfig(b.runConfig, withCmd(cmd)))
  174. }
  175. type runConfigModifier func(*container.Config)
  176. func copyRunConfig(runConfig *container.Config, modifiers ...runConfigModifier) *container.Config {
  177. copy := *runConfig
  178. for _, modifier := range modifiers {
  179. modifier(&copy)
  180. }
  181. return &copy
  182. }
  183. func withCmd(cmd []string) runConfigModifier {
  184. return func(runConfig *container.Config) {
  185. runConfig.Cmd = cmd
  186. }
  187. }
  188. func withCmdComment(comment string) runConfigModifier {
  189. return func(runConfig *container.Config) {
  190. runConfig.Cmd = append(getShell(runConfig), "#(nop) ", comment)
  191. }
  192. }
  193. func withEnv(env []string) runConfigModifier {
  194. return func(runConfig *container.Config) {
  195. runConfig.Env = env
  196. }
  197. }
  198. // getShell is a helper function which gets the right shell for prefixing the
  199. // shell-form of RUN, ENTRYPOINT and CMD instructions
  200. func getShell(c *container.Config) []string {
  201. if 0 == len(c.Shell) {
  202. return append([]string{}, defaultShell[:]...)
  203. }
  204. return append([]string{}, c.Shell[:]...)
  205. }
  206. func (b *Builder) download(srcURL string) (remote builder.Source, p string, err error) {
  207. // get filename from URL
  208. u, err := url.Parse(srcURL)
  209. if err != nil {
  210. return
  211. }
  212. path := filepath.FromSlash(u.Path) // Ensure in platform semantics
  213. if strings.HasSuffix(path, string(os.PathSeparator)) {
  214. path = path[:len(path)-1]
  215. }
  216. parts := strings.Split(path, string(os.PathSeparator))
  217. filename := parts[len(parts)-1]
  218. if filename == "" {
  219. err = fmt.Errorf("cannot determine filename from url: %s", u)
  220. return
  221. }
  222. // Initiate the download
  223. resp, err := httputils.Download(srcURL)
  224. if err != nil {
  225. return
  226. }
  227. // Prepare file in a tmp dir
  228. tmpDir, err := ioutils.TempDir("", "docker-remote")
  229. if err != nil {
  230. return
  231. }
  232. defer func() {
  233. if err != nil {
  234. os.RemoveAll(tmpDir)
  235. }
  236. }()
  237. tmpFileName := filepath.Join(tmpDir, filename)
  238. tmpFile, err := os.OpenFile(tmpFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
  239. if err != nil {
  240. return
  241. }
  242. stdoutFormatter := b.Stdout.(*streamformatter.StdoutFormatter)
  243. progressOutput := stdoutFormatter.StreamFormatter.NewProgressOutput(stdoutFormatter.Writer, true)
  244. progressReader := progress.NewProgressReader(resp.Body, progressOutput, resp.ContentLength, "", "Downloading")
  245. // Download and dump result to tmp file
  246. // TODO: add filehash directly
  247. if _, err = io.Copy(tmpFile, progressReader); err != nil {
  248. tmpFile.Close()
  249. return
  250. }
  251. fmt.Fprintln(b.Stdout)
  252. // Set the mtime to the Last-Modified header value if present
  253. // Otherwise just remove atime and mtime
  254. mTime := time.Time{}
  255. lastMod := resp.Header.Get("Last-Modified")
  256. if lastMod != "" {
  257. // If we can't parse it then just let it default to 'zero'
  258. // otherwise use the parsed time value
  259. if parsedMTime, err := http.ParseTime(lastMod); err == nil {
  260. mTime = parsedMTime
  261. }
  262. }
  263. tmpFile.Close()
  264. if err = system.Chtimes(tmpFileName, mTime, mTime); err != nil {
  265. return
  266. }
  267. lc, err := remotecontext.NewLazyContext(tmpDir)
  268. if err != nil {
  269. return
  270. }
  271. return lc, filename, nil
  272. }
  273. var windowsBlacklist = map[string]bool{
  274. "c:\\": true,
  275. "c:\\windows": true,
  276. }
  277. func (b *Builder) calcCopyInfo(cmdName, origPath string, allowLocalDecompression, allowWildcards bool, imageSource *imageMount) ([]copyInfo, error) {
  278. // Work in daemon-specific OS filepath semantics
  279. origPath = filepath.FromSlash(origPath)
  280. // validate windows paths from other images
  281. if imageSource != nil && runtime.GOOS == "windows" {
  282. p := strings.ToLower(filepath.Clean(origPath))
  283. if !filepath.IsAbs(p) {
  284. if filepath.VolumeName(p) != "" {
  285. if p[len(p)-2:] == ":." { // case where clean returns weird c:. paths
  286. p = p[:len(p)-1]
  287. }
  288. p += "\\"
  289. } else {
  290. p = filepath.Join("c:\\", p)
  291. }
  292. }
  293. if _, blacklisted := windowsBlacklist[p]; blacklisted {
  294. return nil, errors.New("copy from c:\\ or c:\\windows is not allowed on windows")
  295. }
  296. }
  297. if origPath != "" && origPath[0] == os.PathSeparator && len(origPath) > 1 {
  298. origPath = origPath[1:]
  299. }
  300. origPath = strings.TrimPrefix(origPath, "."+string(os.PathSeparator))
  301. source := b.source
  302. var err error
  303. if imageSource != nil {
  304. source, err = imageSource.context()
  305. if err != nil {
  306. return nil, err
  307. }
  308. }
  309. if source == nil {
  310. return nil, errors.Errorf("No context given. Impossible to use %s", cmdName)
  311. }
  312. // Deal with wildcards
  313. if allowWildcards && containsWildcards(origPath) {
  314. var copyInfos []copyInfo
  315. if err := filepath.Walk(source.Root(), func(path string, info os.FileInfo, err error) error {
  316. if err != nil {
  317. return err
  318. }
  319. rel, err := remotecontext.Rel(source.Root(), path)
  320. if err != nil {
  321. return err
  322. }
  323. if rel == "." {
  324. return nil
  325. }
  326. if match, _ := filepath.Match(origPath, rel); !match {
  327. return nil
  328. }
  329. // Note we set allowWildcards to false in case the name has
  330. // a * in it
  331. subInfos, err := b.calcCopyInfo(cmdName, rel, allowLocalDecompression, false, imageSource)
  332. if err != nil {
  333. return err
  334. }
  335. copyInfos = append(copyInfos, subInfos...)
  336. return nil
  337. }); err != nil {
  338. return nil, err
  339. }
  340. return copyInfos, nil
  341. }
  342. // Must be a dir or a file
  343. hash, err := source.Hash(origPath)
  344. if err != nil {
  345. return nil, err
  346. }
  347. fi, err := remotecontext.StatAt(source, origPath)
  348. if err != nil {
  349. return nil, err
  350. }
  351. // TODO: remove, handle dirs in Hash()
  352. copyInfos := []copyInfo{{root: source.Root(), path: origPath, hash: hash, decompress: allowLocalDecompression}}
  353. if imageSource != nil {
  354. // fast-cache based on imageID
  355. if h, ok := b.imageContexts.getCache(imageSource.id, origPath); ok {
  356. copyInfos[0].hash = h.(string)
  357. return copyInfos, nil
  358. }
  359. }
  360. // Deal with the single file case
  361. if !fi.IsDir() {
  362. copyInfos[0].hash = "file:" + copyInfos[0].hash
  363. return copyInfos, nil
  364. }
  365. fp, err := remotecontext.FullPath(source, origPath)
  366. if err != nil {
  367. return nil, err
  368. }
  369. // Must be a dir
  370. var subfiles []string
  371. err = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error {
  372. if err != nil {
  373. return err
  374. }
  375. rel, err := remotecontext.Rel(source.Root(), path)
  376. if err != nil {
  377. return err
  378. }
  379. if rel == "." {
  380. return nil
  381. }
  382. hash, err := source.Hash(rel)
  383. if err != nil {
  384. return nil
  385. }
  386. // we already checked handleHash above
  387. subfiles = append(subfiles, hash)
  388. return nil
  389. })
  390. if err != nil {
  391. return nil, err
  392. }
  393. sort.Strings(subfiles)
  394. hasher := sha256.New()
  395. hasher.Write([]byte(strings.Join(subfiles, ",")))
  396. copyInfos[0].hash = "dir:" + hex.EncodeToString(hasher.Sum(nil))
  397. if imageSource != nil {
  398. b.imageContexts.setCache(imageSource.id, origPath, copyInfos[0].hash)
  399. }
  400. return copyInfos, nil
  401. }
  402. func (b *Builder) processImageFrom(img builder.Image) error {
  403. if img != nil {
  404. b.image = img.ImageID()
  405. if img.RunConfig() != nil {
  406. b.runConfig = img.RunConfig()
  407. }
  408. }
  409. // Check to see if we have a default PATH, note that windows won't
  410. // have one as it's set by HCS
  411. if system.DefaultPathEnv != "" {
  412. if _, ok := b.runConfigEnvMapping()["PATH"]; !ok {
  413. b.runConfig.Env = append(b.runConfig.Env,
  414. "PATH="+system.DefaultPathEnv)
  415. }
  416. }
  417. if img == nil {
  418. // Typically this means they used "FROM scratch"
  419. return nil
  420. }
  421. // Process ONBUILD triggers if they exist
  422. if nTriggers := len(b.runConfig.OnBuild); nTriggers != 0 {
  423. word := "trigger"
  424. if nTriggers > 1 {
  425. word = "triggers"
  426. }
  427. fmt.Fprintf(b.Stderr, "# Executing %d build %s...\n", nTriggers, word)
  428. }
  429. // Copy the ONBUILD triggers, and remove them from the config, since the config will be committed.
  430. onBuildTriggers := b.runConfig.OnBuild
  431. b.runConfig.OnBuild = []string{}
  432. // Reset stdin settings as all build actions run without stdin
  433. b.runConfig.OpenStdin = false
  434. b.runConfig.StdinOnce = false
  435. // parse the ONBUILD triggers by invoking the parser
  436. for _, step := range onBuildTriggers {
  437. result, err := parser.Parse(strings.NewReader(step))
  438. if err != nil {
  439. return err
  440. }
  441. for _, n := range result.AST.Children {
  442. if err := checkDispatch(n); err != nil {
  443. return err
  444. }
  445. upperCasedCmd := strings.ToUpper(n.Value)
  446. switch upperCasedCmd {
  447. case "ONBUILD":
  448. return errors.New("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
  449. case "MAINTAINER", "FROM":
  450. return errors.Errorf("%s isn't allowed as an ONBUILD trigger", upperCasedCmd)
  451. }
  452. }
  453. if err := dispatchFromDockerfile(b, result); err != nil {
  454. return err
  455. }
  456. }
  457. return nil
  458. }
  459. // probeCache checks if cache match can be found for current build instruction.
  460. // If an image is found, probeCache returns `(true, nil)`.
  461. // If no image is found, it returns `(false, nil)`.
  462. // If there is any error, it returns `(false, err)`.
  463. func (b *Builder) probeCache(imageID string, runConfig *container.Config) (bool, error) {
  464. c := b.imageCache
  465. if c == nil || b.options.NoCache || b.cacheBusted {
  466. return false, nil
  467. }
  468. cache, err := c.GetCache(imageID, runConfig)
  469. if err != nil {
  470. return false, err
  471. }
  472. if len(cache) == 0 {
  473. logrus.Debugf("[BUILDER] Cache miss: %s", runConfig.Cmd)
  474. b.cacheBusted = true
  475. return false, nil
  476. }
  477. fmt.Fprint(b.Stdout, " ---> Using cache\n")
  478. logrus.Debugf("[BUILDER] Use cached version: %s", runConfig.Cmd)
  479. b.image = string(cache)
  480. b.imageContexts.update(b.image, runConfig)
  481. return true, nil
  482. }
  483. func (b *Builder) create(runConfig *container.Config) (string, error) {
  484. if !b.hasFromImage() {
  485. return "", errors.New("Please provide a source image with `from` prior to run")
  486. }
  487. resources := container.Resources{
  488. CgroupParent: b.options.CgroupParent,
  489. CPUShares: b.options.CPUShares,
  490. CPUPeriod: b.options.CPUPeriod,
  491. CPUQuota: b.options.CPUQuota,
  492. CpusetCpus: b.options.CPUSetCPUs,
  493. CpusetMems: b.options.CPUSetMems,
  494. Memory: b.options.Memory,
  495. MemorySwap: b.options.MemorySwap,
  496. Ulimits: b.options.Ulimits,
  497. }
  498. // TODO: why not embed a hostconfig in builder?
  499. hostConfig := &container.HostConfig{
  500. SecurityOpt: b.options.SecurityOpt,
  501. Isolation: b.options.Isolation,
  502. ShmSize: b.options.ShmSize,
  503. Resources: resources,
  504. NetworkMode: container.NetworkMode(b.options.NetworkMode),
  505. // Set a log config to override any default value set on the daemon
  506. LogConfig: defaultLogConfig,
  507. ExtraHosts: b.options.ExtraHosts,
  508. }
  509. // Create the container
  510. c, err := b.docker.ContainerCreate(types.ContainerCreateConfig{
  511. Config: runConfig,
  512. HostConfig: hostConfig,
  513. })
  514. if err != nil {
  515. return "", err
  516. }
  517. for _, warning := range c.Warnings {
  518. fmt.Fprintf(b.Stdout, " ---> [Warning] %s\n", warning)
  519. }
  520. b.tmpContainers[c.ID] = struct{}{}
  521. fmt.Fprintf(b.Stdout, " ---> Running in %s\n", stringid.TruncateID(c.ID))
  522. // override the entry point that may have been picked up from the base image
  523. if err := b.docker.ContainerUpdateCmdOnBuild(c.ID, runConfig.Cmd); err != nil {
  524. return "", err
  525. }
  526. return c.ID, nil
  527. }
  528. var errCancelled = errors.New("build cancelled")
  529. func (b *Builder) run(cID string) (err error) {
  530. attached := make(chan struct{})
  531. errCh := make(chan error)
  532. go func() {
  533. errCh <- b.docker.ContainerAttachRaw(cID, nil, b.Stdout, b.Stderr, true, attached)
  534. }()
  535. select {
  536. case err := <-errCh:
  537. return err
  538. case <-attached:
  539. }
  540. finished := make(chan struct{})
  541. cancelErrCh := make(chan error, 1)
  542. go func() {
  543. select {
  544. case <-b.clientCtx.Done():
  545. logrus.Debugln("Build cancelled, killing and removing container:", cID)
  546. b.docker.ContainerKill(cID, 0)
  547. b.removeContainer(cID)
  548. cancelErrCh <- errCancelled
  549. case <-finished:
  550. cancelErrCh <- nil
  551. }
  552. }()
  553. if err := b.docker.ContainerStart(cID, nil, "", ""); err != nil {
  554. close(finished)
  555. if cancelErr := <-cancelErrCh; cancelErr != nil {
  556. logrus.Debugf("Build cancelled (%v) and got an error from ContainerStart: %v",
  557. cancelErr, err)
  558. }
  559. return err
  560. }
  561. // Block on reading output from container, stop on err or chan closed
  562. if err := <-errCh; err != nil {
  563. close(finished)
  564. if cancelErr := <-cancelErrCh; cancelErr != nil {
  565. logrus.Debugf("Build cancelled (%v) and got an error from errCh: %v",
  566. cancelErr, err)
  567. }
  568. return err
  569. }
  570. if ret, _ := b.docker.ContainerWait(cID, -1); ret != 0 {
  571. close(finished)
  572. if cancelErr := <-cancelErrCh; cancelErr != nil {
  573. logrus.Debugf("Build cancelled (%v) and got a non-zero code from ContainerWait: %d",
  574. cancelErr, ret)
  575. }
  576. // TODO: change error type, because jsonmessage.JSONError assumes HTTP
  577. return &jsonmessage.JSONError{
  578. Message: fmt.Sprintf("The command '%s' returned a non-zero code: %d", strings.Join(b.runConfig.Cmd, " "), ret),
  579. Code: ret,
  580. }
  581. }
  582. close(finished)
  583. return <-cancelErrCh
  584. }
  585. func (b *Builder) removeContainer(c string) error {
  586. rmConfig := &types.ContainerRmConfig{
  587. ForceRemove: true,
  588. RemoveVolume: true,
  589. }
  590. if err := b.docker.ContainerRm(c, rmConfig); err != nil {
  591. fmt.Fprintf(b.Stdout, "Error removing intermediate container %s: %v\n", stringid.TruncateID(c), err)
  592. return err
  593. }
  594. return nil
  595. }
  596. func (b *Builder) clearTmp() {
  597. for c := range b.tmpContainers {
  598. if err := b.removeContainer(c); err != nil {
  599. return
  600. }
  601. delete(b.tmpContainers, c)
  602. fmt.Fprintf(b.Stdout, "Removing intermediate container %s\n", stringid.TruncateID(c))
  603. }
  604. }