⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Docker & Containers Interview Questions Scenario 29 of 159 in Docker & Containers
Senior DevOps / SRE [L2] Docker Production Scenario [L2]
🎯 Target Role / Context: Docker Scenario #29 (L2) · Docker

Q: What is the purpose of `.dockerignore`?

A .dockerignore file lists paths that Docker leaves out of the build context, so they are never sent to the builder and cannot be copied into the image by COPY or ADD. It works much like .gitignore but applies to docker build.

#Docker #L2 #Containers #Linux #Git
🎙️ Candidate Opening & Architectural Context
"A <strong><code>.dockerignore</code></strong> file lists paths that Docker leaves out of the build context, so they are never sent to the builder and cannot be copied into the image by <code>COPY</code> or <code>ADD</code>. It works much like <code>.gitignore</code> but applies to <code>docker build</code>. Interviewers ask about it because it affects three things at once: build speed, layer cache hits and accidental secret leakage. A good answer shows you know the context is uploaded <em>before</em> the first instruction runs, and that patterns have rules that differ from <code>.gitignore</code>."
Advertisement
⚡ Recommended Practice Lab

Want to master this scenario in a live sandbox? KodeKloud's Docker Certified Associate (DCA) Hands-On Lab Course covers this exact problem with hands-on terminal drills.

🛠️ Production Runbook & Step-by-Step Resolution

Direct Answer: A <strong><code>.dockerignore</code></strong> file lists paths that Docker leaves out of the build context, so they are never sent to the builder and cannot be copied into the image by <code>COPY</code> or <code>ADD</code>. It works much like <code>.gitignore</code> but applies to <code>docker build</code>. Interviewers ask about it because it affects three things at once: build speed, layer cache hits and accidental secret leakage. A good answer shows you know the context is uploaded <em>before</em> the first instruction runs, and that patterns have rules that differ from <code>.gitignore</code>.

1️⃣

How .dockerignore Fits Into a Build

The build context is the directory you pass to docker build, and .dockerignore filters it before anything else happens.

  • Context transfer: the client packages the context and sends it to the builder; without a filter, .git, node_modules and build output all go too.
  • Location: the file sits at the root of the context. With BuildKit, a file named <Dockerfile-name>.dockerignore next to the Dockerfile takes precedence for that Dockerfile.
  • Scope: it affects COPY, ADD and the context upload only. It does not change bind mounts at docker run time.
  • Matching: patterns are relative to the context root, a later rule overrides an earlier one, and ! re-includes a path.
2️⃣

Write a Practical .dockerignore

Start from what the image does not need at runtime, then verify nothing required is excluded.

# .dockerignore
.git
.gitignore
.env
.env.*
*.pem
node_modules
**/__pycache__
**/*.log
coverage/
dist/
Dockerfile
.dockerignore
docker-compose*.yml

# Re-include a file that a broader rule excluded
!README.md
  • VCS and editor files: .git, .idea, *.swp add size and change often.
  • Dependencies and artifacts: node_modules, target/, __pycache__ should be rebuilt inside the image for the right OS and architecture.
  • Secrets: .env, *.pem, .aws and *.tfstate must never enter a layer, because layers are permanent and extractable.
  • Recursive match: *.md matches only at the root; use **/*.md to match at any depth.
Advertisement
3️⃣

Use an Allowlist and Verify the Result

The safest pattern for large repos is to exclude everything and re-include only what the build needs.

# .dockerignore (allowlist)
*
!package.json
!package-lock.json
!src/

# Debug stage: see what the builder actually received
# FROM busybox AS ctx
# COPY . /ctx
# RUN find /ctx -maxdepth 2

docker build --progress=plain --target ctx .
docker build --progress=plain -t my-app:dev .
  • Allowlist style: start with *, then add !package.json, !src. New files stay out by default.
  • Check context size: run docker build --progress=plain . and read the "transferring context" line; a drop from hundreds of MB to a few MB confirms the filter works.
  • Inspect what landed: a throwaway stage with COPY . /ctx and RUN find /ctx lists exactly what the builder received.
  • Cache effect: with COPY . . any changed file in the context invalidates that layer, so ignoring noisy files keeps later layers cached.
4️⃣

Failure Modes and Edge Cases

Most .dockerignore bugs are over-exclusion or wrong pattern scope.

  • Build fails with "not found": a COPY source is excluded. Re-include it with !path after the broad rule.
  • Rule order: the last matching pattern wins, so a ! line placed before the exclusion has no effect.
  • Ignoring the Dockerfile: allowed; the builder still reads it, it is just not available to COPY.
  • Different from .gitignore: there is no per-directory file and no automatic recursion for patterns like *.log.
Pro Tip: Excluding <code>.git</code> is usually right, but tooling that stamps a version from <code>git describe</code> inside the build will break; pass the version as a <code>--build-arg</code> instead.
5️⃣

Keep It Maintained in Production

Treat the file as part of your supply-chain hygiene.

  • Review with the Dockerfile: change both in the same pull request when directory layout changes.
  • Secrets belong in build secrets: use RUN --mount=type=secret with BuildKit, not files copied into the context.
  • Monorepos: use a per-Dockerfile ignore file so each service sends only its own sources.
  • Defence in depth: combine it with multi-stage builds so build tooling and credentials stay out of the final image.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Prefer an allowlist (<code>*</code> then <code>!src</code>) in monorepos, and confirm it by checking the &quot;transferring context&quot; size in <code>--progress=plain</code> output."
⚡ 60-Second Elevator Pitch Talking Points
  • .dockerignore excludes files from the build context so they are not uploaded to the builder or copied into the image.
  • It speeds builds, keeps COPY layers cached and stops secrets such as .env and keys leaking into permanent layers.
  • I verify it by checking the transferred context size, and in monorepos I use an allowlist plus per-Dockerfile ignore files.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Using *.md and expecting it to match nested files Patterns are anchored at the context root; nested matches need **/*.md.
  2. Putting a ! re-include before the broad exclusion The last matching rule wins, so the exclusion overrides the re-include.
  3. Relying on .dockerignore to hide secrets that are used at build time If the file is excluded the build cannot use it, and if it is copied it stays in a layer; use BuildKit secret mounts.
  4. Assuming it affects docker run -v bind mounts It only filters the build context; mounted host paths are fully visible to the container.
Advertisement
Want more Docker & Containers scenarios?
Explore our complete collection of scenario-based Docker & Containers interview runbooks.
Browse All Docker & Containers Questions →