Interview Q&A / Docker & Kubernetes

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What is a .dockerignore file and why do you need it?

SHORT ANSWER

A .dockerignore lists files and folders excluded from the build context sent to the Docker daemon. It speeds up builds, keeps secrets and node_modules out of the image, and avoids needless cache invalidation.

Explain it simply

When you run docker build ., the whole folder is packed up and sent to Docker as the build context. Without filtering, that includes .git, node_modules, logs and maybe a .env file with passwords. It is like sending a courier a whole almirah when he only needs one shirt.

.dockerignore works like .gitignore: patterns for paths to leave out. Benefits: a smaller context uploads faster; COPY . . cannot accidentally copy secrets; and changes to ignored files do not invalidate the cache of a COPY . . layer. A lean context is one of the cheapest build speed-ups.

The file must sit at the root of the build context, and patterns are relative to that root.

Example

text
# .dockerignore
.git
node_modules
*.log
.env
Dockerfile
docker-compose.yml

What to say in the interview

"A .dockerignore excludes files from the build context. It makes builds faster, stops secrets like .env or heavy folders like node_modules and .git from being copied in, and prevents unnecessary cache busts on COPY . ."

Common mistakes

  • Leaving .env out of .dockerignore, so credentials end up in a layer.
  • Placing the file in a subfolder instead of the context root.

#dockerfile#dockerignore#build-context

Related questions

Practise more Docker & Kubernetes questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Docker & Kubernetes quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.