【问题标题】:ignore all .git folders in .dockerignore忽略 .dockerignore 中的所有 .git 文件夹
【发布时间】:2019-07-14 13:21:28
【问题描述】:

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

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

我测试了文档中描述的许多规则,但我只设法忽略了顶层文件夹。

【问题讨论】:

    标签: docker docker-build


    【解决方案1】:

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

    .dockerignore

    **/.git
    

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

    .dockerignore 文件

    […]

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

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

    […]

    工作示例

    这是在 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
    

    【讨论】:

    • 我试过了,它只在顶部文件夹中删除,文件夹(假名)'extension/.git'仍然存在,所有子文件夹都一样。
    • @MateusSilva 我很困惑地读到你无法让它在你的环境中工作......我认为这应该工作,我编辑了我的答案以包含一个完整的会话示例功能...
    • 您的.dockerignore 中还有其他行吗? (您在问题中没有提到这一点)另外,您是否有只有一个 Dockerfile 并且您的 Dockerfile 是否与顶级 .dockerignore 放在同一文件夹中?跨度>
    • 一个 dockerfile 和 dockerignore 在同一个文件夹中,我只创建了 dockerignore 来忽略 git,所以我只测试了这一行,我使用的是 macos mojave,我会尝试重新创建并在这里发布确切的过程。
    猜你喜欢
    • 2018-09-17
    • 2016-04-09
    • 1970-01-01
    • 2018-06-29
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 2018-08-31
    相关资源
    最近更新 更多