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