Skip to content

Commit ea86320

Browse files
committed
Skip UTF-8 BOM bytes from Dockerignore if exist
This fix tries to address issues related to moby#23221 where Dockerignore may consists of UTF-8 BOM. This likely happens when Notepad tries to save a file as UTF-8 in Windows. This fix skips the UTF-8 BOM bytes from the beginning of the Dockerignore if exists. Additional tests has been added to cover the changes in this fix. This fix is related to moby#23221 (UTF-8 BOM in Dockerfile). Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
1 parent 678c80f commit ea86320

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

builder/dockerignore/dockerignore.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dockerignore
22

33
import (
44
"bufio"
5+
"bytes"
56
"fmt"
67
"io"
78
"path/filepath"
@@ -18,10 +19,18 @@ func ReadAll(reader io.ReadCloser) ([]string, error) {
1819
defer reader.Close()
1920
scanner := bufio.NewScanner(reader)
2021
var excludes []string
22+
currentLine := 0
2123

24+
utf8bom := []byte{0xEF, 0xBB, 0xBF}
2225
for scanner.Scan() {
26+
scannedBytes := scanner.Bytes()
27+
// We trim UTF8 BOM
28+
if currentLine == 0 {
29+
scannedBytes = bytes.TrimPrefix(scannedBytes, utf8bom)
30+
}
31+
pattern := string(scannedBytes)
32+
currentLine++
2333
// Lines starting with # (comments) are ignored before processing
24-
pattern := scanner.Text()
2534
if strings.HasPrefix(pattern, "#") {
2635
continue
2736
}

integration-cli/docker_cli_build_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6805,3 +6805,27 @@ func (s *DockerSuite) TestBuildWithUTF8BOM(c *check.C) {
68056805
_, err = buildImageFromContext(name, ctx, true)
68066806
c.Assert(err, check.IsNil)
68076807
}
6808+
6809+
// Test case for UTF-8 BOM in .dockerignore, related to #23221
6810+
func (s *DockerSuite) TestBuildWithUTF8BOMDockerignore(c *check.C) {
6811+
name := "test-with-utf8-bom-dockerignore"
6812+
dockerfile := `
6813+
FROM busybox
6814+
ADD . /tmp/
6815+
RUN ls -la /tmp
6816+
RUN sh -c "! ls /tmp/Dockerfile"
6817+
RUN ls /tmp/.dockerignore`
6818+
dockerignore := []byte("./Dockerfile\n")
6819+
bomDockerignore := append([]byte{0xEF, 0xBB, 0xBF}, dockerignore...)
6820+
ctx, err := fakeContext(dockerfile, map[string]string{
6821+
"Dockerfile": dockerfile,
6822+
})
6823+
c.Assert(err, check.IsNil)
6824+
defer ctx.Close()
6825+
err = ctx.addFile(".dockerignore", bomDockerignore)
6826+
c.Assert(err, check.IsNil)
6827+
_, err = buildImageFromContext(name, ctx, true)
6828+
if err != nil {
6829+
c.Fatal(err)
6830+
}
6831+
}

0 commit comments

Comments
 (0)