ignore all .git folders in .dockerignore

Mateus Silva picture Mateus Silva · Feb 20, 2019 · Viewed 11.4k times · Source

I have a project that have a few git clones, every folder will have his .git and some folder has clones inside folders

So it's possible to ignore all .git folders?

I tested many rules as described in the docs, but I only managed to ignore on the top folder.

Answer

ErikMD picture ErikMD · Feb 20, 2019

It suffices to use the ** pattern. For example:

.dockerignore

**/.git

The relevant passage from that page of the official doc is:

.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.

[…]

Working example

Here is a complete session with Docker CE under Debian GNU/Linux:

$ 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

Consider the following working directory:

$ tree -a
.
├── a
│   ├── .git
│   │   └── c
│   └── go
│       └── 1
├── b
│   ├── c
│   │   ├── .git
│   │   │   └── c
│   │   └── go
│   │       └── 1
│   ├── .git
│   │   └── c
│   └── go
│       └── 1
├── Dockerfile
├── .dockerignore
├── .git
│   └── c
└── go
    └── 1

And the following source files:

$ cat .dockerignore
**/.git

$ cat Dockerfile
FROM debian

WORKDIR /app

COPY . .

CMD ls -Rhal

Then I get:

$ 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