77

One of things we do often is to package all source code in Dockerfile when we build a Docker image.

ADD . /app

How can we avoid including the .git directory in simple way ?

I tried the Unix way of handling this using ADD [^.]* /app/

Complete sample:

docker@boot2docker:/mnt/sda1/tmp/abc$ find .
.
./c
./.git
./Dockerfile
./good
./good/a1
docker@boot2docker:/mnt/sda1/tmp/abc$ cat Dockerfile
FROM ubuntu

ADD [^.]* /app/
docker@boot2docker:/mnt/sda1/tmp/abc$ docker build -t abc .
Sending build context to Docker daemon 4.096 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu
 ---> 04c5d3b7b065
Step 1 : ADD [^.]* /app/
d ---> 5d67603f108b
Removing intermediate container 60159dee6ac8
Successfully built 5d67603f108b
docker@boot2docker:/mnt/sda1/tmp/abc$ docker run -it abc
root@1b1705dd66a2:/# ls -l app
total 4
-rw-r--r-- 1 1000 staff 30 Jan 22 01:18 Dockerfile
-rw-r--r-- 1 root root   0 Jan 22 01:03 a1
-rw-r--r-- 1 root root   0 Jan 22 00:10 c

And secondly, it will lose the directory structure, since good\a1 gets changed to a1.

Related source code in Docker is https://github.com/docker/docker/blob/eaecf741f0e00a09782d5bcf16159cc8ea258b67/builder/internals.go#L115

Michael M
  • 8,185
  • 2
  • 35
  • 51
Larry Cai
  • 55,923
  • 34
  • 110
  • 156

4 Answers4

97

You may exclude unwanted files with the help of the .dockerignore file

Mykola Gurov
  • 8,517
  • 4
  • 29
  • 27
47

How can we avoid including the .git directory in simple way ?

Just create a file called .dockerignore in the root context folder with the following lines

**/.git
**/node_modules

With such lines Docker will exclude directories .git and node_modules from any subdirectory including root. Docker also supports a special wildcard string ** that matches any number of directories (including zero).

And secondly, it will lose the directory structure, since good\a1 gets changed to a1

With .dockerignore it won't

$ docker run -it --rm sample tree /opt/
/opt/
├── Dockerfile
├── c
│   └── no_sslv2.patch
└── good
    └── a1
        └── README

3 directories, 3 files

Reference to official docs: .dockerignore

Paolo
  • 20,112
  • 21
  • 72
  • 113
ALex_hha
  • 1,345
  • 15
  • 16
2

Because .gitignore and .dockerignore do not use the same syntax, I ended up running this before build:

git ls-files --others --ignored --exclude-standard --directory > .dockerignore
printf "%s\n" .git >> .dockerignore

In this way I get a fresh list of actually ignored stuff from single source - what Git itself ignores. I think this is the most reliable way.

akostadinov
  • 17,364
  • 6
  • 77
  • 85
  • This should be the best solution. PS: why not just `echo '.git' >> .dockerignore` ? – link89 May 11 '23 at 06:41
  • 2
    @link89, you can. Probably initially I was thinking about `"\n%s\n"` to make sure that `.dockerignore` doesn't lack a new line character at end. And `echo` respecting escape sequences is non POSIX compliant. But in the two-liner above, you can safely use `echo`. – akostadinov May 11 '23 at 20:27
0

Add .dockerignore file in your root directory (syntax like the .gitignore file)