build.go 23 KB

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