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

标签 docker docker-build

我有一个项目有几个 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/

相关文章:

go - 通过 api 网关的 http 链接的 docker 容器之间的通信

Docker compose 无法识别 'env_file'

python-3.x - 无法在 docker 中安装 pyodbc 并收到错误命令 'gcc' 失败,退出状态 1

docker - 如何删除 Docker 容器内已删除的文件?

wordpress - WordPress Docker镜像错误-拒绝连接-在插件详细信息预览期间

docker - Kubernetes重复的pod kube-system/metrics-server收到错误

docker - JFrog Artifactory 使用什么 token 端点用于其 Docker 注册表?

php - Docker 没有 MySQL 连接到 Nginx

docker - 运行并行docker构建会给出错误消息->删除中间容器<id>时出错:容器<id>:驱动程序 “windowsfilter”失败

docker - 如何创建大于 20GB 的 windows docker 镜像?