Skip to content

Commit c2535aa

Browse files
committed
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 <github@gone.nl>
1 parent 77dd05c commit c2535aa

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

cli/command/image/build/context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ func GetContextFromReader(rc io.ReadCloser, dockerfileName string) (out io.ReadC
155155
if dockerfileName == "-" {
156156
return nil, "", errors.New("build context is not an archive")
157157
}
158+
if dockerfileName != "" {
159+
return nil, "", errors.New("ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles")
160+
}
158161

159162
dockerfileDir, err := WriteTempDockerfile(rc)
160163
if err != nil {

cli/command/image/build/context_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ func TestGetContextFromReaderString(t *testing.T) {
152152
}
153153
}
154154

155+
func TestGetContextFromReaderStringConflict(t *testing.T) {
156+
rdr, relDockerfile, err := GetContextFromReader(io.NopCloser(strings.NewReader(dockerfileContents)), "custom.Dockerfile")
157+
assert.Check(t, is.Equal(rdr, nil))
158+
assert.Check(t, is.Equal(relDockerfile, ""))
159+
assert.Check(t, is.ErrorContains(err, "ambiguous Dockerfile source: both stdin and flag correspond to Dockerfiles"))
160+
}
161+
155162
func TestGetContextFromReaderTar(t *testing.T) {
156163
contextDir := createTestTempDir(t)
157164
createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents)

0 commit comments

Comments
 (0)