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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2017-01-24 14:19:31 +01:00
parent f35f889c10
commit bfd9613e5f
1 changed files with 8 additions and 1 deletions

View File

@ -156,12 +156,14 @@ func runBuild(dockerCli *command.DockerCli, options buildOptions) error {
switch { switch {
case specifiedContext == "-": case specifiedContext == "-":
buildCtx, relDockerfile, err = build.GetContextFromReader(dockerCli.In(), options.dockerfileName) buildCtx, relDockerfile, err = build.GetContextFromReader(dockerCli.In(), options.dockerfileName)
case isLocalDir(specifiedContext):
contextDir, relDockerfile, err = build.GetContextFromLocalDir(specifiedContext, options.dockerfileName)
case urlutil.IsGitURL(specifiedContext): case urlutil.IsGitURL(specifiedContext):
tempDir, relDockerfile, err = build.GetContextFromGitURL(specifiedContext, options.dockerfileName) tempDir, relDockerfile, err = build.GetContextFromGitURL(specifiedContext, options.dockerfileName)
case urlutil.IsURL(specifiedContext): case urlutil.IsURL(specifiedContext):
buildCtx, relDockerfile, err = build.GetContextFromURL(progBuff, specifiedContext, options.dockerfileName) buildCtx, relDockerfile, err = build.GetContextFromURL(progBuff, specifiedContext, options.dockerfileName)
default: default:
contextDir, relDockerfile, err = build.GetContextFromLocalDir(specifiedContext, options.dockerfileName) return fmt.Errorf("unable to prepare context: path %q not found", specifiedContext)
} }
if err != nil { if err != nil {
@ -356,6 +358,11 @@ func runBuild(dockerCli *command.DockerCli, options buildOptions) error {
return nil return nil
} }
func isLocalDir(c string) bool {
_, err := os.Stat(c)
return err == nil
}
type translatorFunc func(context.Context, reference.NamedTagged) (reference.Canonical, error) type translatorFunc func(context.Context, reference.NamedTagged) (reference.Canonical, error)
// validateTag checks if the given image name can be resolved. // validateTag checks if the given image name can be resolved.