How .dockerignore speeds up builds
Why the build context matters, what to exclude for smaller images, and how deduplicated stack presets keep a .dockerignore readable.
What the build context really is
When you run docker build, the client first packages the directory you point it at and sends that bundle, the build context, to the daemon. Every file in that folder travels across even if the Dockerfile never copies it. A repository carrying node_modules, a .git folder and gigabytes of build output can make the context huge, which slows the initial transfer and bloats the layer cache. A .dockerignore trims that bundle before it is ever sent.
What is safe to exclude
The safest exclusions are files that are regenerated inside the image or that must never ship in one. Dependency folders like node_modules and Python virtual environments are rebuilt by your install step, so copying them in only risks platform mismatches. Version control metadata, editor caches and .DS_Store add noise with no runtime value. Most importantly, .env files and secret material should be ignored so a wildcard COPY cannot bake credentials into a published image.
Why deduplicated presets help
Hand-written ignore files drift over time as teams paste in rules from several sources, leaving the same pattern repeated under different comments. This generator emits one labelled block per stack and removes any pattern a later stack repeats, so a Node plus Python selection lists dist and build only once, under Node. Custom lines land in their own final block and are checked against everything above them. The result is a short, comment-organised file that a reviewer can actually read.
Placing and testing the file
Save the .dockerignore at the root of your build context, the same directory you pass to docker build, which is usually beside the Dockerfile. Docker matches its patterns from that root, so a bare pattern like dist targets the top-level dist folder. After adding the file, run a build and inspect the image or use docker build with a fresh cache to confirm the excluded paths are gone. Remember that a leading exclamation mark re-includes a path you previously ignored.