From bfd9613e5f97f8ecc9bc3b0033fc0b6ce271fba4 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 24 Jan 2017 14:19:31 +0100 Subject: [PATCH] do not ignore local build-contexts starting with "github.com" Docker special-cases build-contexts starting with `github.com`, and treats them as remote URLs. Because of this special treatment, local build contexts in a directory named "github.com" are ignored by `docker build`. This patch changes the way the build-context is detected and first checks if a local path with the given name exists before considering it to be a remote URL. Before this change; $ mkdir -p github.com/foo/bar && echo -e "FROM scratch\nLABEL iam=local" > github.com/foo/bar/Dockerfile $ docker build -t dont-ignore-me github.com/foo/bar Username for 'https://github.com': After this change; $ mkdir -p github.com/foo/bar && echo -e "FROM scratch\nLABEL iam=local" > github.com/foo/bar/Dockerfile $ docker build -t dont-ignore-me github.com/foo/bar Sending build context to Docker daemon 2.048 kB Step 1/2 : FROM scratch ---> Step 2/2 : LABEL iam local ---> Using cache ---> ae2c603fe970 Successfully built ae2c603fe970 Signed-off-by: Sebastiaan van Stijn --- command/image/build.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/command/image/build.go b/command/image/build.go index 3c92ba20b9..fe903c74ef 100644 --- a/command/image/build.go +++ b/command/image/build.go @@ -156,12 +156,14 @@ func runBuild(dockerCli *command.DockerCli, options buildOptions) error { switch { case specifiedContext == "-": buildCtx, relDockerfile, err = build.GetContextFromReader(dockerCli.In(), options.dockerfileName) + case isLocalDir(specifiedContext): + contextDir, relDockerfile, err = build.GetContextFromLocalDir(specifiedContext, options.dockerfileName) case urlutil.IsGitURL(specifiedContext): tempDir, relDockerfile, err = build.GetContextFromGitURL(specifiedContext, options.dockerfileName) case urlutil.IsURL(specifiedContext): buildCtx, relDockerfile, err = build.GetContextFromURL(progBuff, specifiedContext, options.dockerfileName) default: - contextDir, relDockerfile, err = build.GetContextFromLocalDir(specifiedContext, options.dockerfileName) + return fmt.Errorf("unable to prepare context: path %q not found", specifiedContext) } if err != nil { @@ -356,6 +358,11 @@ func runBuild(dockerCli *command.DockerCli, options buildOptions) error { return nil } +func isLocalDir(c string) bool { + _, err := os.Stat(c) + return err == nil +} + type translatorFunc func(context.Context, reference.NamedTagged) (reference.Canonical, error) // validateTag checks if the given image name can be resolved.