忽略.dockerignore中的所有.git文件夹

问题描述 投票:0回答:1

我有一个项目有几个git克隆,每个文件夹将有他的.git和一些文件夹在文件夹中有克隆

那么可以忽略所有.git文件夹吗?

我按照文档中的描述测试了很多规则,但我只是忽略了顶层文件夹。

docker docker-build
1个回答
7
投票

使用**模式就足够了。例如:

.dockerignore

**/.git

来自that page of the official doc的相关段落是:

.dockerignore file

[…]

匹配是使用Go的filepath.Match规则完成的。预处理步骤删除前导和尾随空格,并使用Go的.消除..filepath.Clean元素。忽略预处理后为空白的行。

除了Go的filepath.Match规则,Docker还支持一个特殊的通配符字符串**,它匹配任意数量的目录(包括零)。例如,**/*.go将排除在所有目录中找到的以.go结尾的所有文件,包括构建上下文的根。

[…]

Working example

这是Debian GNU / Linux下Docker CE的完整会话:

$ 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
© www.soinside.com 2019 - 2024. All rights reserved.