#best-practice #docker

If you are using Git to do version control, you might have noticed that when you build your Docker image from your repository, the docker build context is a lot bigger than expected:

1docker build -t geoip-server .
2Sending build context to Docker daemon   42.6MB

In this scenario, there are only a couple of files in the repo, so why is the build context more than 40 MB?

After some investigation, it turns out the explanation is simple: the .dockerignore file was not configured correctly. I had a .dockerignore file with the following content:

 1LICENSE
 2README.md
 3Makefile
 4**/testdata*
 5geoip-server
 6database.mmdb
 7database.mmdb.md5
 8db-downloader
 9GeoLite2-City.mmdb
10GeoLite2-City.mmdb.md5

I thought I had covered everything, but there is a (big) hidden folder which wasn't listed, the .git folder. You cannot see it in your Finder but it's there and it's sometimes bigger than you think. So, after updating the .dockerignore file with the .git folder added, the final ignore file looks as follows:

 1.git
 2.gitignore
 3LICENSE
 4README.md
 5Makefile
 6**/testdata*
 7geoip-server
 8database.mmdb
 9database.mmdb.md5
10db-downloader
11GeoLite2-City.mmdb
12GeoLite2-City.mmdb.md5

When you then do a build, you'll see that the context is a lot smaller and the build is a lot faster:

1docker build -t geoip-server .
2Sending build context to Docker daemon  116.2kB

PS: the Dockerfile I'm using for testing is using a multi-stage build to avoid the re-download of the Go modules every time I do a build. You can find the full Dockerfile here.