github.com/skf/moby@v1.13.1/builder/context.go (about) 1 package builder 2 3 import ( 4 "bufio" 5 "fmt" 6 "io" 7 "io/ioutil" 8 "os" 9 "os/exec" 10 "path/filepath" 11 "runtime" 12 "strings" 13 14 "github.com/docker/docker/pkg/archive" 15 "github.com/docker/docker/pkg/fileutils" 16 "github.com/docker/docker/pkg/gitutils" 17 "github.com/docker/docker/pkg/httputils" 18 "github.com/docker/docker/pkg/ioutils" 19 "github.com/docker/docker/pkg/progress" 20 "github.com/docker/docker/pkg/streamformatter" 21 ) 22 23 // ValidateContextDirectory checks if all the contents of the directory 24 // can be read and returns an error if some files can't be read 25 // symlinks which point to non-existing files don't trigger an error 26 func ValidateContextDirectory(srcPath string, excludes []string) error { 27 contextRoot, err := getContextRoot(srcPath) 28 if err != nil { 29 return err 30 } 31 return filepath.Walk(contextRoot, func(filePath string, f os.FileInfo, err error) error { 32 if err != nil { 33 if os.IsPermission(err) { 34 return fmt.Errorf("can't stat '%s'", filePath) 35 } 36 if os.IsNotExist(err) { 37 return nil 38 } 39 return err 40 } 41 42 // skip this directory/file if it's not in the path, it won't get added to the context 43 if relFilePath, err := filepath.Rel(contextRoot, filePath); err != nil { 44 return err 45 } else if skip, err := fileutils.Matches(relFilePath, excludes); err != nil { 46 return err 47 } else if skip { 48 if f.IsDir() { 49 return filepath.SkipDir 50 } 51 return nil 52 } 53 54 // skip checking if symlinks point to non-existing files, such symlinks can be useful 55 // also skip named pipes, because they hanging on open 56 if f.Mode()&(os.ModeSymlink|os.ModeNamedPipe) != 0 { 57 return nil 58 } 59 60 if !f.IsDir() { 61 currentFile, err := os.Open(filePath) 62 if err != nil && os.IsPermission(err) { 63 return fmt.Errorf("no permission to read from '%s'", filePath) 64 } 65 currentFile.Close() 66 } 67 return nil 68 }) 69 } 70 71 // GetContextFromReader will read the contents of the given reader as either a 72 // Dockerfile or tar archive. Returns a tar archive used as a context and a 73 // path to the Dockerfile inside the tar. 74 func GetContextFromReader(r io.ReadCloser, dockerfileName string) (out io.ReadCloser, relDockerfile string, err error) { 75 buf := bufio.NewReader(r) 76 77 magic, err := buf.Peek(archive.HeaderSize) 78 if err != nil && err != io.EOF { 79 return nil, "", fmt.Errorf("failed to peek context header from STDIN: %v", err) 80 } 81 82 if archive.IsArchive(magic) { 83 return ioutils.NewReadCloserWrapper(buf, func() error { return r.Close() }), dockerfileName, nil 84 } 85 86 // Input should be read as a Dockerfile. 87 tmpDir, err := ioutil.TempDir("", "docker-build-context-") 88 if err != nil { 89 return nil, "", fmt.Errorf("unbale to create temporary context directory: %v", err) 90 } 91 92 f, err := os.Create(filepath.Join(tmpDir, DefaultDockerfileName)) 93 if err != nil { 94 return nil, "", err 95 } 96 _, err = io.Copy(f, buf) 97 if err != nil { 98 f.Close() 99 return nil, "", err 100 } 101 102 if err := f.Close(); err != nil { 103 return nil, "", err 104 } 105 if err := r.Close(); err != nil { 106 return nil, "", err 107 } 108 109 tar, err := archive.Tar(tmpDir, archive.Uncompressed) 110 if err != nil { 111 return nil, "", err 112 } 113 114 return ioutils.NewReadCloserWrapper(tar, func() error { 115 err := tar.Close() 116 os.RemoveAll(tmpDir) 117 return err 118 }), DefaultDockerfileName, nil 119 120 } 121 122 // GetContextFromGitURL uses a Git URL as context for a `docker build`. The 123 // git repo is cloned into a temporary directory used as the context directory. 124 // Returns the absolute path to the temporary context directory, the relative 125 // path of the dockerfile in that context directory, and a non-nil error on 126 // success. 127 func GetContextFromGitURL(gitURL, dockerfileName string) (absContextDir, relDockerfile string, err error) { 128 if _, err := exec.LookPath("git"); err != nil { 129 return "", "", fmt.Errorf("unable to find 'git': %v", err) 130 } 131 if absContextDir, err = gitutils.Clone(gitURL); err != nil { 132 return "", "", fmt.Errorf("unable to 'git clone' to temporary context directory: %v", err) 133 } 134 135 return getDockerfileRelPath(absContextDir, dockerfileName) 136 } 137 138 // GetContextFromURL uses a remote URL as context for a `docker build`. The 139 // remote resource is downloaded as either a Dockerfile or a tar archive. 140 // Returns the tar archive used for the context and a path of the 141 // dockerfile inside the tar. 142 func GetContextFromURL(out io.Writer, remoteURL, dockerfileName string) (io.ReadCloser, string, error) { 143 response, err := httputils.Download(remoteURL) 144 if err != nil { 145 return nil, "", fmt.Errorf("unable to download remote context %s: %v", remoteURL, err) 146 } 147 progressOutput := streamformatter.NewStreamFormatter().NewProgressOutput(out, true) 148 149 // Pass the response body through a progress reader. 150 progReader := progress.NewProgressReader(response.Body, progressOutput, response.ContentLength, "", fmt.Sprintf("Downloading build context from remote url: %s", remoteURL)) 151 152 return GetContextFromReader(ioutils.NewReadCloserWrapper(progReader, func() error { return response.Body.Close() }), dockerfileName) 153 } 154 155 // GetContextFromLocalDir uses the given local directory as context for a 156 // `docker build`. Returns the absolute path to the local context directory, 157 // the relative path of the dockerfile in that context directory, and a non-nil 158 // error on success. 159 func GetContextFromLocalDir(localDir, dockerfileName string) (absContextDir, relDockerfile string, err error) { 160 // When using a local context directory, when the Dockerfile is specified 161 // with the `-f/--file` option then it is considered relative to the 162 // current directory and not the context directory. 163 if dockerfileName != "" { 164 if dockerfileName, err = filepath.Abs(dockerfileName); err != nil { 165 return "", "", fmt.Errorf("unable to get absolute path to Dockerfile: %v", err) 166 } 167 } 168 169 return getDockerfileRelPath(localDir, dockerfileName) 170 } 171 172 // getDockerfileRelPath uses the given context directory for a `docker build` 173 // and returns the absolute path to the context directory, the relative path of 174 // the dockerfile in that context directory, and a non-nil error on success. 175 func getDockerfileRelPath(givenContextDir, givenDockerfile string) (absContextDir, relDockerfile string, err error) { 176 if absContextDir, err = filepath.Abs(givenContextDir); err != nil { 177 return "", "", fmt.Errorf("unable to get absolute context directory of given context directory %q: %v", givenContextDir, err) 178 } 179 180 // The context dir might be a symbolic link, so follow it to the actual 181 // target directory. 182 // 183 // FIXME. We use isUNC (always false on non-Windows platforms) to workaround 184 // an issue in golang. On Windows, EvalSymLinks does not work on UNC file 185 // paths (those starting with \\). This hack means that when using links 186 // on UNC paths, they will not be followed. 187 if !isUNC(absContextDir) { 188 absContextDir, err = filepath.EvalSymlinks(absContextDir) 189 if err != nil { 190 return "", "", fmt.Errorf("unable to evaluate symlinks in context path: %v", err) 191 } 192 } 193 194 stat, err := os.Lstat(absContextDir) 195 if err != nil { 196 return "", "", fmt.Errorf("unable to stat context directory %q: %v", absContextDir, err) 197 } 198 199 if !stat.IsDir() { 200 return "", "", fmt.Errorf("context must be a directory: %s", absContextDir) 201 } 202 203 absDockerfile := givenDockerfile 204 if absDockerfile == "" { 205 // No -f/--file was specified so use the default relative to the 206 // context directory. 207 absDockerfile = filepath.Join(absContextDir, DefaultDockerfileName) 208 209 // Just to be nice ;-) look for 'dockerfile' too but only 210 // use it if we found it, otherwise ignore this check 211 if _, err = os.Lstat(absDockerfile); os.IsNotExist(err) { 212 altPath := filepath.Join(absContextDir, strings.ToLower(DefaultDockerfileName)) 213 if _, err = os.Lstat(altPath); err == nil { 214 absDockerfile = altPath 215 } 216 } 217 } 218 219 // If not already an absolute path, the Dockerfile path should be joined to 220 // the base directory. 221 if !filepath.IsAbs(absDockerfile) { 222 absDockerfile = filepath.Join(absContextDir, absDockerfile) 223 } 224 225 // Evaluate symlinks in the path to the Dockerfile too. 226 // 227 // FIXME. We use isUNC (always false on non-Windows platforms) to workaround 228 // an issue in golang. On Windows, EvalSymLinks does not work on UNC file 229 // paths (those starting with \\). This hack means that when using links 230 // on UNC paths, they will not be followed. 231 if !isUNC(absDockerfile) { 232 absDockerfile, err = filepath.EvalSymlinks(absDockerfile) 233 if err != nil { 234 return "", "", fmt.Errorf("unable to evaluate symlinks in Dockerfile path: %v", err) 235 } 236 } 237 238 if _, err := os.Lstat(absDockerfile); err != nil { 239 if os.IsNotExist(err) { 240 return "", "", fmt.Errorf("Cannot locate Dockerfile: %q", absDockerfile) 241 } 242 return "", "", fmt.Errorf("unable to stat Dockerfile: %v", err) 243 } 244 245 if relDockerfile, err = filepath.Rel(absContextDir, absDockerfile); err != nil { 246 return "", "", fmt.Errorf("unable to get relative Dockerfile path: %v", err) 247 } 248 249 if strings.HasPrefix(relDockerfile, ".."+string(filepath.Separator)) { 250 return "", "", fmt.Errorf("The Dockerfile (%s) must be within the build context (%s)", givenDockerfile, givenContextDir) 251 } 252 253 return absContextDir, relDockerfile, nil 254 } 255 256 // isUNC returns true if the path is UNC (one starting \\). It always returns 257 // false on Linux. 258 func isUNC(path string) bool { 259 return runtime.GOOS == "windows" && strings.HasPrefix(path, `\\`) 260 }