Here’s something I use on personal projects:
.dockerignore:
*
!src
!package.json
!package-lock.json
!tsconfig.json Notice that we ignore everything with *, then opt-in files via the ! prefix.
Why?
Prepending lines with ! makes .dockerignore files opt-in, not opt-out. You’re telling it what you want, not what you don’t want.
With opt-out it’s easy to be forgetful. Does your .dockerignore contain .git? Have you added node_modules? .env? What could you have forgotten?
Opt-in 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 opt-out 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.
Our tools don’t make it easy to manage Docker context bloat.
It takes intentional effort that I’d prefer to avoid.