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)