⛴️ Invert your .dockerignore for great profit
Here are two .dockerignore files:
Default allow (old, busted)
data
node_modules
.dockerignore
.env
.env.example
.gitignore
.git
.prettierignore
.prettierrc
Dockerfile
eslint.config.js
mise.tomlDefault ignore (new, saucy)
* # Ignore everything, except:
!src
!package.json
!package-lock.json
!tsconfig.json
On the left we’re saying what we don’t want in our image. It’s the default style.
On the right we’re explicit about what we do want.
Why?
With default allow it’s easy to be forgetful. Does your .dockerignore contain .git?
You probably remembered to add node_modules because builds were slow, but then you
have to ask… what else have you forgotten?
Default ignore is:
- Easier to reason about. This is the main thing for me. We have tools like dive to explore image bloat, perhaps we even run it once in a while. Or… use default ignore and be explicit about what matters.
- Faster builds: less content in your Docker context makes for faster builds.
- Quicker deployments during build push and node pull.
- Improved security - less chance of unexpected files appearing in your images - e.g
.env,.git
Our tools don’t make it easy to manage Docker context bloat.
It takes intentional effort that I’d prefer to avoid.