From c2535aa46744e6e75fe263d8b150b245b687c100 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 13 Jun 2023 14:03:03 +0200 Subject: [PATCH] build: error if Dockerfile name is passed with Dockerfile from stdin When passing a Dockerfile through stdin, it's not possible to specify the name of the Dockerfile (using the `-f` option). When building with BuildKit enabled, an error is already produced for this case, but the classic builder silently ignored it. This patch adds an error for this situation: echo -e 'FROM busybox' | DOCKER_BUILDKIT=0 docker build -f some.Dockerfile - DEPRECATED: The legacy builder is deprecated and will be removed in a future release. BuildKit is currently disabled; enable it by removing the DOCKER_BUILDKIT=0 environment-variable. unable to prepare context: ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles Signed-off-by: Sebastiaan van Stijn --- cli/command/image/build/context.go | 3 +++ cli/command/image/build/context_test.go | 7 +++++++ 2 files changed, 10 insertions(+) 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)