build.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. package client
  2. import (
  3. "archive/tar"
  4. "bufio"
  5. "bytes"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. "regexp"
  13. "runtime"
  14. "strings"
  15. "github.com/docker/docker/api"
  16. "github.com/docker/docker/api/types"
  17. "github.com/docker/docker/builder/dockerignore"
  18. Cli "github.com/docker/docker/cli"
  19. "github.com/docker/docker/opts"
  20. "github.com/docker/docker/pkg/archive"
  21. "github.com/docker/docker/pkg/fileutils"
  22. "github.com/docker/docker/pkg/gitutils"
  23. "github.com/docker/docker/pkg/httputils"
  24. "github.com/docker/docker/pkg/jsonmessage"
  25. flag "github.com/docker/docker/pkg/mflag"
  26. "github.com/docker/docker/pkg/progress"
  27. "github.com/docker/docker/pkg/streamformatter"
  28. "github.com/docker/docker/pkg/urlutil"
  29. "github.com/docker/docker/reference"
  30. runconfigopts "github.com/docker/docker/runconfig/opts"
  31. "github.com/docker/go-units"
  32. )
  33. // CmdBuild builds a new image from the source code at a given path.
  34. //
  35. // If '-' is provided instead of a path or URL, Docker will build an image from either a Dockerfile or tar archive read from STDIN.
  36. //
  37. // Usage: docker build [OPTIONS] PATH | URL | -
  38. func (cli *DockerCli) CmdBuild(args ...string) error {
  39. cmd := Cli.Subcmd("build", []string{"PATH | URL | -"}, Cli.DockerCommands["build"].Description, true)
  40. flTags := opts.NewListOpts(validateTag)
  41. cmd.Var(&flTags, []string{"t", "-tag"}, "Name and optionally a tag in the 'name:tag' format")
  42. suppressOutput := cmd.Bool([]string{"q", "-quiet"}, false, "Suppress the build output and print image ID on success")
  43. noCache := cmd.Bool([]string{"-no-cache"}, false, "Do not use cache when building the image")
  44. rm := cmd.Bool([]string{"-rm"}, true, "Remove intermediate containers after a successful build")
  45. forceRm := cmd.Bool([]string{"-force-rm"}, false, "Always remove intermediate containers")
  46. pull := cmd.Bool([]string{"-pull"}, false, "Always attempt to pull a newer version of the image")
  47. dockerfileName := cmd.String([]string{"f", "-file"}, "", "Name of the Dockerfile (Default is 'PATH/Dockerfile')")
  48. flMemoryString := cmd.String([]string{"m", "-memory"}, "", "Memory limit")
  49. flMemorySwap := cmd.String([]string{"-memory-swap"}, "", "Swap limit equal to memory plus swap: '-1' to enable unlimited swap")
  50. flShmSize := cmd.String([]string{"-shm-size"}, "", "Size of /dev/shm, default value is 64MB")
  51. flCPUShares := cmd.Int64([]string{"#c", "-cpu-shares"}, 0, "CPU shares (relative weight)")
  52. flCPUPeriod := cmd.Int64([]string{"-cpu-period"}, 0, "Limit the CPU CFS (Completely Fair Scheduler) period")
  53. flCPUQuota := cmd.Int64([]string{"-cpu-quota"}, 0, "Limit the CPU CFS (Completely Fair Scheduler) quota")
  54. flCPUSetCpus := cmd.String([]string{"-cpuset-cpus"}, "", "CPUs in which to allow execution (0-3, 0,1)")
  55. flCPUSetMems := cmd.String([]string{"-cpuset-mems"}, "", "MEMs in which to allow execution (0-3, 0,1)")
  56. flCgroupParent := cmd.String([]string{"-cgroup-parent"}, "", "Optional parent cgroup for the container")
  57. flBuildArg := opts.NewListOpts(opts.ValidateEnv)
  58. cmd.Var(&flBuildArg, []string{"-build-arg"}, "Set build-time variables")
  59. isolation := cmd.String([]string{"-isolation"}, "", "Container isolation level")
  60. ulimits := make(map[string]*units.Ulimit)
  61. flUlimits := runconfigopts.NewUlimitOpt(&ulimits)
  62. cmd.Var(flUlimits, []string{"-ulimit"}, "Ulimit options")
  63. cmd.Require(flag.Exact, 1)
  64. // For trusted pull on "FROM <image>" instruction.
  65. addTrustedFlags(cmd, true)
  66. cmd.ParseFlags(args, true)
  67. var (
  68. context io.ReadCloser
  69. isRemote bool
  70. err error
  71. )
  72. _, err = exec.LookPath("git")
  73. hasGit := err == nil
  74. specifiedContext := cmd.Arg(0)
  75. var (
  76. contextDir string
  77. tempDir string
  78. relDockerfile string
  79. progBuff io.Writer
  80. buildBuff io.Writer
  81. )
  82. progBuff = cli.out
  83. buildBuff = cli.out
  84. if *suppressOutput {
  85. progBuff = bytes.NewBuffer(nil)
  86. buildBuff = bytes.NewBuffer(nil)
  87. }
  88. switch {
  89. case specifiedContext == "-":
  90. tempDir, relDockerfile, err = getContextFromReader(cli.in, *dockerfileName)
  91. case urlutil.IsGitURL(specifiedContext) && hasGit:
  92. tempDir, relDockerfile, err = getContextFromGitURL(specifiedContext, *dockerfileName)
  93. case urlutil.IsURL(specifiedContext):
  94. tempDir, relDockerfile, err = getContextFromURL(progBuff, specifiedContext, *dockerfileName)
  95. default:
  96. contextDir, relDockerfile, err = getContextFromLocalDir(specifiedContext, *dockerfileName)
  97. }
  98. if err != nil {
  99. if *suppressOutput && urlutil.IsURL(specifiedContext) {
  100. fmt.Fprintln(cli.err, progBuff)
  101. }
  102. return fmt.Errorf("unable to prepare context: %s", err)
  103. }
  104. if tempDir != "" {
  105. defer os.RemoveAll(tempDir)
  106. contextDir = tempDir
  107. }
  108. // And canonicalize dockerfile name to a platform-independent one
  109. relDockerfile, err = archive.CanonicalTarNameForPath(relDockerfile)
  110. if err != nil {
  111. return fmt.Errorf("cannot canonicalize dockerfile path %s: %v", relDockerfile, err)
  112. }
  113. f, err := os.Open(filepath.Join(contextDir, ".dockerignore"))
  114. if err != nil && !os.IsNotExist(err) {
  115. return err
  116. }
  117. var excludes []string
  118. if err == nil {
  119. excludes, err = dockerignore.ReadAll(f)
  120. if err != nil {
  121. return err
  122. }
  123. }
  124. if err := validateContextDirectory(contextDir, excludes); err != nil {
  125. return fmt.Errorf("Error checking context: '%s'.", err)
  126. }
  127. // If .dockerignore mentions .dockerignore or the Dockerfile
  128. // then make sure we send both files over to the daemon
  129. // because Dockerfile is, obviously, needed no matter what, and
  130. // .dockerignore is needed to know if either one needs to be
  131. // removed. The daemon will remove them for us, if needed, after it
  132. // parses the Dockerfile. Ignore errors here, as they will have been
  133. // caught by validateContextDirectory above.
  134. var includes = []string{"."}
  135. keepThem1, _ := fileutils.Matches(".dockerignore", excludes)
  136. keepThem2, _ := fileutils.Matches(relDockerfile, excludes)
  137. if keepThem1 || keepThem2 {
  138. includes = append(includes, ".dockerignore", relDockerfile)
  139. }
  140. context, err = archive.TarWithOptions(contextDir, &archive.TarOptions{
  141. Compression: archive.Uncompressed,
  142. ExcludePatterns: excludes,
  143. IncludeFiles: includes,
  144. })
  145. if err != nil {
  146. return err
  147. }
  148. var resolvedTags []*resolvedTag
  149. if isTrusted() {
  150. // Resolve the FROM lines in the Dockerfile to trusted digest references
  151. // using Notary. On a successful build, we must tag the resolved digests
  152. // to the original name specified in the Dockerfile.
  153. var newDockerfile *trustedDockerfile
  154. newDockerfile, resolvedTags, err = rewriteDockerfileFrom(filepath.Join(contextDir, relDockerfile), cli.trustedReference)
  155. if err != nil {
  156. return fmt.Errorf("unable to process Dockerfile: %v", err)
  157. }
  158. defer newDockerfile.Close()
  159. // Wrap the tar archive to replace the Dockerfile entry with the rewritten
  160. // Dockerfile which uses trusted pulls.
  161. context = replaceDockerfileTarWrapper(context, newDockerfile, relDockerfile)
  162. }
  163. // Setup an upload progress bar
  164. progressOutput := streamformatter.NewStreamFormatter().NewProgressOutput(progBuff, true)
  165. var body io.Reader = progress.NewProgressReader(context, progressOutput, 0, "", "Sending build context to Docker daemon")
  166. var memory int64
  167. if *flMemoryString != "" {
  168. parsedMemory, err := units.RAMInBytes(*flMemoryString)
  169. if err != nil {
  170. return err
  171. }
  172. memory = parsedMemory
  173. }
  174. var memorySwap int64
  175. if *flMemorySwap != "" {
  176. if *flMemorySwap == "-1" {
  177. memorySwap = -1
  178. } else {
  179. parsedMemorySwap, err := units.RAMInBytes(*flMemorySwap)
  180. if err != nil {
  181. return err
  182. }
  183. memorySwap = parsedMemorySwap
  184. }
  185. }
  186. var remoteContext string
  187. if isRemote {
  188. remoteContext = cmd.Arg(0)
  189. }
  190. options := types.ImageBuildOptions{
  191. Context: body,
  192. Memory: memory,
  193. MemorySwap: memorySwap,
  194. Tags: flTags.GetAll(),
  195. SuppressOutput: *suppressOutput,
  196. RemoteContext: remoteContext,
  197. NoCache: *noCache,
  198. Remove: *rm,
  199. ForceRemove: *forceRm,
  200. PullParent: *pull,
  201. Isolation: *isolation,
  202. CPUSetCPUs: *flCPUSetCpus,
  203. CPUSetMems: *flCPUSetMems,
  204. CPUShares: *flCPUShares,
  205. CPUQuota: *flCPUQuota,
  206. CPUPeriod: *flCPUPeriod,
  207. CgroupParent: *flCgroupParent,
  208. ShmSize: *flShmSize,
  209. Dockerfile: relDockerfile,
  210. Ulimits: flUlimits.GetList(),
  211. BuildArgs: flBuildArg.GetAll(),
  212. AuthConfigs: cli.configFile.AuthConfigs,
  213. }
  214. response, err := cli.client.ImageBuild(options)
  215. if err != nil {
  216. return err
  217. }
  218. err = jsonmessage.DisplayJSONMessagesStream(response.Body, buildBuff, cli.outFd, cli.isTerminalOut)
  219. if err != nil {
  220. if jerr, ok := err.(*jsonmessage.JSONError); ok {
  221. // If no error code is set, default to 1
  222. if jerr.Code == 0 {
  223. jerr.Code = 1
  224. }
  225. if *suppressOutput {
  226. fmt.Fprintf(cli.err, "%s%s", progBuff, buildBuff)
  227. }
  228. return Cli.StatusError{Status: jerr.Message, StatusCode: jerr.Code}
  229. }
  230. }
  231. // Windows: show error message about modified file permissions.
  232. if response.OSType == "windows" {
  233. fmt.Fprintln(cli.err, `SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.`)
  234. }
  235. // Everything worked so if -q was provided the output from the daemon
  236. // should be just the image ID and we'll print that to stdout.
  237. if *suppressOutput {
  238. fmt.Fprintf(cli.out, "%s", buildBuff)
  239. }
  240. if isTrusted() {
  241. // Since the build was successful, now we must tag any of the resolved
  242. // images from the above Dockerfile rewrite.
  243. for _, resolved := range resolvedTags {
  244. if err := cli.tagTrusted(resolved.digestRef, resolved.tagRef); err != nil {
  245. return err
  246. }
  247. }
  248. }
  249. return nil
  250. }
  251. // validateContextDirectory checks if all the contents of the directory
  252. // can be read and returns an error if some files can't be read
  253. // symlinks which point to non-existing files don't trigger an error
  254. func validateContextDirectory(srcPath string, excludes []string) error {
  255. contextRoot, err := getContextRoot(srcPath)
  256. if err != nil {
  257. return err
  258. }
  259. return filepath.Walk(contextRoot, func(filePath string, f os.FileInfo, err error) error {
  260. // skip this directory/file if it's not in the path, it won't get added to the context
  261. if relFilePath, err := filepath.Rel(contextRoot, filePath); err != nil {
  262. return err
  263. } else if skip, err := fileutils.Matches(relFilePath, excludes); err != nil {
  264. return err
  265. } else if skip {
  266. if f.IsDir() {
  267. return filepath.SkipDir
  268. }
  269. return nil
  270. }
  271. if err != nil {
  272. if os.IsPermission(err) {
  273. return fmt.Errorf("can't stat '%s'", filePath)
  274. }
  275. if os.IsNotExist(err) {
  276. return nil
  277. }
  278. return err
  279. }
  280. // skip checking if symlinks point to non-existing files, such symlinks can be useful
  281. // also skip named pipes, because they hanging on open
  282. if f.Mode()&(os.ModeSymlink|os.ModeNamedPipe) != 0 {
  283. return nil
  284. }
  285. if !f.IsDir() {
  286. currentFile, err := os.Open(filePath)
  287. if err != nil && os.IsPermission(err) {
  288. return fmt.Errorf("no permission to read from '%s'", filePath)
  289. }
  290. currentFile.Close()
  291. }
  292. return nil
  293. })
  294. }
  295. // validateTag checks if the given image name can be resolved.
  296. func validateTag(rawRepo string) (string, error) {
  297. _, err := reference.ParseNamed(rawRepo)
  298. if err != nil {
  299. return "", err
  300. }
  301. return rawRepo, nil
  302. }
  303. // isUNC returns true if the path is UNC (one starting \\). It always returns
  304. // false on Linux.
  305. func isUNC(path string) bool {
  306. return runtime.GOOS == "windows" && strings.HasPrefix(path, `\\`)
  307. }
  308. // getDockerfileRelPath uses the given context directory for a `docker build`
  309. // and returns the absolute path to the context directory, the relative path of
  310. // the dockerfile in that context directory, and a non-nil error on success.
  311. func getDockerfileRelPath(givenContextDir, givenDockerfile string) (absContextDir, relDockerfile string, err error) {
  312. if absContextDir, err = filepath.Abs(givenContextDir); err != nil {
  313. return "", "", fmt.Errorf("unable to get absolute context directory: %v", err)
  314. }
  315. // The context dir might be a symbolic link, so follow it to the actual
  316. // target directory.
  317. //
  318. // FIXME. We use isUNC (always false on non-Windows platforms) to workaround
  319. // an issue in golang. On Windows, EvalSymLinks does not work on UNC file
  320. // paths (those starting with \\). This hack means that when using links
  321. // on UNC paths, they will not be followed.
  322. if !isUNC(absContextDir) {
  323. absContextDir, err = filepath.EvalSymlinks(absContextDir)
  324. if err != nil {
  325. return "", "", fmt.Errorf("unable to evaluate symlinks in context path: %v", err)
  326. }
  327. }
  328. stat, err := os.Lstat(absContextDir)
  329. if err != nil {
  330. return "", "", fmt.Errorf("unable to stat context directory %q: %v", absContextDir, err)
  331. }
  332. if !stat.IsDir() {
  333. return "", "", fmt.Errorf("context must be a directory: %s", absContextDir)
  334. }
  335. absDockerfile := givenDockerfile
  336. if absDockerfile == "" {
  337. // No -f/--file was specified so use the default relative to the
  338. // context directory.
  339. absDockerfile = filepath.Join(absContextDir, api.DefaultDockerfileName)
  340. // Just to be nice ;-) look for 'dockerfile' too but only
  341. // use it if we found it, otherwise ignore this check
  342. if _, err = os.Lstat(absDockerfile); os.IsNotExist(err) {
  343. altPath := filepath.Join(absContextDir, strings.ToLower(api.DefaultDockerfileName))
  344. if _, err = os.Lstat(altPath); err == nil {
  345. absDockerfile = altPath
  346. }
  347. }
  348. }
  349. // If not already an absolute path, the Dockerfile path should be joined to
  350. // the base directory.
  351. if !filepath.IsAbs(absDockerfile) {
  352. absDockerfile = filepath.Join(absContextDir, absDockerfile)
  353. }
  354. // Evaluate symlinks in the path to the Dockerfile too.
  355. //
  356. // FIXME. We use isUNC (always false on non-Windows platforms) to workaround
  357. // an issue in golang. On Windows, EvalSymLinks does not work on UNC file
  358. // paths (those starting with \\). This hack means that when using links
  359. // on UNC paths, they will not be followed.
  360. if !isUNC(absDockerfile) {
  361. absDockerfile, err = filepath.EvalSymlinks(absDockerfile)
  362. if err != nil {
  363. return "", "", fmt.Errorf("unable to evaluate symlinks in Dockerfile path: %v", err)
  364. }
  365. }
  366. if _, err := os.Lstat(absDockerfile); err != nil {
  367. if os.IsNotExist(err) {
  368. return "", "", fmt.Errorf("Cannot locate Dockerfile: %q", absDockerfile)
  369. }
  370. return "", "", fmt.Errorf("unable to stat Dockerfile: %v", err)
  371. }
  372. if relDockerfile, err = filepath.Rel(absContextDir, absDockerfile); err != nil {
  373. return "", "", fmt.Errorf("unable to get relative Dockerfile path: %v", err)
  374. }
  375. if strings.HasPrefix(relDockerfile, ".."+string(filepath.Separator)) {
  376. return "", "", fmt.Errorf("The Dockerfile (%s) must be within the build context (%s)", givenDockerfile, givenContextDir)
  377. }
  378. return absContextDir, relDockerfile, nil
  379. }
  380. // writeToFile copies from the given reader and writes it to a file with the
  381. // given filename.
  382. func writeToFile(r io.Reader, filename string) error {
  383. file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0600))
  384. if err != nil {
  385. return fmt.Errorf("unable to create file: %v", err)
  386. }
  387. defer file.Close()
  388. if _, err := io.Copy(file, r); err != nil {
  389. return fmt.Errorf("unable to write file: %v", err)
  390. }
  391. return nil
  392. }
  393. // getContextFromReader will read the contents of the given reader as either a
  394. // Dockerfile or tar archive to be extracted to a temporary directory used as
  395. // the context directory. Returns the absolute path to the temporary context
  396. // directory, the relative path of the dockerfile in that context directory,
  397. // and a non-nil error on success.
  398. func getContextFromReader(r io.Reader, dockerfileName string) (absContextDir, relDockerfile string, err error) {
  399. buf := bufio.NewReader(r)
  400. magic, err := buf.Peek(archive.HeaderSize)
  401. if err != nil && err != io.EOF {
  402. return "", "", fmt.Errorf("failed to peek context header from STDIN: %v", err)
  403. }
  404. if absContextDir, err = ioutil.TempDir("", "docker-build-context-"); err != nil {
  405. return "", "", fmt.Errorf("unbale to create temporary context directory: %v", err)
  406. }
  407. defer func(d string) {
  408. if err != nil {
  409. os.RemoveAll(d)
  410. }
  411. }(absContextDir)
  412. if !archive.IsArchive(magic) { // Input should be read as a Dockerfile.
  413. // -f option has no meaning when we're reading it from stdin,
  414. // so just use our default Dockerfile name
  415. relDockerfile = api.DefaultDockerfileName
  416. return absContextDir, relDockerfile, writeToFile(buf, filepath.Join(absContextDir, relDockerfile))
  417. }
  418. if err := archive.Untar(buf, absContextDir, nil); err != nil {
  419. return "", "", fmt.Errorf("unable to extract stdin to temporary context directory: %v", err)
  420. }
  421. return getDockerfileRelPath(absContextDir, dockerfileName)
  422. }
  423. // getContextFromGitURL uses a Git URL as context for a `docker build`. The
  424. // git repo is cloned into a temporary directory used as the context directory.
  425. // Returns the absolute path to the temporary context directory, the relative
  426. // path of the dockerfile in that context directory, and a non-nil error on
  427. // success.
  428. func getContextFromGitURL(gitURL, dockerfileName string) (absContextDir, relDockerfile string, err error) {
  429. if absContextDir, err = gitutils.Clone(gitURL); err != nil {
  430. return "", "", fmt.Errorf("unable to 'git clone' to temporary context directory: %v", err)
  431. }
  432. return getDockerfileRelPath(absContextDir, dockerfileName)
  433. }
  434. // getContextFromURL uses a remote URL as context for a `docker build`. The
  435. // remote resource is downloaded as either a Dockerfile or a context tar
  436. // archive and stored in a temporary directory used as the context directory.
  437. // Returns the absolute path to the temporary context directory, the relative
  438. // path of the dockerfile in that context directory, and a non-nil error on
  439. // success.
  440. func getContextFromURL(out io.Writer, remoteURL, dockerfileName string) (absContextDir, relDockerfile string, err error) {
  441. response, err := httputils.Download(remoteURL)
  442. if err != nil {
  443. return "", "", fmt.Errorf("unable to download remote context %s: %v", remoteURL, err)
  444. }
  445. defer response.Body.Close()
  446. progressOutput := streamformatter.NewStreamFormatter().NewProgressOutput(out, true)
  447. // Pass the response body through a progress reader.
  448. progReader := progress.NewProgressReader(response.Body, progressOutput, response.ContentLength, "", fmt.Sprintf("Downloading build context from remote url: %s", remoteURL))
  449. return getContextFromReader(progReader, dockerfileName)
  450. }
  451. // getContextFromLocalDir uses the given local directory as context for a
  452. // `docker build`. Returns the absolute path to the local context directory,
  453. // the relative path of the dockerfile in that context directory, and a non-nil
  454. // error on success.
  455. func getContextFromLocalDir(localDir, dockerfileName string) (absContextDir, relDockerfile string, err error) {
  456. // When using a local context directory, when the Dockerfile is specified
  457. // with the `-f/--file` option then it is considered relative to the
  458. // current directory and not the context directory.
  459. if dockerfileName != "" {
  460. if dockerfileName, err = filepath.Abs(dockerfileName); err != nil {
  461. return "", "", fmt.Errorf("unable to get absolute path to Dockerfile: %v", err)
  462. }
  463. }
  464. return getDockerfileRelPath(localDir, dockerfileName)
  465. }
  466. var dockerfileFromLinePattern = regexp.MustCompile(`(?i)^[\s]*FROM[ \f\r\t\v]+(?P<image>[^ \f\r\t\v\n#]+)`)
  467. type trustedDockerfile struct {
  468. *os.File
  469. size int64
  470. }
  471. func (td *trustedDockerfile) Close() error {
  472. td.File.Close()
  473. return os.Remove(td.File.Name())
  474. }
  475. // resolvedTag records the repository, tag, and resolved digest reference
  476. // from a Dockerfile rewrite.
  477. type resolvedTag struct {
  478. digestRef reference.Canonical
  479. tagRef reference.NamedTagged
  480. }
  481. // rewriteDockerfileFrom rewrites the given Dockerfile by resolving images in
  482. // "FROM <image>" instructions to a digest reference. `translator` is a
  483. // function that takes a repository name and tag reference and returns a
  484. // trusted digest reference.
  485. func rewriteDockerfileFrom(dockerfileName string, translator func(reference.NamedTagged) (reference.Canonical, error)) (newDockerfile *trustedDockerfile, resolvedTags []*resolvedTag, err error) {
  486. dockerfile, err := os.Open(dockerfileName)
  487. if err != nil {
  488. return nil, nil, fmt.Errorf("unable to open Dockerfile: %v", err)
  489. }
  490. defer dockerfile.Close()
  491. scanner := bufio.NewScanner(dockerfile)
  492. // Make a tempfile to store the rewritten Dockerfile.
  493. tempFile, err := ioutil.TempFile("", "trusted-dockerfile-")
  494. if err != nil {
  495. return nil, nil, fmt.Errorf("unable to make temporary trusted Dockerfile: %v", err)
  496. }
  497. trustedFile := &trustedDockerfile{
  498. File: tempFile,
  499. }
  500. defer func() {
  501. if err != nil {
  502. // Close the tempfile if there was an error during Notary lookups.
  503. // Otherwise the caller should close it.
  504. trustedFile.Close()
  505. }
  506. }()
  507. // Scan the lines of the Dockerfile, looking for a "FROM" line.
  508. for scanner.Scan() {
  509. line := scanner.Text()
  510. matches := dockerfileFromLinePattern.FindStringSubmatch(line)
  511. if matches != nil && matches[1] != api.NoBaseImageSpecifier {
  512. // Replace the line with a resolved "FROM repo@digest"
  513. ref, err := reference.ParseNamed(matches[1])
  514. if err != nil {
  515. return nil, nil, err
  516. }
  517. ref = reference.WithDefaultTag(ref)
  518. if ref, ok := ref.(reference.NamedTagged); ok && isTrusted() {
  519. trustedRef, err := translator(ref)
  520. if err != nil {
  521. return nil, nil, err
  522. }
  523. line = dockerfileFromLinePattern.ReplaceAllLiteralString(line, fmt.Sprintf("FROM %s", trustedRef.String()))
  524. resolvedTags = append(resolvedTags, &resolvedTag{
  525. digestRef: trustedRef,
  526. tagRef: ref,
  527. })
  528. }
  529. }
  530. n, err := fmt.Fprintln(tempFile, line)
  531. if err != nil {
  532. return nil, nil, err
  533. }
  534. trustedFile.size += int64(n)
  535. }
  536. tempFile.Seek(0, os.SEEK_SET)
  537. return trustedFile, resolvedTags, scanner.Err()
  538. }
  539. // replaceDockerfileTarWrapper wraps the given input tar archive stream and
  540. // replaces the entry with the given Dockerfile name with the contents of the
  541. // new Dockerfile. Returns a new tar archive stream with the replaced
  542. // Dockerfile.
  543. func replaceDockerfileTarWrapper(inputTarStream io.ReadCloser, newDockerfile *trustedDockerfile, dockerfileName string) io.ReadCloser {
  544. pipeReader, pipeWriter := io.Pipe()
  545. go func() {
  546. tarReader := tar.NewReader(inputTarStream)
  547. tarWriter := tar.NewWriter(pipeWriter)
  548. defer inputTarStream.Close()
  549. for {
  550. hdr, err := tarReader.Next()
  551. if err == io.EOF {
  552. // Signals end of archive.
  553. tarWriter.Close()
  554. pipeWriter.Close()
  555. return
  556. }
  557. if err != nil {
  558. pipeWriter.CloseWithError(err)
  559. return
  560. }
  561. var content io.Reader = tarReader
  562. if hdr.Name == dockerfileName {
  563. // This entry is the Dockerfile. Since the tar archive was
  564. // generated from a directory on the local filesystem, the
  565. // Dockerfile will only appear once in the archive.
  566. hdr.Size = newDockerfile.size
  567. content = newDockerfile
  568. }
  569. if err := tarWriter.WriteHeader(hdr); err != nil {
  570. pipeWriter.CloseWithError(err)
  571. return
  572. }
  573. if _, err := io.Copy(tarWriter, content); err != nil {
  574. pipeWriter.CloseWithError(err)
  575. return
  576. }
  577. }
  578. }()
  579. return pipeReader
  580. }