Docker Ignore

Limiting what is visible to a build

One feature that is widely ignored is .dockerignore but implementing this can speed up your builds by limiting what is sent to docker before the build starts.

The format of .dockerignore is pretty simple & is documented here but it can be pretty basic.

For example, this site is built by a docker container however we only need two directories & a single file to be passed to docker to perform the build so our .dockerignore is pretty basic:

1*
2!bin
3!tools
4!go.mod

Here we first exclude everything with * on line 1 then add exclusion rules (lines starting with !) to include the bin & tools directories and the go.mod file.

The speed improvement is noticeable. For this site it's currently 300MB but with those four simple lines only 207KB is now sent to docker instead of everything.

You'll note that Dockerfile isn't passed to the build context as it's not needed there. It's actually advised to never send it to the build context, something most dev's don't realise.