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:
docker build -t geoip-server . Sending 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:
LICENSE README.md Makefile **/testdata* geoip-server database.mmdb database.mmdb.md5 db-downloader GeoLite2-City.mmdb GeoLite2-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:
.git .gitignore LICENSE README.md Makefile **/testdata* geoip-server database.mmdb database.mmdb.md5 db-downloader GeoLite2-City.mmdb GeoLite2-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:
docker build -t geoip-server . Sending 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.