.dockerignore

Difficulty: Beginner

Question

What is a .dockerignore file, and why does it matter for build speed, cache hits and security?

Answer

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.

What the build context is

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:

  • Large folders such as 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.
  • Every edit to any file, even a README, changes the checksum of 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.

Syntax rules

  • One pattern per line, matched relative to the context root. # starts a comment.
  • Patterns use Go's filepath.Match rules plus for any number of directories, e.g. /*.log.
  • ! re-includes something, e.g. *.md then !README.md. Later lines win.
  • With BuildKit you can keep a per-Dockerfile ignore file: api.Dockerfile uses api.Dockerfile.dockerignore if it exists, which helps monorepos.

What to ignore, and what not to

IgnoreWhy
node_modules, .venv, target/Rebuilt inside the image for the right OS and CPU
.gitLarge, changes on every commit, exposes history
.env, *.pem, secrets/Secrets should never be in a layer
dist, build, coverageGenerated output, rebuilt in the image
Logs, editor foldersNoise 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 . ..

Checking what gets sent

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.

Where .dockerignore acts in a build. Filtering happens before files leave your machine, so ignored files never reach any layer.
  1. Project directory: Source, node_modules, .git, .env and more.
  2. .dockerignore filter: Drops matching paths from the context.
  3. Build context sent to BuildKit: Smaller transfer and stable checksums.
  4. COPY instructions: Can only copy what survived the filter.
  5. Image layers: No secrets or host-built modules inside.

Code examples

A practical .dockerignore for a Node.js service

# .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.

Listing exactly what is in the build context

# 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.

Key points

  • .dockerignore filters the build context before it reaches the builder; ignored files cannot be copied into the image.
  • It speeds up builds, prevents cache busting from irrelevant files and keeps secrets out of layers.
  • Patterns follow Go filepath.Match with ** and ! exceptions; later rules override earlier ones.
  • Per-Dockerfile ignore files (name.Dockerfile.dockerignore) help monorepos.
  • Never ignore files the build needs, such as lockfiles.

Common follow-up questions

  • How would you pass a secret, like an npm token, into a build without it ending up in a layer?
  • Why should you copy package.json before the rest of the source?
  • What happens if you run docker build from your home directory?
  • How can you check what is being sent in the build context?

Interview tips

  • Cover all three benefits (speed, cache stability, security); most candidates only mention speed.
  • Mention the host node_modules native-binary problem; it is a real bug people hit.
  • Show you can verify it, for example by reading the transferring context size in the build output.

Concepts covered

.dockerignore, Build context, BuildKit, Cache invalidation, Secret leakage

More Docker & Containers questions