From 77dd05caad3be37e0c4c09d8efebfad21e607671 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 13 Jun 2023 13:47:56 +0200 Subject: [PATCH 1/2] cli/command/image: remove errStdinConflict This error was only used in a single location, so no need to define a package-level variable for this. Signed-off-by: Sebastiaan van Stijn --- cli/command/image/build.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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() } From c2535aa46744e6e75fe263d8b150b245b687c100 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 13 Jun 2023 14:03:03 +0200 Subject: [PATCH 2/2] 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)