use-dockerignore
Missing .dockerignore? Why node_modules, .git and .env end up in your Docker image, slow down builds, and how to write one.
On this page
Without a .dockerignore, docker build ships your entire project directory to the daemon as build context — node_modules, .git history, logs, local .env files — and COPY . . happily bakes all of it into the image. This rule fires when a Dockerfile is not accompanied by a .dockerignore file.
What the rule catches
$ docker build .
=> transferring context: 482.16MB # node_modules, .git, logs — all of it
=> [4/5] COPY . . # …and now it's in the imageScanning this file reports:
⚠ WARN [docker-doctor/use-dockerignore]
Using COPY/ADD with a wildcard or directory, but no .dockerignore file was found next to the Dockerfile or at the project root. This can copy local build folders and secrets.Why it matters
Three failures, one missing file. Speed: a multi-hundred-megabyte context is uploaded on every single build before the first instruction runs. Cache: COPY . . fingerprints everything it copies, so an updated log file or editor swap file invalidates the layer — and every layer after it — even though nothing meaningful changed. Security: .env files, credentials, and the full .git history quietly become part of an artifact that gets pushed to registries. A .dockerignore fixes all three and takes a minute to write.
How to fix it
Create a .dockerignore file in the same directory as the Dockerfile to prevent copying unnecessary files (like node_modules, logs, build artifacts).
node_modules
.git
dist
*.log
.env*
Dockerfile
compose.yamlStart from the entries above and add your ecosystem's equivalents (__pycache__, target/, vendor/, .venv). Ignoring node_modules is not optional polish — a host-platform node_modules copied into a Linux image is also a common source of "works locally, crashes in Docker" native-module bugs. The install should happen inside the build, ordered for cache as described in order-layers.
Rule details
- Rule key —
docker-doctor/use-dockerignore - Category — Performance
- Default severity —
warning - Applies to — Dockerfiles
Explain this rule from the CLI:
npx @docker-doctor/cli@latest rules explain docker-doctor/use-dockerignoreChange its severity — or turn it off — in your config file:
// docker-doctor.config.ts
export default {
rules: {
"docker-doctor/use-dockerignore": "off",
},
};Severity affects the health score: error findings cost more points than warning, and info costs the least.