internals.go 19 KB

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