build.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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 if the
  240. // daemon isn't running Windows.
  241. if response.OSType != "windows" && runtime.GOOS == "windows" {
  242. 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.`)
  243. }
  244. // Everything worked so if -q was provided the output from the daemon
  245. // should be just the image ID and we'll print that to stdout.
  246. if *suppressOutput {
  247. fmt.Fprintf(cli.out, "%s", buildBuff)
  248. }
  249. if isTrusted() {
  250. // Since the build was successful, now we must tag any of the resolved
  251. // images from the above Dockerfile rewrite.
  252. for _, resolved := range resolvedTags {
  253. if err := cli.tagTrusted(resolved.digestRef, resolved.tagRef); err != nil {
  254. return err
  255. }
  256. }
  257. }
  258. return nil
  259. }
  260. // validateContextDirectory checks if all the contents of the directory
  261. // can be read and returns an error if some files can't be read
  262. // symlinks which point to non-existing files don't trigger an error
  263. func validateContextDirectory(srcPath string, excludes []string) error {
  264. contextRoot, err := getContextRoot(srcPath)
  265. if err != nil {
  266. return err
  267. }
  268. return filepath.Walk(contextRoot, func(filePath string, f os.FileInfo, err error) error {
  269. // skip this directory/file if it's not in the path, it won't get added to the context
  270. if relFilePath, err := filepath.Rel(contextRoot, filePath); err != nil {
  271. return err
  272. } else if skip, err := fileutils.Matches(relFilePath, excludes); err != nil {
  273. return err
  274. } else if skip {
  275. if f.IsDir() {
  276. return filepath.SkipDir
  277. }
  278. return nil
  279. }
  280. if err != nil {
  281. if os.IsPermission(err) {
  282. return fmt.Errorf("can't stat '%s'", filePath)
  283. }
  284. if os.IsNotExist(err) {
  285. return nil
  286. }
  287. return err
  288. }
  289. // skip checking if symlinks point to non-existing files, such symlinks can be useful
  290. // also skip named pipes, because they hanging on open
  291. if f.Mode()&(os.ModeSymlink|os.ModeNamedPipe) != 0 {
  292. return nil
  293. }
  294. if !f.IsDir() {
  295. currentFile, err := os.Open(filePath)
  296. if err != nil && os.IsPermission(err) {
  297. return fmt.Errorf("no permission to read from '%s'", filePath)
  298. }
  299. currentFile.Close()
  300. }
  301. return nil
  302. })
  303. }
  304. // validateTag checks if the given image name can be resolved.
  305. func validateTag(rawRepo string) (string, error) {
  306. _, err := reference.ParseNamed(rawRepo)
  307. if err != nil {
  308. return "", err
  309. }
  310. return rawRepo, nil
  311. }
  312. // isUNC returns true if the path is UNC (one starting \\). It always returns
  313. // false on Linux.
  314. func isUNC(path string) bool {
  315. return runtime.GOOS == "windows" && strings.HasPrefix(path, `\\`)
  316. }
  317. // getDockerfileRelPath uses the given context directory for a `docker build`
  318. // and returns the absolute path to the context directory, the relative path of
  319. // the dockerfile in that context directory, and a non-nil error on success.
  320. func getDockerfileRelPath(givenContextDir, givenDockerfile string) (absContextDir, relDockerfile string, err error) {
  321. if absContextDir, err = filepath.Abs(givenContextDir); err != nil {
  322. return "", "", fmt.Errorf("unable to get absolute context directory: %v", err)
  323. }
  324. // The context dir might be a symbolic link, so follow it to the actual
  325. // target directory.
  326. //
  327. // FIXME. We use isUNC (always false on non-Windows platforms) to workaround
  328. // an issue in golang. On Windows, EvalSymLinks does not work on UNC file
  329. // paths (those starting with \\). This hack means that when using links
  330. // on UNC paths, they will not be followed.
  331. if !isUNC(absContextDir) {
  332. absContextDir, err = filepath.EvalSymlinks(absContextDir)
  333. if err != nil {
  334. return "", "", fmt.Errorf("unable to evaluate symlinks in context path: %v", err)
  335. }
  336. }
  337. stat, err := os.Lstat(absContextDir)
  338. if err != nil {
  339. return "", "", fmt.Errorf("unable to stat context directory %q: %v", absContextDir, err)
  340. }
  341. if !stat.IsDir() {
  342. return "", "", fmt.Errorf("context must be a directory: %s", absContextDir)
  343. }
  344. absDockerfile := givenDockerfile
  345. if absDockerfile == "" {
  346. // No -f/--file was specified so use the default relative to the
  347. // context directory.
  348. absDockerfile = filepath.Join(absContextDir, api.DefaultDockerfileName)
  349. // Just to be nice ;-) look for 'dockerfile' too but only
  350. // use it if we found it, otherwise ignore this check
  351. if _, err = os.Lstat(absDockerfile); os.IsNotExist(err) {
  352. altPath := filepath.Join(absContextDir, strings.ToLower(api.DefaultDockerfileName))
  353. if _, err = os.Lstat(altPath); err == nil {
  354. absDockerfile = altPath
  355. }
  356. }
  357. }
  358. // If not already an absolute path, the Dockerfile path should be joined to
  359. // the base directory.
  360. if !filepath.IsAbs(absDockerfile) {
  361. absDockerfile = filepath.Join(absContextDir, absDockerfile)
  362. }
  363. // Evaluate symlinks in the path to the Dockerfile too.
  364. //
  365. // FIXME. We use isUNC (always false on non-Windows platforms) to workaround
  366. // an issue in golang. On Windows, EvalSymLinks does not work on UNC file
  367. // paths (those starting with \\). This hack means that when using links
  368. // on UNC paths, they will not be followed.
  369. if !isUNC(absDockerfile) {
  370. absDockerfile, err = filepath.EvalSymlinks(absDockerfile)
  371. if err != nil {
  372. return "", "", fmt.Errorf("unable to evaluate symlinks in Dockerfile path: %v", err)
  373. }
  374. }
  375. if _, err := os.Lstat(absDockerfile); err != nil {
  376. if os.IsNotExist(err) {
  377. return "", "", fmt.Errorf("Cannot locate Dockerfile: %q", absDockerfile)
  378. }
  379. return "", "", fmt.Errorf("unable to stat Dockerfile: %v", err)
  380. }
  381. if relDockerfile, err = filepath.Rel(absContextDir, absDockerfile); err != nil {
  382. return "", "", fmt.Errorf("unable to get relative Dockerfile path: %v", err)
  383. }
  384. if strings.HasPrefix(relDockerfile, ".."+string(filepath.Separator)) {
  385. return "", "", fmt.Errorf("The Dockerfile (%s) must be within the build context (%s)", givenDockerfile, givenContextDir)
  386. }
  387. return absContextDir, relDockerfile, nil
  388. }
  389. // writeToFile copies from the given reader and writes it to a file with the
  390. // given filename.
  391. func writeToFile(r io.Reader, filename string) error {
  392. file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0600))
  393. if err != nil {
  394. return fmt.Errorf("unable to create file: %v", err)
  395. }
  396. defer file.Close()
  397. if _, err := io.Copy(file, r); err != nil {
  398. return fmt.Errorf("unable to write file: %v", err)
  399. }
  400. return nil
  401. }
  402. // getContextFromReader will read the contents of the given reader as either a
  403. // Dockerfile or tar archive to be extracted to a temporary directory used as
  404. // the context directory. Returns the absolute path to the temporary context
  405. // directory, the relative path of the dockerfile in that context directory,
  406. // and a non-nil error on success.
  407. func getContextFromReader(r io.Reader, dockerfileName string) (absContextDir, relDockerfile string, err error) {
  408. buf := bufio.NewReader(r)
  409. magic, err := buf.Peek(archive.HeaderSize)
  410. if err != nil && err != io.EOF {
  411. return "", "", fmt.Errorf("failed to peek context header from STDIN: %v", err)
  412. }
  413. if absContextDir, err = ioutil.TempDir("", "docker-build-context-"); err != nil {
  414. return "", "", fmt.Errorf("unbale to create temporary context directory: %v", err)
  415. }
  416. defer func(d string) {
  417. if err != nil {
  418. os.RemoveAll(d)
  419. }
  420. }(absContextDir)
  421. if !archive.IsArchive(magic) { // Input should be read as a Dockerfile.
  422. // -f option has no meaning when we're reading it from stdin,
  423. // so just use our default Dockerfile name
  424. relDockerfile = api.DefaultDockerfileName
  425. return absContextDir, relDockerfile, writeToFile(buf, filepath.Join(absContextDir, relDockerfile))
  426. }
  427. if err := archive.Untar(buf, absContextDir, nil); err != nil {
  428. return "", "", fmt.Errorf("unable to extract stdin to temporary context directory: %v", err)
  429. }
  430. return getDockerfileRelPath(absContextDir, dockerfileName)
  431. }
  432. // getContextFromGitURL uses a Git URL as context for a `docker build`. The
  433. // git repo is cloned into a temporary directory used as the context directory.
  434. // Returns the absolute path to the temporary context directory, the relative
  435. // path of the dockerfile in that context directory, and a non-nil error on
  436. // success.
  437. func getContextFromGitURL(gitURL, dockerfileName string) (absContextDir, relDockerfile string, err error) {
  438. if absContextDir, err = gitutils.Clone(gitURL); err != nil {
  439. return "", "", fmt.Errorf("unable to 'git clone' to temporary context directory: %v", err)
  440. }
  441. return getDockerfileRelPath(absContextDir, dockerfileName)
  442. }
  443. // getContextFromURL uses a remote URL as context for a `docker build`. The
  444. // remote resource is downloaded as either a Dockerfile or a context tar
  445. // archive and stored in a temporary directory used as the context directory.
  446. // Returns the absolute path to the temporary context directory, the relative
  447. // path of the dockerfile in that context directory, and a non-nil error on
  448. // success.
  449. func getContextFromURL(out io.Writer, remoteURL, dockerfileName string) (absContextDir, relDockerfile string, err error) {
  450. response, err := httputils.Download(remoteURL)
  451. if err != nil {
  452. return "", "", fmt.Errorf("unable to download remote context %s: %v", remoteURL, err)
  453. }
  454. defer response.Body.Close()
  455. progressOutput := streamformatter.NewStreamFormatter().NewProgressOutput(out, true)
  456. // Pass the response body through a progress reader.
  457. progReader := progress.NewProgressReader(response.Body, progressOutput, response.ContentLength, "", fmt.Sprintf("Downloading build context from remote url: %s", remoteURL))
  458. return getContextFromReader(progReader, dockerfileName)
  459. }
  460. // getContextFromLocalDir uses the given local directory as context for a
  461. // `docker build`. Returns the absolute path to the local context directory,
  462. // the relative path of the dockerfile in that context directory, and a non-nil
  463. // error on success.
  464. func getContextFromLocalDir(localDir, dockerfileName string) (absContextDir, relDockerfile string, err error) {
  465. // When using a local context directory, when the Dockerfile is specified
  466. // with the `-f/--file` option then it is considered relative to the
  467. // current directory and not the context directory.
  468. if dockerfileName != "" {
  469. if dockerfileName, err = filepath.Abs(dockerfileName); err != nil {
  470. return "", "", fmt.Errorf("unable to get absolute path to Dockerfile: %v", err)
  471. }
  472. }
  473. return getDockerfileRelPath(localDir, dockerfileName)
  474. }
  475. var dockerfileFromLinePattern = regexp.MustCompile(`(?i)^[\s]*FROM[ \f\r\t\v]+(?P<image>[^ \f\r\t\v\n#]+)`)
  476. type trustedDockerfile struct {
  477. *os.File
  478. size int64
  479. }
  480. func (td *trustedDockerfile) Close() error {
  481. td.File.Close()
  482. return os.Remove(td.File.Name())
  483. }
  484. // resolvedTag records the repository, tag, and resolved digest reference
  485. // from a Dockerfile rewrite.
  486. type resolvedTag struct {
  487. digestRef reference.Canonical
  488. tagRef reference.NamedTagged
  489. }
  490. // rewriteDockerfileFrom rewrites the given Dockerfile by resolving images in
  491. // "FROM <image>" instructions to a digest reference. `translator` is a
  492. // function that takes a repository name and tag reference and returns a
  493. // trusted digest reference.
  494. func rewriteDockerfileFrom(dockerfileName string, translator func(reference.NamedTagged) (reference.Canonical, error)) (newDockerfile *trustedDockerfile, resolvedTags []*resolvedTag, err error) {
  495. dockerfile, err := os.Open(dockerfileName)
  496. if err != nil {
  497. return nil, nil, fmt.Errorf("unable to open Dockerfile: %v", err)
  498. }
  499. defer dockerfile.Close()
  500. scanner := bufio.NewScanner(dockerfile)
  501. // Make a tempfile to store the rewritten Dockerfile.
  502. tempFile, err := ioutil.TempFile("", "trusted-dockerfile-")
  503. if err != nil {
  504. return nil, nil, fmt.Errorf("unable to make temporary trusted Dockerfile: %v", err)
  505. }
  506. trustedFile := &trustedDockerfile{
  507. File: tempFile,
  508. }
  509. defer func() {
  510. if err != nil {
  511. // Close the tempfile if there was an error during Notary lookups.
  512. // Otherwise the caller should close it.
  513. trustedFile.Close()
  514. }
  515. }()
  516. // Scan the lines of the Dockerfile, looking for a "FROM" line.
  517. for scanner.Scan() {
  518. line := scanner.Text()
  519. matches := dockerfileFromLinePattern.FindStringSubmatch(line)
  520. if matches != nil && matches[1] != api.NoBaseImageSpecifier {
  521. // Replace the line with a resolved "FROM repo@digest"
  522. ref, err := reference.ParseNamed(matches[1])
  523. if err != nil {
  524. return nil, nil, err
  525. }
  526. ref = reference.WithDefaultTag(ref)
  527. if ref, ok := ref.(reference.NamedTagged); ok && isTrusted() {
  528. trustedRef, err := translator(ref)
  529. if err != nil {
  530. return nil, nil, err
  531. }
  532. line = dockerfileFromLinePattern.ReplaceAllLiteralString(line, fmt.Sprintf("FROM %s", trustedRef.String()))
  533. resolvedTags = append(resolvedTags, &resolvedTag{
  534. digestRef: trustedRef,
  535. tagRef: ref,
  536. })
  537. }
  538. }
  539. n, err := fmt.Fprintln(tempFile, line)
  540. if err != nil {
  541. return nil, nil, err
  542. }
  543. trustedFile.size += int64(n)
  544. }
  545. tempFile.Seek(0, os.SEEK_SET)
  546. return trustedFile, resolvedTags, scanner.Err()
  547. }
  548. // replaceDockerfileTarWrapper wraps the given input tar archive stream and
  549. // replaces the entry with the given Dockerfile name with the contents of the
  550. // new Dockerfile. Returns a new tar archive stream with the replaced
  551. // Dockerfile.
  552. func replaceDockerfileTarWrapper(inputTarStream io.ReadCloser, newDockerfile *trustedDockerfile, dockerfileName string) io.ReadCloser {
  553. pipeReader, pipeWriter := io.Pipe()
  554. go func() {
  555. tarReader := tar.NewReader(inputTarStream)
  556. tarWriter := tar.NewWriter(pipeWriter)
  557. defer inputTarStream.Close()
  558. for {
  559. hdr, err := tarReader.Next()
  560. if err == io.EOF {
  561. // Signals end of archive.
  562. tarWriter.Close()
  563. pipeWriter.Close()
  564. return
  565. }
  566. if err != nil {
  567. pipeWriter.CloseWithError(err)
  568. return
  569. }
  570. var content io.Reader = tarReader
  571. if hdr.Name == dockerfileName {
  572. // This entry is the Dockerfile. Since the tar archive was
  573. // generated from a directory on the local filesystem, the
  574. // Dockerfile will only appear once in the archive.
  575. hdr.Size = newDockerfile.size
  576. content = newDockerfile
  577. }
  578. if err := tarWriter.WriteHeader(hdr); err != nil {
  579. pipeWriter.CloseWithError(err)
  580. return
  581. }
  582. if _, err := io.Copy(tarWriter, content); err != nil {
  583. pipeWriter.CloseWithError(err)
  584. return
  585. }
  586. }
  587. }()
  588. return pipeReader
  589. }