From 8913dace341c8fc9f9a3ab9518c8521c161b307f Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Wed, 1 Jun 2016 15:20:54 -0700 Subject: [PATCH] Add support for comment in .dockerignore This fix tries to address the issue raised in #20083 where comment is not supported in `.dockerignore`. This fix updated the processing of `.dockerignore` so that any lines starting with `#` are ignored, which is similiar to the behavior of `.gitignore`. Related documentation has been updated. Additional tests have been added to cover the changes. This fix fixes #20083. Signed-off-by: Yong Tang --- builder/dockerignore/dockerignore.go | 7 ++++- docs/reference/builder.md | 5 ++++ integration-cli/docker_cli_build_test.go | 36 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/builder/dockerignore/dockerignore.go b/builder/dockerignore/dockerignore.go index 1fed3199af..4e09b05b0a 100644 --- a/builder/dockerignore/dockerignore.go +++ b/builder/dockerignore/dockerignore.go @@ -20,7 +20,12 @@ func ReadAll(reader io.ReadCloser) ([]string, error) { var excludes []string for scanner.Scan() { - pattern := strings.TrimSpace(scanner.Text()) + // Lines starting with # (comments) are ignored before processing + pattern := scanner.Text() + if strings.HasPrefix(pattern, "#") { + continue + } + pattern = strings.TrimSpace(pattern) if pattern == "" { continue } diff --git a/docs/reference/builder.md b/docs/reference/builder.md index f460338b22..35d5dfd495 100644 --- a/docs/reference/builder.md +++ b/docs/reference/builder.md @@ -379,9 +379,13 @@ the working and the root directory. For example, the patterns in the `foo` subdirectory of `PATH` or in the root of the git repository located at `URL`. Neither excludes anything else. +If a line in `.dockerignore` file starts with `#` in column 1, then this line is +considered as a comment and is ignored before interpreted by the CLI. + Here is an example `.dockerignore` file: ``` +# comment */temp* */*/temp* temp? @@ -391,6 +395,7 @@ This file causes the following build behavior: | Rule | Behavior | |----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `# comment` | Ignored. | | `*/temp*` | Exclude files and directories whose names start with `temp` in any immediate subdirectory of the root. For example, the plain file `/somedir/temporary.txt` is excluded, as is the directory `/somedir/temp`. | | `*/*/temp*` | Exclude files and directories starting with `temp` from any subdirectory that is two levels below the root. For example, `/somedir/subdir/temporary.txt` is excluded. | | `temp?` | Exclude files and directories in the root directory whose names are a one-character extension of `temp`. For example, `/tempa` and `/tempb` are excluded. diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index 0695138519..8b74b4d6cb 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -6755,3 +6755,39 @@ func (s *DockerSuite) TestBuildDeleteCommittedFile(c *check.C) { c.Fatal(err) } } + +// #20083 +func (s *DockerSuite) TestBuildDockerignoreComment(c *check.C) { + name := "testbuilddockerignorecleanpaths" + dockerfile := ` + FROM busybox + ADD . /tmp/ + RUN sh -c "(ls -la /tmp/#1)" + RUN sh -c "(! ls -la /tmp/#2)" + RUN sh -c "(! ls /tmp/foo) && (! ls /tmp/foo2) && (ls /tmp/dir1/foo)"` + ctx, err := fakeContext(dockerfile, map[string]string{ + "foo": "foo", + "foo2": "foo2", + "dir1/foo": "foo in dir1", + "#1": "# file 1", + "#2": "# file 2", + ".dockerignore": `# Visual C++ cache files +# because we have git ;-) +# The above comment is from #20083 +foo +#dir1/foo +foo2 +# The following is considered as comment as # is at the beginning +#1 +# The following is not considered as comment as # is not at the beginning + #2 +`, + }) + if err != nil { + c.Fatal(err) + } + defer ctx.Close() + if _, err := buildImageFromContext(name, ctx, true); err != nil { + c.Fatal(err) + } +}