Skip to content

Commit c96484a

Browse files
authored
Merge pull request #4346 from thaJeztah/build_errors
build: error if Dockerfile name is passed with Dockerfile from stdin
2 parents 3c99d22 + c2535aa commit c96484a

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

cli/command/image/build.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ import (
3434
"github.com/spf13/cobra"
3535
)
3636

37-
var errStdinConflict = errors.New("invalid argument: can't use stdin for both build context and dockerfile")
38-
3937
type buildOptions struct {
4038
context string
4139
dockerfileName string
@@ -189,7 +187,7 @@ func runBuild(dockerCli command.Cli, options buildOptions) error {
189187

190188
if options.dockerfileFromStdin() {
191189
if options.contextFromStdin() {
192-
return errStdinConflict
190+
return errors.New("invalid argument: can't use stdin for both build context and dockerfile")
193191
}
194192
dockerfileCtx = dockerCli.In()
195193
}

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)