Signed-off-by: Vincent Demeester <vincent@sbr.pm>
| ... | ... |
@@ -6,13 +6,10 @@ import ( |
| 6 | 6 |
"bytes" |
| 7 | 7 |
"fmt" |
| 8 | 8 |
"io" |
| 9 |
- "io/ioutil" |
|
| 10 | 9 |
"os" |
| 11 |
- "os/exec" |
|
| 12 | 10 |
"path/filepath" |
| 13 | 11 |
"regexp" |
| 14 | 12 |
"runtime" |
| 15 |
- "strings" |
|
| 16 | 13 |
|
| 17 | 14 |
"golang.org/x/net/context" |
| 18 | 15 |
|
| ... | ... |
@@ -23,9 +20,6 @@ import ( |
| 23 | 23 |
"github.com/docker/docker/opts" |
| 24 | 24 |
"github.com/docker/docker/pkg/archive" |
| 25 | 25 |
"github.com/docker/docker/pkg/fileutils" |
| 26 |
- "github.com/docker/docker/pkg/gitutils" |
|
| 27 |
- "github.com/docker/docker/pkg/httputils" |
|
| 28 |
- "github.com/docker/docker/pkg/ioutils" |
|
| 29 | 26 |
"github.com/docker/docker/pkg/jsonmessage" |
| 30 | 27 |
flag "github.com/docker/docker/pkg/mflag" |
| 31 | 28 |
"github.com/docker/docker/pkg/progress" |
| ... | ... |
@@ -103,13 +97,13 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
|
| 103 | 103 |
|
| 104 | 104 |
switch {
|
| 105 | 105 |
case specifiedContext == "-": |
| 106 |
- ctx, relDockerfile, err = getContextFromReader(cli.in, *dockerfileName) |
|
| 106 |
+ ctx, relDockerfile, err = builder.GetContextFromReader(cli.in, *dockerfileName) |
|
| 107 | 107 |
case urlutil.IsGitURL(specifiedContext): |
| 108 |
- tempDir, relDockerfile, err = getContextFromGitURL(specifiedContext, *dockerfileName) |
|
| 108 |
+ tempDir, relDockerfile, err = builder.GetContextFromGitURL(specifiedContext, *dockerfileName) |
|
| 109 | 109 |
case urlutil.IsURL(specifiedContext): |
| 110 |
- ctx, relDockerfile, err = getContextFromURL(progBuff, specifiedContext, *dockerfileName) |
|
| 110 |
+ ctx, relDockerfile, err = builder.GetContextFromURL(progBuff, specifiedContext, *dockerfileName) |
|
| 111 | 111 |
default: |
| 112 |
- contextDir, relDockerfile, err = getContextFromLocalDir(specifiedContext, *dockerfileName) |
|
| 112 |
+ contextDir, relDockerfile, err = builder.GetContextFromLocalDir(specifiedContext, *dockerfileName) |
|
| 113 | 113 |
} |
| 114 | 114 |
|
| 115 | 115 |
if err != nil {
|
| ... | ... |
@@ -292,96 +286,6 @@ func validateTag(rawRepo string) (string, error) {
|
| 292 | 292 |
return rawRepo, nil |
| 293 | 293 |
} |
| 294 | 294 |
|
| 295 |
-// isUNC returns true if the path is UNC (one starting \\). It always returns |
|
| 296 |
-// false on Linux. |
|
| 297 |
-func isUNC(path string) bool {
|
|
| 298 |
- return runtime.GOOS == "windows" && strings.HasPrefix(path, `\\`) |
|
| 299 |
-} |
|
| 300 |
- |
|
| 301 |
-// getDockerfileRelPath uses the given context directory for a `docker build` |
|
| 302 |
-// and returns the absolute path to the context directory, the relative path of |
|
| 303 |
-// the dockerfile in that context directory, and a non-nil error on success. |
|
| 304 |
-func getDockerfileRelPath(givenContextDir, givenDockerfile string) (absContextDir, relDockerfile string, err error) {
|
|
| 305 |
- if absContextDir, err = filepath.Abs(givenContextDir); err != nil {
|
|
| 306 |
- return "", "", fmt.Errorf("unable to get absolute context directory: %v", err)
|
|
| 307 |
- } |
|
| 308 |
- |
|
| 309 |
- // The context dir might be a symbolic link, so follow it to the actual |
|
| 310 |
- // target directory. |
|
| 311 |
- // |
|
| 312 |
- // FIXME. We use isUNC (always false on non-Windows platforms) to workaround |
|
| 313 |
- // an issue in golang. On Windows, EvalSymLinks does not work on UNC file |
|
| 314 |
- // paths (those starting with \\). This hack means that when using links |
|
| 315 |
- // on UNC paths, they will not be followed. |
|
| 316 |
- if !isUNC(absContextDir) {
|
|
| 317 |
- absContextDir, err = filepath.EvalSymlinks(absContextDir) |
|
| 318 |
- if err != nil {
|
|
| 319 |
- return "", "", fmt.Errorf("unable to evaluate symlinks in context path: %v", err)
|
|
| 320 |
- } |
|
| 321 |
- } |
|
| 322 |
- |
|
| 323 |
- stat, err := os.Lstat(absContextDir) |
|
| 324 |
- if err != nil {
|
|
| 325 |
- return "", "", fmt.Errorf("unable to stat context directory %q: %v", absContextDir, err)
|
|
| 326 |
- } |
|
| 327 |
- |
|
| 328 |
- if !stat.IsDir() {
|
|
| 329 |
- return "", "", fmt.Errorf("context must be a directory: %s", absContextDir)
|
|
| 330 |
- } |
|
| 331 |
- |
|
| 332 |
- absDockerfile := givenDockerfile |
|
| 333 |
- if absDockerfile == "" {
|
|
| 334 |
- // No -f/--file was specified so use the default relative to the |
|
| 335 |
- // context directory. |
|
| 336 |
- absDockerfile = filepath.Join(absContextDir, api.DefaultDockerfileName) |
|
| 337 |
- |
|
| 338 |
- // Just to be nice ;-) look for 'dockerfile' too but only |
|
| 339 |
- // use it if we found it, otherwise ignore this check |
|
| 340 |
- if _, err = os.Lstat(absDockerfile); os.IsNotExist(err) {
|
|
| 341 |
- altPath := filepath.Join(absContextDir, strings.ToLower(api.DefaultDockerfileName)) |
|
| 342 |
- if _, err = os.Lstat(altPath); err == nil {
|
|
| 343 |
- absDockerfile = altPath |
|
| 344 |
- } |
|
| 345 |
- } |
|
| 346 |
- } |
|
| 347 |
- |
|
| 348 |
- // If not already an absolute path, the Dockerfile path should be joined to |
|
| 349 |
- // the base directory. |
|
| 350 |
- if !filepath.IsAbs(absDockerfile) {
|
|
| 351 |
- absDockerfile = filepath.Join(absContextDir, absDockerfile) |
|
| 352 |
- } |
|
| 353 |
- |
|
| 354 |
- // Evaluate symlinks in the path to the Dockerfile too. |
|
| 355 |
- // |
|
| 356 |
- // FIXME. We use isUNC (always false on non-Windows platforms) to workaround |
|
| 357 |
- // an issue in golang. On Windows, EvalSymLinks does not work on UNC file |
|
| 358 |
- // paths (those starting with \\). This hack means that when using links |
|
| 359 |
- // on UNC paths, they will not be followed. |
|
| 360 |
- if !isUNC(absDockerfile) {
|
|
| 361 |
- absDockerfile, err = filepath.EvalSymlinks(absDockerfile) |
|
| 362 |
- if err != nil {
|
|
| 363 |
- return "", "", fmt.Errorf("unable to evaluate symlinks in Dockerfile path: %v", err)
|
|
| 364 |
- } |
|
| 365 |
- } |
|
| 366 |
- |
|
| 367 |
- if _, err := os.Lstat(absDockerfile); err != nil {
|
|
| 368 |
- if os.IsNotExist(err) {
|
|
| 369 |
- return "", "", fmt.Errorf("Cannot locate Dockerfile: %q", absDockerfile)
|
|
| 370 |
- } |
|
| 371 |
- return "", "", fmt.Errorf("unable to stat Dockerfile: %v", err)
|
|
| 372 |
- } |
|
| 373 |
- |
|
| 374 |
- if relDockerfile, err = filepath.Rel(absContextDir, absDockerfile); err != nil {
|
|
| 375 |
- return "", "", fmt.Errorf("unable to get relative Dockerfile path: %v", err)
|
|
| 376 |
- } |
|
| 377 |
- |
|
| 378 |
- if strings.HasPrefix(relDockerfile, ".."+string(filepath.Separator)) {
|
|
| 379 |
- return "", "", fmt.Errorf("The Dockerfile (%s) must be within the build context (%s)", givenDockerfile, givenContextDir)
|
|
| 380 |
- } |
|
| 381 |
- |
|
| 382 |
- return absContextDir, relDockerfile, nil |
|
| 383 |
-} |
|
| 384 |
- |
|
| 385 | 295 |
// writeToFile copies from the given reader and writes it to a file with the |
| 386 | 296 |
// given filename. |
| 387 | 297 |
func writeToFile(r io.Reader, filename string) error {
|
| ... | ... |
@@ -398,107 +302,6 @@ func writeToFile(r io.Reader, filename string) error {
|
| 398 | 398 |
return nil |
| 399 | 399 |
} |
| 400 | 400 |
|
| 401 |
-// getContextFromReader will read the contents of the given reader as either a |
|
| 402 |
-// Dockerfile or tar archive. Returns a tar archive used as a context and a |
|
| 403 |
-// path to the Dockerfile inside the tar. |
|
| 404 |
-func getContextFromReader(r io.ReadCloser, dockerfileName string) (out io.ReadCloser, relDockerfile string, err error) {
|
|
| 405 |
- buf := bufio.NewReader(r) |
|
| 406 |
- |
|
| 407 |
- magic, err := buf.Peek(archive.HeaderSize) |
|
| 408 |
- if err != nil && err != io.EOF {
|
|
| 409 |
- return nil, "", fmt.Errorf("failed to peek context header from STDIN: %v", err)
|
|
| 410 |
- } |
|
| 411 |
- |
|
| 412 |
- if archive.IsArchive(magic) {
|
|
| 413 |
- return ioutils.NewReadCloserWrapper(buf, func() error { return r.Close() }), dockerfileName, nil
|
|
| 414 |
- } |
|
| 415 |
- |
|
| 416 |
- // Input should be read as a Dockerfile. |
|
| 417 |
- tmpDir, err := ioutil.TempDir("", "docker-build-context-")
|
|
| 418 |
- if err != nil {
|
|
| 419 |
- return nil, "", fmt.Errorf("unbale to create temporary context directory: %v", err)
|
|
| 420 |
- } |
|
| 421 |
- |
|
| 422 |
- f, err := os.Create(filepath.Join(tmpDir, api.DefaultDockerfileName)) |
|
| 423 |
- if err != nil {
|
|
| 424 |
- return nil, "", err |
|
| 425 |
- } |
|
| 426 |
- _, err = io.Copy(f, buf) |
|
| 427 |
- if err != nil {
|
|
| 428 |
- f.Close() |
|
| 429 |
- return nil, "", err |
|
| 430 |
- } |
|
| 431 |
- |
|
| 432 |
- if err := f.Close(); err != nil {
|
|
| 433 |
- return nil, "", err |
|
| 434 |
- } |
|
| 435 |
- if err := r.Close(); err != nil {
|
|
| 436 |
- return nil, "", err |
|
| 437 |
- } |
|
| 438 |
- |
|
| 439 |
- tar, err := archive.Tar(tmpDir, archive.Uncompressed) |
|
| 440 |
- if err != nil {
|
|
| 441 |
- return nil, "", err |
|
| 442 |
- } |
|
| 443 |
- |
|
| 444 |
- return ioutils.NewReadCloserWrapper(tar, func() error {
|
|
| 445 |
- err := tar.Close() |
|
| 446 |
- os.RemoveAll(tmpDir) |
|
| 447 |
- return err |
|
| 448 |
- }), api.DefaultDockerfileName, nil |
|
| 449 |
- |
|
| 450 |
-} |
|
| 451 |
- |
|
| 452 |
-// getContextFromGitURL uses a Git URL as context for a `docker build`. The |
|
| 453 |
-// git repo is cloned into a temporary directory used as the context directory. |
|
| 454 |
-// Returns the absolute path to the temporary context directory, the relative |
|
| 455 |
-// path of the dockerfile in that context directory, and a non-nil error on |
|
| 456 |
-// success. |
|
| 457 |
-func getContextFromGitURL(gitURL, dockerfileName string) (absContextDir, relDockerfile string, err error) {
|
|
| 458 |
- if _, err := exec.LookPath("git"); err != nil {
|
|
| 459 |
- return "", "", fmt.Errorf("unable to find 'git': %v", err)
|
|
| 460 |
- } |
|
| 461 |
- if absContextDir, err = gitutils.Clone(gitURL); err != nil {
|
|
| 462 |
- return "", "", fmt.Errorf("unable to 'git clone' to temporary context directory: %v", err)
|
|
| 463 |
- } |
|
| 464 |
- |
|
| 465 |
- return getDockerfileRelPath(absContextDir, dockerfileName) |
|
| 466 |
-} |
|
| 467 |
- |
|
| 468 |
-// getContextFromURL uses a remote URL as context for a `docker build`. The |
|
| 469 |
-// remote resource is downloaded as either a Dockerfile or a tar archive. |
|
| 470 |
-// Returns the tar archive used for the context and a path of the |
|
| 471 |
-// dockerfile inside the tar. |
|
| 472 |
-func getContextFromURL(out io.Writer, remoteURL, dockerfileName string) (io.ReadCloser, string, error) {
|
|
| 473 |
- response, err := httputils.Download(remoteURL) |
|
| 474 |
- if err != nil {
|
|
| 475 |
- return nil, "", fmt.Errorf("unable to download remote context %s: %v", remoteURL, err)
|
|
| 476 |
- } |
|
| 477 |
- progressOutput := streamformatter.NewStreamFormatter().NewProgressOutput(out, true) |
|
| 478 |
- |
|
| 479 |
- // Pass the response body through a progress reader. |
|
| 480 |
- progReader := progress.NewProgressReader(response.Body, progressOutput, response.ContentLength, "", fmt.Sprintf("Downloading build context from remote url: %s", remoteURL))
|
|
| 481 |
- |
|
| 482 |
- return getContextFromReader(ioutils.NewReadCloserWrapper(progReader, func() error { return response.Body.Close() }), dockerfileName)
|
|
| 483 |
-} |
|
| 484 |
- |
|
| 485 |
-// getContextFromLocalDir uses the given local directory as context for a |
|
| 486 |
-// `docker build`. Returns the absolute path to the local context directory, |
|
| 487 |
-// the relative path of the dockerfile in that context directory, and a non-nil |
|
| 488 |
-// error on success. |
|
| 489 |
-func getContextFromLocalDir(localDir, dockerfileName string) (absContextDir, relDockerfile string, err error) {
|
|
| 490 |
- // When using a local context directory, when the Dockerfile is specified |
|
| 491 |
- // with the `-f/--file` option then it is considered relative to the |
|
| 492 |
- // current directory and not the context directory. |
|
| 493 |
- if dockerfileName != "" {
|
|
| 494 |
- if dockerfileName, err = filepath.Abs(dockerfileName); err != nil {
|
|
| 495 |
- return "", "", fmt.Errorf("unable to get absolute path to Dockerfile: %v", err)
|
|
| 496 |
- } |
|
| 497 |
- } |
|
| 498 |
- |
|
| 499 |
- return getDockerfileRelPath(localDir, dockerfileName) |
|
| 500 |
-} |
|
| 501 |
- |
|
| 502 | 401 |
var dockerfileFromLinePattern = regexp.MustCompile(`(?i)^[\s]*FROM[ \f\r\t\v]+(?P<image>[^ \f\r\t\v\n#]+)`) |
| 503 | 402 |
|
| 504 | 403 |
// resolvedTag records the repository, tag, and resolved digest reference |
| ... | ... |
@@ -23,9 +23,6 @@ const ( |
| 23 | 23 |
// MinVersion represents Minimum REST API version supported |
| 24 | 24 |
MinVersion version.Version = "1.12" |
| 25 | 25 |
|
| 26 |
- // DefaultDockerfileName is the Default filename with Docker commands, read by docker build |
|
| 27 |
- DefaultDockerfileName string = "Dockerfile" |
|
| 28 |
- |
|
| 29 | 26 |
// NoBaseImageSpecifier is the symbol used by the FROM |
| 30 | 27 |
// command to specify that no base image is to be used. |
| 31 | 28 |
NoBaseImageSpecifier string = "scratch" |
| ... | ... |
@@ -14,6 +14,11 @@ import ( |
| 14 | 14 |
"github.com/docker/engine-api/types/container" |
| 15 | 15 |
) |
| 16 | 16 |
|
| 17 |
+const ( |
|
| 18 |
+ // DefaultDockerfileName is the Default filename with Docker commands, read by docker build |
|
| 19 |
+ DefaultDockerfileName string = "Dockerfile" |
|
| 20 |
+) |
|
| 21 |
+ |
|
| 17 | 22 |
// Context represents a file system tree. |
| 18 | 23 |
type Context interface {
|
| 19 | 24 |
// Close allows to signal that the filesystem tree won't be used anymore. |
| ... | ... |
@@ -1,11 +1,23 @@ |
| 1 | 1 |
package builder |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 |
+ "bufio" |
|
| 4 | 5 |
"fmt" |
| 6 |
+ "io" |
|
| 7 |
+ "io/ioutil" |
|
| 5 | 8 |
"os" |
| 9 |
+ "os/exec" |
|
| 6 | 10 |
"path/filepath" |
| 11 |
+ "runtime" |
|
| 12 |
+ "strings" |
|
| 7 | 13 |
|
| 14 |
+ "github.com/docker/docker/pkg/archive" |
|
| 8 | 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" |
|
| 9 | 21 |
) |
| 10 | 22 |
|
| 11 | 23 |
// ValidateContextDirectory checks if all the contents of the directory |
| ... | ... |
@@ -55,3 +67,194 @@ func ValidateContextDirectory(srcPath string, excludes []string) error {
|
| 55 | 55 |
return nil |
| 56 | 56 |
}) |
| 57 | 57 |
} |
| 58 |
+ |
|
| 59 |
+// GetContextFromReader will read the contents of the given reader as either a |
|
| 60 |
+// Dockerfile or tar archive. Returns a tar archive used as a context and a |
|
| 61 |
+// path to the Dockerfile inside the tar. |
|
| 62 |
+func GetContextFromReader(r io.ReadCloser, dockerfileName string) (out io.ReadCloser, relDockerfile string, err error) {
|
|
| 63 |
+ buf := bufio.NewReader(r) |
|
| 64 |
+ |
|
| 65 |
+ magic, err := buf.Peek(archive.HeaderSize) |
|
| 66 |
+ if err != nil && err != io.EOF {
|
|
| 67 |
+ return nil, "", fmt.Errorf("failed to peek context header from STDIN: %v", err)
|
|
| 68 |
+ } |
|
| 69 |
+ |
|
| 70 |
+ if archive.IsArchive(magic) {
|
|
| 71 |
+ return ioutils.NewReadCloserWrapper(buf, func() error { return r.Close() }), dockerfileName, nil
|
|
| 72 |
+ } |
|
| 73 |
+ |
|
| 74 |
+ // Input should be read as a Dockerfile. |
|
| 75 |
+ tmpDir, err := ioutil.TempDir("", "docker-build-context-")
|
|
| 76 |
+ if err != nil {
|
|
| 77 |
+ return nil, "", fmt.Errorf("unbale to create temporary context directory: %v", err)
|
|
| 78 |
+ } |
|
| 79 |
+ |
|
| 80 |
+ f, err := os.Create(filepath.Join(tmpDir, DefaultDockerfileName)) |
|
| 81 |
+ if err != nil {
|
|
| 82 |
+ return nil, "", err |
|
| 83 |
+ } |
|
| 84 |
+ _, err = io.Copy(f, buf) |
|
| 85 |
+ if err != nil {
|
|
| 86 |
+ f.Close() |
|
| 87 |
+ return nil, "", err |
|
| 88 |
+ } |
|
| 89 |
+ |
|
| 90 |
+ if err := f.Close(); err != nil {
|
|
| 91 |
+ return nil, "", err |
|
| 92 |
+ } |
|
| 93 |
+ if err := r.Close(); err != nil {
|
|
| 94 |
+ return nil, "", err |
|
| 95 |
+ } |
|
| 96 |
+ |
|
| 97 |
+ tar, err := archive.Tar(tmpDir, archive.Uncompressed) |
|
| 98 |
+ if err != nil {
|
|
| 99 |
+ return nil, "", err |
|
| 100 |
+ } |
|
| 101 |
+ |
|
| 102 |
+ return ioutils.NewReadCloserWrapper(tar, func() error {
|
|
| 103 |
+ err := tar.Close() |
|
| 104 |
+ os.RemoveAll(tmpDir) |
|
| 105 |
+ return err |
|
| 106 |
+ }), DefaultDockerfileName, nil |
|
| 107 |
+ |
|
| 108 |
+} |
|
| 109 |
+ |
|
| 110 |
+// GetContextFromGitURL uses a Git URL as context for a `docker build`. The |
|
| 111 |
+// git repo is cloned into a temporary directory used as the context directory. |
|
| 112 |
+// Returns the absolute path to the temporary context directory, the relative |
|
| 113 |
+// path of the dockerfile in that context directory, and a non-nil error on |
|
| 114 |
+// success. |
|
| 115 |
+func GetContextFromGitURL(gitURL, dockerfileName string) (absContextDir, relDockerfile string, err error) {
|
|
| 116 |
+ if _, err := exec.LookPath("git"); err != nil {
|
|
| 117 |
+ return "", "", fmt.Errorf("unable to find 'git': %v", err)
|
|
| 118 |
+ } |
|
| 119 |
+ if absContextDir, err = gitutils.Clone(gitURL); err != nil {
|
|
| 120 |
+ return "", "", fmt.Errorf("unable to 'git clone' to temporary context directory: %v", err)
|
|
| 121 |
+ } |
|
| 122 |
+ |
|
| 123 |
+ return getDockerfileRelPath(absContextDir, dockerfileName) |
|
| 124 |
+} |
|
| 125 |
+ |
|
| 126 |
+// GetContextFromURL uses a remote URL as context for a `docker build`. The |
|
| 127 |
+// remote resource is downloaded as either a Dockerfile or a tar archive. |
|
| 128 |
+// Returns the tar archive used for the context and a path of the |
|
| 129 |
+// dockerfile inside the tar. |
|
| 130 |
+func GetContextFromURL(out io.Writer, remoteURL, dockerfileName string) (io.ReadCloser, string, error) {
|
|
| 131 |
+ response, err := httputils.Download(remoteURL) |
|
| 132 |
+ if err != nil {
|
|
| 133 |
+ return nil, "", fmt.Errorf("unable to download remote context %s: %v", remoteURL, err)
|
|
| 134 |
+ } |
|
| 135 |
+ progressOutput := streamformatter.NewStreamFormatter().NewProgressOutput(out, true) |
|
| 136 |
+ |
|
| 137 |
+ // Pass the response body through a progress reader. |
|
| 138 |
+ progReader := progress.NewProgressReader(response.Body, progressOutput, response.ContentLength, "", fmt.Sprintf("Downloading build context from remote url: %s", remoteURL))
|
|
| 139 |
+ |
|
| 140 |
+ return GetContextFromReader(ioutils.NewReadCloserWrapper(progReader, func() error { return response.Body.Close() }), dockerfileName)
|
|
| 141 |
+} |
|
| 142 |
+ |
|
| 143 |
+// GetContextFromLocalDir uses the given local directory as context for a |
|
| 144 |
+// `docker build`. Returns the absolute path to the local context directory, |
|
| 145 |
+// the relative path of the dockerfile in that context directory, and a non-nil |
|
| 146 |
+// error on success. |
|
| 147 |
+func GetContextFromLocalDir(localDir, dockerfileName string) (absContextDir, relDockerfile string, err error) {
|
|
| 148 |
+ // When using a local context directory, when the Dockerfile is specified |
|
| 149 |
+ // with the `-f/--file` option then it is considered relative to the |
|
| 150 |
+ // current directory and not the context directory. |
|
| 151 |
+ if dockerfileName != "" {
|
|
| 152 |
+ if dockerfileName, err = filepath.Abs(dockerfileName); err != nil {
|
|
| 153 |
+ return "", "", fmt.Errorf("unable to get absolute path to Dockerfile: %v", err)
|
|
| 154 |
+ } |
|
| 155 |
+ } |
|
| 156 |
+ |
|
| 157 |
+ return getDockerfileRelPath(localDir, dockerfileName) |
|
| 158 |
+} |
|
| 159 |
+ |
|
| 160 |
+// getDockerfileRelPath uses the given context directory for a `docker build` |
|
| 161 |
+// and returns the absolute path to the context directory, the relative path of |
|
| 162 |
+// the dockerfile in that context directory, and a non-nil error on success. |
|
| 163 |
+func getDockerfileRelPath(givenContextDir, givenDockerfile string) (absContextDir, relDockerfile string, err error) {
|
|
| 164 |
+ if absContextDir, err = filepath.Abs(givenContextDir); err != nil {
|
|
| 165 |
+ return "", "", fmt.Errorf("unable to get absolute context directory: %v", err)
|
|
| 166 |
+ } |
|
| 167 |
+ |
|
| 168 |
+ // The context dir might be a symbolic link, so follow it to the actual |
|
| 169 |
+ // target directory. |
|
| 170 |
+ // |
|
| 171 |
+ // FIXME. We use isUNC (always false on non-Windows platforms) to workaround |
|
| 172 |
+ // an issue in golang. On Windows, EvalSymLinks does not work on UNC file |
|
| 173 |
+ // paths (those starting with \\). This hack means that when using links |
|
| 174 |
+ // on UNC paths, they will not be followed. |
|
| 175 |
+ if !isUNC(absContextDir) {
|
|
| 176 |
+ absContextDir, err = filepath.EvalSymlinks(absContextDir) |
|
| 177 |
+ if err != nil {
|
|
| 178 |
+ return "", "", fmt.Errorf("unable to evaluate symlinks in context path: %v", err)
|
|
| 179 |
+ } |
|
| 180 |
+ } |
|
| 181 |
+ |
|
| 182 |
+ stat, err := os.Lstat(absContextDir) |
|
| 183 |
+ if err != nil {
|
|
| 184 |
+ return "", "", fmt.Errorf("unable to stat context directory %q: %v", absContextDir, err)
|
|
| 185 |
+ } |
|
| 186 |
+ |
|
| 187 |
+ if !stat.IsDir() {
|
|
| 188 |
+ return "", "", fmt.Errorf("context must be a directory: %s", absContextDir)
|
|
| 189 |
+ } |
|
| 190 |
+ |
|
| 191 |
+ absDockerfile := givenDockerfile |
|
| 192 |
+ if absDockerfile == "" {
|
|
| 193 |
+ // No -f/--file was specified so use the default relative to the |
|
| 194 |
+ // context directory. |
|
| 195 |
+ absDockerfile = filepath.Join(absContextDir, DefaultDockerfileName) |
|
| 196 |
+ |
|
| 197 |
+ // Just to be nice ;-) look for 'dockerfile' too but only |
|
| 198 |
+ // use it if we found it, otherwise ignore this check |
|
| 199 |
+ if _, err = os.Lstat(absDockerfile); os.IsNotExist(err) {
|
|
| 200 |
+ altPath := filepath.Join(absContextDir, strings.ToLower(DefaultDockerfileName)) |
|
| 201 |
+ if _, err = os.Lstat(altPath); err == nil {
|
|
| 202 |
+ absDockerfile = altPath |
|
| 203 |
+ } |
|
| 204 |
+ } |
|
| 205 |
+ } |
|
| 206 |
+ |
|
| 207 |
+ // If not already an absolute path, the Dockerfile path should be joined to |
|
| 208 |
+ // the base directory. |
|
| 209 |
+ if !filepath.IsAbs(absDockerfile) {
|
|
| 210 |
+ absDockerfile = filepath.Join(absContextDir, absDockerfile) |
|
| 211 |
+ } |
|
| 212 |
+ |
|
| 213 |
+ // Evaluate symlinks in the path to the Dockerfile too. |
|
| 214 |
+ // |
|
| 215 |
+ // FIXME. We use isUNC (always false on non-Windows platforms) to workaround |
|
| 216 |
+ // an issue in golang. On Windows, EvalSymLinks does not work on UNC file |
|
| 217 |
+ // paths (those starting with \\). This hack means that when using links |
|
| 218 |
+ // on UNC paths, they will not be followed. |
|
| 219 |
+ if !isUNC(absDockerfile) {
|
|
| 220 |
+ absDockerfile, err = filepath.EvalSymlinks(absDockerfile) |
|
| 221 |
+ if err != nil {
|
|
| 222 |
+ return "", "", fmt.Errorf("unable to evaluate symlinks in Dockerfile path: %v", err)
|
|
| 223 |
+ } |
|
| 224 |
+ } |
|
| 225 |
+ |
|
| 226 |
+ if _, err := os.Lstat(absDockerfile); err != nil {
|
|
| 227 |
+ if os.IsNotExist(err) {
|
|
| 228 |
+ return "", "", fmt.Errorf("Cannot locate Dockerfile: %q", absDockerfile)
|
|
| 229 |
+ } |
|
| 230 |
+ return "", "", fmt.Errorf("unable to stat Dockerfile: %v", err)
|
|
| 231 |
+ } |
|
| 232 |
+ |
|
| 233 |
+ if relDockerfile, err = filepath.Rel(absContextDir, absDockerfile); err != nil {
|
|
| 234 |
+ return "", "", fmt.Errorf("unable to get relative Dockerfile path: %v", err)
|
|
| 235 |
+ } |
|
| 236 |
+ |
|
| 237 |
+ if strings.HasPrefix(relDockerfile, ".."+string(filepath.Separator)) {
|
|
| 238 |
+ return "", "", fmt.Errorf("The Dockerfile (%s) must be within the build context (%s)", givenDockerfile, givenContextDir)
|
|
| 239 |
+ } |
|
| 240 |
+ |
|
| 241 |
+ return absContextDir, relDockerfile, nil |
|
| 242 |
+} |
|
| 243 |
+ |
|
| 244 |
+// isUNC returns true if the path is UNC (one starting \\). It always returns |
|
| 245 |
+// false on Linux. |
|
| 246 |
+func isUNC(path string) bool {
|
|
| 247 |
+ return runtime.GOOS == "windows" && strings.HasPrefix(path, `\\`) |
|
| 248 |
+} |
| ... | ... |
@@ -19,7 +19,6 @@ import ( |
| 19 | 19 |
"time" |
| 20 | 20 |
|
| 21 | 21 |
"github.com/Sirupsen/logrus" |
| 22 |
- "github.com/docker/docker/api" |
|
| 23 | 22 |
"github.com/docker/docker/builder" |
| 24 | 23 |
"github.com/docker/docker/builder/dockerfile/parser" |
| 25 | 24 |
"github.com/docker/docker/pkg/archive" |
| ... | ... |
@@ -604,7 +603,7 @@ func (b *Builder) readDockerfile() error {
|
| 604 | 604 |
// that then look for 'dockerfile'. If neither are found then default |
| 605 | 605 |
// back to 'Dockerfile' and use that in the error message. |
| 606 | 606 |
if b.options.Dockerfile == "" {
|
| 607 |
- b.options.Dockerfile = api.DefaultDockerfileName |
|
| 607 |
+ b.options.Dockerfile = builder.DefaultDockerfileName |
|
| 608 | 608 |
if _, _, err := b.context.Stat(b.options.Dockerfile); os.IsNotExist(err) {
|
| 609 | 609 |
lowercase := strings.ToLower(b.options.Dockerfile) |
| 610 | 610 |
if _, _, err := b.context.Stat(lowercase); err == nil {
|
| ... | ... |
@@ -8,7 +8,6 @@ import ( |
| 8 | 8 |
"io/ioutil" |
| 9 | 9 |
"regexp" |
| 10 | 10 |
|
| 11 |
- "github.com/docker/docker/api" |
|
| 12 | 11 |
"github.com/docker/docker/pkg/archive" |
| 13 | 12 |
"github.com/docker/docker/pkg/httputils" |
| 14 | 13 |
"github.com/docker/docker/pkg/urlutil" |
| ... | ... |
@@ -87,7 +86,7 @@ func DetectContextFromRemoteURL(r io.ReadCloser, remoteURL string, createProgres |
| 87 | 87 |
|
| 88 | 88 |
// dockerfileName is set to signal that the remote was interpreted as a single Dockerfile, in which case the caller |
| 89 | 89 |
// should use dockerfileName as the new name for the Dockerfile, irrespective of any other user input. |
| 90 |
- dockerfileName = api.DefaultDockerfileName |
|
| 90 |
+ dockerfileName = DefaultDockerfileName |
|
| 91 | 91 |
|
| 92 | 92 |
// TODO: return a context without tarsum |
| 93 | 93 |
return archive.Generate(dockerfileName, string(dockerfile)) |