diff --git a/cli/command/image/build.go b/cli/command/image/build.go index 27b6cafd87..dd7510c440 100644 --- a/cli/command/image/build.go +++ b/cli/command/image/build.go @@ -34,8 +34,6 @@ import ( "github.com/spf13/cobra" ) -var errStdinConflict = errors.New("invalid argument: can't use stdin for both build context and dockerfile") - type buildOptions struct { context string dockerfileName string @@ -189,7 +187,7 @@ func runBuild(dockerCli command.Cli, options buildOptions) error { if options.dockerfileFromStdin() { if options.contextFromStdin() { - return errStdinConflict + return errors.New("invalid argument: can't use stdin for both build context and dockerfile") } dockerfileCtx = dockerCli.In() } diff --git a/cli/command/image/build/context.go b/cli/command/image/build/context.go index 747c23c7e0..fc1e7b64d4 100644 --- a/cli/command/image/build/context.go +++ b/cli/command/image/build/context.go @@ -155,6 +155,9 @@ func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadC if dockerfileName == "-" { return nil, "", errors.New("build context is not an archive") } + if dockerfileName != "" { + return nil, "", errors.New("ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles") + } dockerfileDir, err := WriteTempDockerfile(rc) if err != nil { diff --git a/cli/command/image/build/context_test.go b/cli/command/image/build/context_test.go index 15686e0be4..63cfaa0e12 100644 --- a/cli/command/image/build/context_test.go +++ b/cli/command/image/build/context_test.go @@ -152,6 +152,13 @@ func TestGetContextFromReaderString(t *testing.T) { } } +func TestGetContextFromReaderStringConflict(t *testing.T) { + rdr, relDockerfile, err := GetContextFromReader(io.NopCloser(strings.NewReader(dockerfileContents)), "custom.Dockerfile") + assert.Check(t, is.Equal(rdr, nil)) + assert.Check(t, is.Equal(relDockerfile, "")) + assert.Check(t, is.ErrorContains(err, "ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles")) +} + func TestGetContextFromReaderTar(t *testing.T) { contextDir := createTestTempDir(t) createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents)