build.go 22 KB

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