2016-12-22 16:25:02 -05:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2017-06-05 18:23:21 -04:00
|
|
|
"archive/tar"
|
2016-12-22 16:25:02 -05:00
|
|
|
"bufio"
|
2017-06-05 18:23:21 -04:00
|
|
|
"bytes"
|
2016-12-22 16:25:02 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2017-06-05 18:23:21 -04:00
|
|
|
"net/http"
|
2016-12-22 16:25:02 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2017-05-03 15:58:39 -04:00
|
|
|
"time"
|
|
|
|
|
2017-06-05 18:23:21 -04:00
|
|
|
"github.com/docker/docker/builder/remotecontext/git"
|
2016-12-22 16:25:02 -05:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2017-06-21 15:13:44 -04:00
|
|
|
"github.com/docker/docker/pkg/pools"
|
2016-12-22 16:25:02 -05:00
|
|
|
"github.com/docker/docker/pkg/progress"
|
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2017-04-16 16:06:16 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
Use golang.org/x/sys/execabs
On Windows, the os/exec.{Command,CommandContext,LookPath} functions
resolve command names that have neither path separators nor file extension
(e.g., "git") by first looking in the current working directory before
looking in the PATH environment variable.
Go maintainers intended to match cmd.exe's historical behavior.
However, this is pretty much never the intended behavior and as an abundance of precaution
this patch prevents that when executing commands.
Example of commands that docker.exe may execute: `git`, `docker-buildx` (or other cli plugin), `docker-credential-wincred`, `docker`.
Note that this was prompted by the [Go 1.15.7 security fixes](https://blog.golang.org/path-security), but unlike in `go.exe`,
the windows path lookups in docker are not in a code path allowing remote code execution, thus there is no security impact on docker.
Signed-off-by: Tibor Vass <tibor@docker.com>
2021-01-25 14:18:54 -05:00
|
|
|
exec "golang.org/x/sys/execabs"
|
2016-12-22 16:25:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultDockerfileName is the Default filename with Docker commands, read by docker build
|
|
|
|
DefaultDockerfileName string = "Dockerfile"
|
2017-06-08 16:08:11 -04:00
|
|
|
// archiveHeaderSize is the number of bytes in an archive header
|
|
|
|
archiveHeaderSize = 512
|
2016-12-22 16:25:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateContextDirectory checks if all the contents of the directory
|
|
|
|
// can be read and returns an error if some files can't be read
|
|
|
|
// symlinks which point to non-existing files don't trigger an error
|
|
|
|
func ValidateContextDirectory(srcPath string, excludes []string) error {
|
|
|
|
contextRoot, err := getContextRoot(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-14 12:42:28 -05:00
|
|
|
|
|
|
|
pm, err := fileutils.NewPatternMatcher(excludes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:25:02 -05:00
|
|
|
return filepath.Walk(contextRoot, func(filePath string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
if os.IsPermission(err) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("can't stat '%s'", filePath)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
if os.IsNotExist(err) {
|
2018-05-11 09:34:53 -04:00
|
|
|
return errors.Errorf("file ('%s') not found or excluded by .dockerignore", filePath)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip this directory/file if it's not in the path, it won't get added to the context
|
|
|
|
if relFilePath, err := filepath.Rel(contextRoot, filePath); err != nil {
|
|
|
|
return err
|
2018-12-23 07:05:23 -05:00
|
|
|
} else if skip, err := filepathMatches(pm, relFilePath); err != nil {
|
2016-12-22 16:25:02 -05:00
|
|
|
return err
|
|
|
|
} else if skip {
|
|
|
|
if f.IsDir() {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip checking if symlinks point to non-existing files, such symlinks can be useful
|
|
|
|
// also skip named pipes, because they hanging on open
|
|
|
|
if f.Mode()&(os.ModeSymlink|os.ModeNamedPipe) != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !f.IsDir() {
|
|
|
|
currentFile, err := os.Open(filePath)
|
|
|
|
if err != nil && os.IsPermission(err) {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("no permission to read from '%s'", filePath)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
currentFile.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-23 07:05:23 -05:00
|
|
|
func filepathMatches(matcher *fileutils.PatternMatcher, file string) (bool, error) {
|
|
|
|
file = filepath.Clean(file)
|
|
|
|
if file == "." {
|
|
|
|
// Don't let them exclude everything, kind of silly.
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return matcher.Matches(file)
|
|
|
|
}
|
|
|
|
|
2018-05-18 22:56:22 -04:00
|
|
|
// DetectArchiveReader detects whether the input stream is an archive or a
|
|
|
|
// Dockerfile and returns a buffered version of input, safe to consume in lieu
|
|
|
|
// of input. If an archive is detected, isArchive is set to true, and to false
|
|
|
|
// otherwise, in which case it is safe to assume input represents the contents
|
|
|
|
// of a Dockerfile.
|
|
|
|
func DetectArchiveReader(input io.ReadCloser) (rc io.ReadCloser, isArchive bool, err error) {
|
|
|
|
buf := bufio.NewReader(input)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
2019-03-21 18:26:59 -04:00
|
|
|
magic, err := buf.Peek(archiveHeaderSize * 2)
|
2016-12-22 16:25:02 -05:00
|
|
|
if err != nil && err != io.EOF {
|
2018-05-18 22:56:22 -04:00
|
|
|
return nil, false, errors.Errorf("failed to peek context header from STDIN: %v", err)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
2018-05-18 22:56:22 -04:00
|
|
|
return ioutils.NewReadCloserWrapper(buf, func() error { return input.Close() }), IsArchive(magic), nil
|
|
|
|
}
|
2016-12-22 16:25:02 -05:00
|
|
|
|
2018-05-18 22:56:22 -04:00
|
|
|
// WriteTempDockerfile writes a Dockerfile stream to a temporary file with a
|
|
|
|
// name specified by DefaultDockerfileName and returns the path to the
|
|
|
|
// temporary directory containing the Dockerfile.
|
2018-06-13 14:25:22 -04:00
|
|
|
func WriteTempDockerfile(rc io.ReadCloser) (dockerfileDir string, err error) {
|
2018-06-13 18:35:15 -04:00
|
|
|
// err is a named return value, due to the defer call below.
|
2018-06-13 14:25:22 -04:00
|
|
|
dockerfileDir, err = ioutil.TempDir("", "docker-build-tempdockerfile-")
|
2016-12-22 16:25:02 -05:00
|
|
|
if err != nil {
|
2018-05-18 22:56:22 -04:00
|
|
|
return "", errors.Errorf("unable to create temporary context directory: %v", err)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
2018-06-13 14:25:22 -04:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(dockerfileDir)
|
|
|
|
}
|
|
|
|
}()
|
2016-12-22 16:25:02 -05:00
|
|
|
|
2018-05-18 22:56:22 -04:00
|
|
|
f, err := os.Create(filepath.Join(dockerfileDir, DefaultDockerfileName))
|
2016-12-22 16:25:02 -05:00
|
|
|
if err != nil {
|
2018-05-18 22:56:22 -04:00
|
|
|
return "", err
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
2018-06-08 21:16:51 -04:00
|
|
|
defer f.Close()
|
|
|
|
if _, err := io.Copy(f, rc); err != nil {
|
2018-05-18 22:56:22 -04:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return dockerfileDir, rc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContextFromReader will read the contents of the given reader as either a
|
|
|
|
// Dockerfile or tar archive. Returns a tar archive used as a context and a
|
|
|
|
// path to the Dockerfile inside the tar.
|
|
|
|
func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadCloser, relDockerfile string, err error) {
|
|
|
|
rc, isArchive, err := DetectArchiveReader(rc)
|
|
|
|
if err != nil {
|
2016-12-22 16:25:02 -05:00
|
|
|
return nil, "", err
|
|
|
|
}
|
2018-05-18 22:56:22 -04:00
|
|
|
|
|
|
|
if isArchive {
|
|
|
|
return rc, dockerfileName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Input should be read as a Dockerfile.
|
|
|
|
|
|
|
|
if dockerfileName == "-" {
|
|
|
|
return nil, "", errors.New("build context is not an archive")
|
|
|
|
}
|
|
|
|
|
|
|
|
dockerfileDir, err := WriteTempDockerfile(rc)
|
|
|
|
if err != nil {
|
2016-12-22 16:25:02 -05:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
2018-05-18 22:56:22 -04:00
|
|
|
tar, err := archive.Tar(dockerfileDir, archive.Uncompressed)
|
2016-12-22 16:25:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutils.NewReadCloserWrapper(tar, func() error {
|
|
|
|
err := tar.Close()
|
2018-05-18 22:56:22 -04:00
|
|
|
os.RemoveAll(dockerfileDir)
|
2016-12-22 16:25:02 -05:00
|
|
|
return err
|
|
|
|
}), DefaultDockerfileName, nil
|
|
|
|
}
|
|
|
|
|
2017-06-08 16:08:11 -04:00
|
|
|
// IsArchive checks for the magic bytes of a tar or any supported compression
|
|
|
|
// algorithm.
|
|
|
|
func IsArchive(header []byte) bool {
|
|
|
|
compression := archive.DetectCompression(header)
|
|
|
|
if compression != archive.Uncompressed {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
r := tar.NewReader(bytes.NewBuffer(header))
|
|
|
|
_, err := r.Next()
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2016-12-22 16:25:02 -05:00
|
|
|
// GetContextFromGitURL uses a Git URL as context for a `docker build`. The
|
|
|
|
// git repo is cloned into a temporary directory used as the context directory.
|
|
|
|
// Returns the absolute path to the temporary context directory, the relative
|
|
|
|
// path of the dockerfile in that context directory, and a non-nil error on
|
|
|
|
// success.
|
2017-04-06 15:36:15 -04:00
|
|
|
func GetContextFromGitURL(gitURL, dockerfileName string) (string, string, error) {
|
2016-12-22 16:25:02 -05:00
|
|
|
if _, err := exec.LookPath("git"); err != nil {
|
2017-04-06 15:36:15 -04:00
|
|
|
return "", "", errors.Wrapf(err, "unable to find 'git'")
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
2017-06-05 18:23:21 -04:00
|
|
|
absContextDir, err := git.Clone(gitURL)
|
2017-04-06 15:36:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", "", errors.Wrapf(err, "unable to 'git clone' to temporary context directory")
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
2017-04-16 15:01:15 -04:00
|
|
|
absContextDir, err = ResolveAndValidateContextPath(absContextDir)
|
2017-04-06 15:36:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
relDockerfile, err := getDockerfileRelPath(absContextDir, dockerfileName)
|
2018-02-17 19:40:55 -05:00
|
|
|
if err == nil && strings.HasPrefix(relDockerfile, ".."+string(filepath.Separator)) {
|
|
|
|
return "", "", errors.Errorf("the Dockerfile (%s) must be within the build context", dockerfileName)
|
|
|
|
}
|
|
|
|
|
2017-04-06 15:36:15 -04:00
|
|
|
return absContextDir, relDockerfile, err
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetContextFromURL uses a remote URL as context for a `docker build`. The
|
|
|
|
// remote resource is downloaded as either a Dockerfile or a tar archive.
|
|
|
|
// Returns the tar archive used for the context and a path of the
|
|
|
|
// dockerfile inside the tar.
|
|
|
|
func GetContextFromURL(out io.Writer, remoteURL, dockerfileName string) (io.ReadCloser, string, error) {
|
2017-06-05 18:23:21 -04:00
|
|
|
response, err := getWithStatusError(remoteURL)
|
2016-12-22 16:25:02 -05:00
|
|
|
if err != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
return nil, "", errors.Errorf("unable to download remote context %s: %v", remoteURL, err)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
2017-05-01 14:54:56 -04:00
|
|
|
progressOutput := streamformatter.NewProgressOutput(out)
|
2016-12-22 16:25:02 -05:00
|
|
|
|
|
|
|
// Pass the response body through a progress reader.
|
|
|
|
progReader := progress.NewProgressReader(response.Body, progressOutput, response.ContentLength, "", fmt.Sprintf("Downloading build context from remote url: %s", remoteURL))
|
|
|
|
|
|
|
|
return GetContextFromReader(ioutils.NewReadCloserWrapper(progReader, func() error { return response.Body.Close() }), dockerfileName)
|
|
|
|
}
|
|
|
|
|
2017-06-05 18:23:21 -04:00
|
|
|
// getWithStatusError does an http.Get() and returns an error if the
|
|
|
|
// status code is 4xx or 5xx.
|
|
|
|
func getWithStatusError(url string) (resp *http.Response, err error) {
|
2019-10-28 20:51:07 -04:00
|
|
|
// #nosec G107
|
2017-06-05 18:23:21 -04:00
|
|
|
if resp, err = http.Get(url); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode < 400 {
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
msg := fmt.Sprintf("failed to GET %s with status %s", url, resp.Status)
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
2019-09-12 16:12:15 -04:00
|
|
|
return nil, errors.Wrapf(err, "%s: error reading body", msg)
|
2017-06-05 18:23:21 -04:00
|
|
|
}
|
2019-09-12 16:12:15 -04:00
|
|
|
return nil, errors.Errorf("%s: %s", msg, bytes.TrimSpace(body))
|
2017-06-05 18:23:21 -04:00
|
|
|
}
|
|
|
|
|
2016-12-22 16:25:02 -05:00
|
|
|
// GetContextFromLocalDir uses the given local directory as context for a
|
|
|
|
// `docker build`. Returns the absolute path to the local context directory,
|
|
|
|
// the relative path of the dockerfile in that context directory, and a non-nil
|
|
|
|
// error on success.
|
2017-04-06 15:36:15 -04:00
|
|
|
func GetContextFromLocalDir(localDir, dockerfileName string) (string, string, error) {
|
2017-04-16 15:01:15 -04:00
|
|
|
localDir, err := ResolveAndValidateContextPath(localDir)
|
2017-04-06 15:36:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// When using a local context directory, and the Dockerfile is specified
|
2016-12-22 16:25:02 -05:00
|
|
|
// with the `-f/--file` option then it is considered relative to the
|
|
|
|
// current directory and not the context directory.
|
2017-02-21 15:07:45 -05:00
|
|
|
if dockerfileName != "" && dockerfileName != "-" {
|
2016-12-22 16:25:02 -05:00
|
|
|
if dockerfileName, err = filepath.Abs(dockerfileName); err != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
return "", "", errors.Errorf("unable to get absolute path to Dockerfile: %v", err)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 15:36:15 -04:00
|
|
|
relDockerfile, err := getDockerfileRelPath(localDir, dockerfileName)
|
|
|
|
return localDir, relDockerfile, err
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
2017-04-16 15:01:15 -04:00
|
|
|
// ResolveAndValidateContextPath uses the given context directory for a `docker build`
|
2017-04-06 15:36:15 -04:00
|
|
|
// and returns the absolute path to the context directory.
|
2017-04-16 15:01:15 -04:00
|
|
|
func ResolveAndValidateContextPath(givenContextDir string) (string, error) {
|
2017-04-06 15:36:15 -04:00
|
|
|
absContextDir, err := filepath.Abs(givenContextDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Errorf("unable to get absolute context directory of given context directory %q: %v", givenContextDir, err)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// The context dir might be a symbolic link, so follow it to the actual
|
|
|
|
// target directory.
|
|
|
|
//
|
|
|
|
// FIXME. We use isUNC (always false on non-Windows platforms) to workaround
|
|
|
|
// an issue in golang. On Windows, EvalSymLinks does not work on UNC file
|
|
|
|
// paths (those starting with \\). This hack means that when using links
|
|
|
|
// on UNC paths, they will not be followed.
|
|
|
|
if !isUNC(absContextDir) {
|
|
|
|
absContextDir, err = filepath.EvalSymlinks(absContextDir)
|
|
|
|
if err != nil {
|
2017-04-06 15:36:15 -04:00
|
|
|
return "", errors.Errorf("unable to evaluate symlinks in context path: %v", err)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stat, err := os.Lstat(absContextDir)
|
|
|
|
if err != nil {
|
2017-04-06 15:36:15 -04:00
|
|
|
return "", errors.Errorf("unable to stat context directory %q: %v", absContextDir, err)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !stat.IsDir() {
|
2017-04-06 15:36:15 -04:00
|
|
|
return "", errors.Errorf("context must be a directory: %s", absContextDir)
|
|
|
|
}
|
|
|
|
return absContextDir, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDockerfileRelPath returns the dockerfile path relative to the context
|
|
|
|
// directory
|
|
|
|
func getDockerfileRelPath(absContextDir, givenDockerfile string) (string, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if givenDockerfile == "-" {
|
|
|
|
return givenDockerfile, nil
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
absDockerfile := givenDockerfile
|
|
|
|
if absDockerfile == "" {
|
|
|
|
// No -f/--file was specified so use the default relative to the
|
|
|
|
// context directory.
|
|
|
|
absDockerfile = filepath.Join(absContextDir, DefaultDockerfileName)
|
|
|
|
|
|
|
|
// Just to be nice ;-) look for 'dockerfile' too but only
|
|
|
|
// use it if we found it, otherwise ignore this check
|
|
|
|
if _, err = os.Lstat(absDockerfile); os.IsNotExist(err) {
|
|
|
|
altPath := filepath.Join(absContextDir, strings.ToLower(DefaultDockerfileName))
|
|
|
|
if _, err = os.Lstat(altPath); err == nil {
|
|
|
|
absDockerfile = altPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not already an absolute path, the Dockerfile path should be joined to
|
|
|
|
// the base directory.
|
|
|
|
if !filepath.IsAbs(absDockerfile) {
|
|
|
|
absDockerfile = filepath.Join(absContextDir, absDockerfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate symlinks in the path to the Dockerfile too.
|
|
|
|
//
|
|
|
|
// FIXME. We use isUNC (always false on non-Windows platforms) to workaround
|
|
|
|
// an issue in golang. On Windows, EvalSymLinks does not work on UNC file
|
|
|
|
// paths (those starting with \\). This hack means that when using links
|
|
|
|
// on UNC paths, they will not be followed.
|
2017-04-06 15:36:15 -04:00
|
|
|
if !isUNC(absDockerfile) {
|
|
|
|
absDockerfile, err = filepath.EvalSymlinks(absDockerfile)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Errorf("unable to evaluate symlinks in Dockerfile path: %v", err)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
2017-04-06 15:36:15 -04:00
|
|
|
}
|
2016-12-22 16:25:02 -05:00
|
|
|
|
2017-04-06 15:36:15 -04:00
|
|
|
if _, err := os.Lstat(absDockerfile); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return "", errors.Errorf("Cannot locate Dockerfile: %q", absDockerfile)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
2017-04-06 15:36:15 -04:00
|
|
|
return "", errors.Errorf("unable to stat Dockerfile: %v", err)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
2017-04-06 15:36:15 -04:00
|
|
|
relDockerfile, err := filepath.Rel(absContextDir, absDockerfile)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Errorf("unable to get relative Dockerfile path: %v", err)
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
2017-04-06 15:36:15 -04:00
|
|
|
return relDockerfile, nil
|
2016-12-22 16:25:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// isUNC returns true if the path is UNC (one starting \\). It always returns
|
|
|
|
// false on Linux.
|
|
|
|
func isUNC(path string) bool {
|
|
|
|
return runtime.GOOS == "windows" && strings.HasPrefix(path, `\\`)
|
|
|
|
}
|
2017-04-16 16:06:16 -04:00
|
|
|
|
|
|
|
// AddDockerfileToBuildContext from a ReadCloser, returns a new archive and
|
|
|
|
// the relative path to the dockerfile in the context.
|
|
|
|
func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCloser) (io.ReadCloser, string, error) {
|
|
|
|
file, err := ioutil.ReadAll(dockerfileCtx)
|
|
|
|
dockerfileCtx.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
now := time.Now()
|
|
|
|
randomName := ".dockerfile." + stringid.GenerateRandomID()[:20]
|
|
|
|
|
|
|
|
buildCtx = archive.ReplaceFileTarWrapper(buildCtx, map[string]archive.TarModifierFunc{
|
|
|
|
// Add the dockerfile with a random filename
|
build: fix AddDockerfileToBuildContext not de-referencing tar header template
Commit https://github.com/docker/docker/commit/73aef6edfe5ae533f9cf547f924c98a9b0f7be59
modified archive.ReplaceFileTarWrapper to set the Name field in the tar header,
if the field was not set.
That change exposed an issue in how a Dockerfile from stdin was sent to the daemon.
When attempting to build using a build-context, and a Dockerfile from stdin, the
following happened:
```bash
mkdir build-stdin && cd build-stdin && echo hello > hello.txt
DOCKER_BUILDKIT=0 docker build --no-cache -t foo -f- . <<'EOF'
FROM alpine
COPY . .
EOF
Sending build context to Docker daemon 2.607kB
Error response from daemon: dockerfile parse error line 1: unknown instruction: .DOCKERIGNORE
```
Removing the `-t foo`, oddly lead to a different failure:
```bash
DOCKER_BUILDKIT=0 docker build --no-cache -f- . <<'EOF'
FROM alpine
COPY . .
EOF
Sending build context to Docker daemon 2.581kB
Error response from daemon: Cannot locate specified Dockerfile: .dockerfile.701d0d71fb1497d6a7ce
```
From the above, it looks like the tar headers got mangled, causing (in the first
case) the daemon to use the build-context tar as a plain-text file, and therefore
parsing it as Dockerfile, and in the second case, causing it to not being able to
find the Dockerfile in the context.
I noticed that both TarModifierFuncs were using the same `hdrTmpl` struct, which
looks to caused them to step on each other's toes. Changing them to each initialize
their own struct made the issue go away.
After this change:
```bash
DOCKER_BUILDKIT=0 docker build --no-cache -t foo -f- . <<'EOF'
FROM alpine
COPY . .
EOF
Sending build context to Docker daemon 2.607kB
Step 1/2 : FROM alpine
---> d4ff818577bc
Step 2/2 : COPY . .
---> 556f745e6938
Successfully built 556f745e6938
Successfully tagged foo:latest
DOCKER_BUILDKIT=0 docker build --no-cache -f- . <<'EOF'
FROM alpine
COPY . .
EOF
Sending build context to Docker daemon 2.607kB
Step 1/2 : FROM alpine
---> d4ff818577bc
Step 2/2 : COPY . .
---> aaaee43bec5e
Successfully built aaaee43bec5e
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-08-10 17:13:00 -04:00
|
|
|
randomName: func(_ string, _ *tar.Header, _ io.Reader) (*tar.Header, []byte, error) {
|
|
|
|
header := &tar.Header{
|
|
|
|
Name: randomName,
|
|
|
|
Mode: 0600,
|
|
|
|
ModTime: now,
|
|
|
|
Typeflag: tar.TypeReg,
|
|
|
|
AccessTime: now,
|
|
|
|
ChangeTime: now,
|
|
|
|
}
|
|
|
|
return header, file, nil
|
2017-04-16 16:06:16 -04:00
|
|
|
},
|
|
|
|
// Update .dockerignore to include the random filename
|
|
|
|
".dockerignore": func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
|
|
|
|
if h == nil {
|
build: fix AddDockerfileToBuildContext not de-referencing tar header template
Commit https://github.com/docker/docker/commit/73aef6edfe5ae533f9cf547f924c98a9b0f7be59
modified archive.ReplaceFileTarWrapper to set the Name field in the tar header,
if the field was not set.
That change exposed an issue in how a Dockerfile from stdin was sent to the daemon.
When attempting to build using a build-context, and a Dockerfile from stdin, the
following happened:
```bash
mkdir build-stdin && cd build-stdin && echo hello > hello.txt
DOCKER_BUILDKIT=0 docker build --no-cache -t foo -f- . <<'EOF'
FROM alpine
COPY . .
EOF
Sending build context to Docker daemon 2.607kB
Error response from daemon: dockerfile parse error line 1: unknown instruction: .DOCKERIGNORE
```
Removing the `-t foo`, oddly lead to a different failure:
```bash
DOCKER_BUILDKIT=0 docker build --no-cache -f- . <<'EOF'
FROM alpine
COPY . .
EOF
Sending build context to Docker daemon 2.581kB
Error response from daemon: Cannot locate specified Dockerfile: .dockerfile.701d0d71fb1497d6a7ce
```
From the above, it looks like the tar headers got mangled, causing (in the first
case) the daemon to use the build-context tar as a plain-text file, and therefore
parsing it as Dockerfile, and in the second case, causing it to not being able to
find the Dockerfile in the context.
I noticed that both TarModifierFuncs were using the same `hdrTmpl` struct, which
looks to caused them to step on each other's toes. Changing them to each initialize
their own struct made the issue go away.
After this change:
```bash
DOCKER_BUILDKIT=0 docker build --no-cache -t foo -f- . <<'EOF'
FROM alpine
COPY . .
EOF
Sending build context to Docker daemon 2.607kB
Step 1/2 : FROM alpine
---> d4ff818577bc
Step 2/2 : COPY . .
---> 556f745e6938
Successfully built 556f745e6938
Successfully tagged foo:latest
DOCKER_BUILDKIT=0 docker build --no-cache -f- . <<'EOF'
FROM alpine
COPY . .
EOF
Sending build context to Docker daemon 2.607kB
Step 1/2 : FROM alpine
---> d4ff818577bc
Step 2/2 : COPY . .
---> aaaee43bec5e
Successfully built aaaee43bec5e
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2021-08-10 17:13:00 -04:00
|
|
|
h = &tar.Header{
|
|
|
|
Name: ".dockerignore",
|
|
|
|
Mode: 0600,
|
|
|
|
ModTime: now,
|
|
|
|
Typeflag: tar.TypeReg,
|
|
|
|
AccessTime: now,
|
|
|
|
ChangeTime: now,
|
|
|
|
}
|
2017-04-16 16:06:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
if content != nil {
|
|
|
|
if _, err := b.ReadFrom(content); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b.WriteString(".dockerignore")
|
|
|
|
}
|
|
|
|
b.WriteString("\n" + randomName + "\n")
|
|
|
|
return h, b.Bytes(), nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return buildCtx, randomName, nil
|
|
|
|
}
|
2017-06-21 15:13:44 -04:00
|
|
|
|
|
|
|
// Compress the build context for sending to the API
|
|
|
|
func Compress(buildCtx io.ReadCloser) (io.ReadCloser, error) {
|
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
compressWriter, err := archive.CompressStream(pipeWriter, archive.Gzip)
|
|
|
|
if err != nil {
|
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
}
|
|
|
|
defer buildCtx.Close()
|
|
|
|
|
|
|
|
if _, err := pools.Copy(compressWriter, buildCtx); err != nil {
|
|
|
|
pipeWriter.CloseWithError(
|
|
|
|
errors.Wrap(err, "failed to compress context"))
|
|
|
|
compressWriter.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
compressWriter.Close()
|
|
|
|
pipeWriter.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return pipeReader, nil
|
|
|
|
}
|