Difficulty: Beginner
What is a .dockerignore file, and why does it matter for build speed, cache hits and security?
A .dockerignore file lists files and directories that should be excluded from the build context, the set of files the Docker client sends to the builder when you run docker build .. Excluding things like node_modules, .git, .env and build output makes builds faster, keeps COPY . . from invalidating the cache on irrelevant changes, and stops secrets from ending up inside your image.
When you run docker build -t app ., the final . is the context directory. The builder (BuildKit, the default since Docker Engine 23.0) can only see files inside that context, and instructions like COPY . /app copy from it. Without a .dockerignore:
node_modules, .git or a local data/ directory get transferred, which is slow, especially to a remote builder or in CI.COPY . . copies your host's node_modules, which may contain native binaries compiled for macOS or Windows that crash on Linux.COPY . . and busts the cache for every later step..env files, private keys and .git history can be baked into a layer and shipped to a registry, where anyone with pull access can extract them.BuildKit is smarter than the legacy builder: it transfers the context incrementally and skips files no instruction uses. But COPY . . uses everything, so the ignore file still matters.
# starts a comment.filepath.Match rules plus for any number of directories, e.g. /*.log.! re-includes something, e.g. *.md then !README.md. Later lines win.api.Dockerfile uses api.Dockerfile.dockerignore if it exists, which helps monorepos.| Ignore | Why |
|---|---|
node_modules, .venv, target/ | Rebuilt inside the image for the right OS and CPU |
.git | Large, changes on every commit, exposes history |
.env, *.pem, secrets/ | Secrets should never be in a layer |
dist, build, coverage | Generated output, rebuilt in the image |
| Logs, editor folders | Noise that only busts cache |
Do not ignore files the build needs. A common bug is ignoring package-lock.json or tsconfig.json and then wondering why npm ci or the TypeScript build fails. Listing Dockerfile and .dockerignore is fine: the builder still reads them, they just are not copied into the image by COPY . ..
BuildKit prints a transferring context line with the size, so a jump from 2 MB to 400 MB is an immediate signal. To see the exact file list, build a throwaway stage that copies the context and lists it, as in the example below.
# .dockerignore
node_modules
npm-debug.log*
dist
coverage
.git
.github
.vscode
.env
.env.*
!.env.example
*.pem
**/*.log
Dockerfile*
docker-compose*.yml
Dependencies and build output are rebuilt inside the image, secrets are excluded with an exception for the harmless example file, and CI and editor folders stop busting the cache.
# Throwaway Dockerfile that prints the context
cat > /tmp/ctx.Dockerfile <<'DF'
FROM busybox
COPY . /ctx
RUN find /ctx -maxdepth 2 | sort
DF
docker build --no-cache --progress=plain -f /tmp/ctx.Dockerfile .
The RUN step prints every file that made it through .dockerignore, which is the quickest way to debug a missing or leaked file.
.dockerignore, Build context, BuildKit, Cache invalidation, Secret leakage