diff --git a/builder/dockerfile/parser/parser.go b/builder/dockerfile/parser/parser.go index 683f30f68a..48aee99103 100644 --- a/builder/dockerfile/parser/parser.go +++ b/builder/dockerfile/parser/parser.go @@ -3,6 +3,7 @@ package parser import ( "bufio" + "bytes" "fmt" "io" "regexp" @@ -152,8 +153,14 @@ func Parse(rwc io.Reader) (*Node, error) { root.StartLine = -1 scanner := bufio.NewScanner(rwc) + utf8bom := []byte{0xEF, 0xBB, 0xBF} for scanner.Scan() { - scannedLine := strings.TrimLeftFunc(scanner.Text(), unicode.IsSpace) + scannedBytes := scanner.Bytes() + // We trim UTF8 BOM + if currentLine == 0 { + scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom) + } + scannedLine := strings.TrimLeftFunc(string(scannedBytes), unicode.IsSpace) currentLine++ line, child, err := ParseLine(scannedLine) if err != nil { diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index 8b74b4d6cb..d8c5a31014 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -6791,3 +6791,17 @@ foo2 c.Fatal(err) } } + +// Test case for #23221 +func (s *DockerSuite) TestBuildWithUTF8BOM(c *check.C) { + name := "test-with-utf8-bom" + dockerfile := []byte(`FROM busybox`) + bomDockerfile := append([]byte{0xEF, 0xBB, 0xBF}, dockerfile...) + ctx, err := fakeContextFromNewTempDir() + c.Assert(err, check.IsNil) + defer ctx.Close() + err = ctx.addFile("Dockerfile", bomDockerfile) + c.Assert(err, check.IsNil) + _, err = buildImageFromContext(name, ctx, true) + c.Assert(err, check.IsNil) +}