How to ADD all files/directories except a hidden directory like .git in Dockerfile

Larry Cai picture Larry Cai · Jan 22, 2015 · Viewed 46.3k times · Source

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

Answer

Mykola Gurov picture Mykola Gurov · Jan 22, 2015

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