QA
Beginner LevelDocker & Deployment

What is a .dockerignore file and why is it important?

❓ Interview Prompt: Explain how .dockerignore reduces build context size, speeds up image builds, and prevents sensitive file leaks.

When you run docker build, Docker packages the entire build context (the directory you specify) and sends it to the Docker daemon before building. Without .dockerignore, this includes node_modules (hundreds of MB!), .git history, local logs, and test artifacts.

Why .dockerignore Matters:

1
Speed: Excluding node_modules (can be 500MB+) from the build context drastically reduces upload time to the daemon.
2
Security: Prevents accidentally COPY-ing .env, AWS credentials, or private keys into image layers.
3
Cache correctness: Prevents irrelevant files (*.log, .DS_Store) from invalidating layer cache.
4
Image size: COPY . . won't accidentally bloat the image with test fixtures or coverage reports.

Syntax (same as .gitignore):

•
node_modules — exclude directory
•
**/*.log — exclude all log files recursively
•
!src/ — re-include a specific path (exception)

Code Implementation Example

text
# .dockerignore — place in same directory as Dockerfile

# Dependencies (will be reinstalled inside container)
node_modules
npm-debug.log
yarn-error.log

# Build outputs (built inside container)
.next
dist
build
out

# Development & test artifacts
coverage
.nyc_output
**/*.test.ts
**/*.spec.ts

# Secrets & sensitive files
.env
.env.local
.env.*.local
**/.aws
**/.ssh

# Git & editor files
.git
.gitignore
.github
.vscode
.idea
**/*.DS_Store

# Documentation (not needed in runtime image)
README.md
docs/
💡Comprehensive .dockerignore excluding node_modules, secrets, test artifacts, and editor files.
🎯 Interview Key Takeaway

.dockerignore is the Docker equivalent of .gitignore — exclude node_modules, .env files, and test artifacts to shrink build context and prevent secret leaks.

#Docker#.dockerignore#Build Context#Security#Beginner
Back to all Docker & Deployment Questions→

More Docker & Deployment Interview Questions

Beginner LevelDocker & Deployment

How do Docker Multi-Stage Builds minimize image size and improve security?

Beginner LevelDocker & Deployment

What is the difference between a Docker Container and a Virtual Machine (VM)?

Beginner LevelDocker & Deployment

What is the difference between a Docker Image and a Docker Container?