我有一个项目有几个 git 克隆,每个文件夹都有他的 .git 并且一些文件夹在文件夹内有克隆
那么可以忽略所有 .git 文件夹吗?
我测试了文档中描述的许多规则,但我只设法忽略了顶部文件夹。
最佳答案
使用 **
就足够了图案。例如:
.dockerignore
**/.git
相关文章来自 that page of the official doc是:
.dockerignore file
[…]
Matching is done using Go’s filepath.Match rules. A preprocessing step removes leading and trailing whitespace and eliminates
.
and..
elements using Go’s filepath.Clean. Lines that are blank after preprocessing are ignored.Beyond Go’s filepath.Match rules, Docker also supports a special wildcard string
**
that matches any number of directories (including zero). For example,**/*.go
will exclude all files that end with.go
that are found in all directories, including the root of the build context.[…]
工作示例
这是在 Debian GNU/Linux 下与 Docker CE 的完整 session :
$ docker version
Client:
Version: 18.09.2
API version: 1.39
Go version: go1.10.6
Git commit: 6247962
Built: Sun Feb 10 04:13:52 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.2
API version: 1.39 (minimum version 1.12)
Go version: go1.10.6
Git commit: 6247962
Built: Sun Feb 10 03:42:13 2019
OS/Arch: linux/amd64
Experimental: false
考虑以下工作目录:
$ tree -a
.
├── a
│ ├── .git
│ │ └── c
│ └── go
│ └── 1
├── b
│ ├── c
│ │ ├── .git
│ │ │ └── c
│ │ └── go
│ │ └── 1
│ ├── .git
│ │ └── c
│ └── go
│ └── 1
├── Dockerfile
├── .dockerignore
├── .git
│ └── c
└── go
└── 1
以及以下源文件:
$ cat .dockerignore
**/.git
$ cat Dockerfile
FROM debian
WORKDIR /app
COPY . .
CMD ls -Rhal
然后我得到:
$ docker build -t test .
[…]
$ docker run --rm -it test
.:
total 28K
drwxr-xr-x 1 root root 4.0K Feb 20 19:40 .
drwxr-xr-x 1 root root 4.0K Feb 20 19:43 ..
-rw-r--r-- 1 root root 8 Feb 20 19:38 .dockerignore
-rw-r--r-- 1 root root 50 Feb 20 19:40 Dockerfile
drwxr-xr-x 3 root root 4.0K Feb 20 19:39 a
drwxr-xr-x 4 root root 4.0K Feb 20 19:39 b
drwx------ 2 root root 4.0K Feb 20 19:40 go
./a:
total 12K
drwxr-xr-x 3 root root 4.0K Feb 20 19:39 .
drwxr-xr-x 1 root root 4.0K Feb 20 19:40 ..
drwxr-xr-x 2 root root 4.0K Feb 20 19:39 go
./a/go:
total 8.0K
drwxr-xr-x 2 root root 4.0K Feb 20 19:39 .
drwxr-xr-x 3 root root 4.0K Feb 20 19:39 ..
-rw-r--r-- 1 root root 0 Feb 20 19:39 1
./b:
total 16K
drwxr-xr-x 4 root root 4.0K Feb 20 19:39 .
drwxr-xr-x 1 root root 4.0K Feb 20 19:40 ..
drwxr-xr-x 3 root root 4.0K Feb 20 19:39 c
drwxr-xr-x 2 root root 4.0K Feb 20 19:39 go
./b/c:
total 12K
drwxr-xr-x 3 root root 4.0K Feb 20 19:39 .
drwxr-xr-x 4 root root 4.0K Feb 20 19:39 ..
drwxr-xr-x 2 root root 4.0K Feb 20 19:39 go
./b/c/go:
total 8.0K
drwxr-xr-x 2 root root 4.0K Feb 20 19:39 .
drwxr-xr-x 3 root root 4.0K Feb 20 19:39 ..
-rw-r--r-- 1 root root 0 Feb 20 19:39 1
./b/go:
total 8.0K
drwxr-xr-x 2 root root 4.0K Feb 20 19:39 .
drwxr-xr-x 4 root root 4.0K Feb 20 19:39 ..
-rw-r--r-- 1 root root 0 Feb 20 19:39 1
./go:
total 8.0K
drwx------ 2 root root 4.0K Feb 20 19:40 .
drwxr-xr-x 1 root root 4.0K Feb 20 19:40 ..
-rw-r--r-- 1 root root 0 Feb 20 19:40 1
关于docker - 忽略 .dockerignore 中的所有 .git 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54793349/