Docker & Containers (50 Qs)•Part 1: Basic & Foundational Concepts (Junior / Entry-Level)
What is the `.dockerignore` file and why is it critical for build speed and security?
junior LevelMost AskedCode ExampleSecurity & AuthDocker & Containers
In Simple Words:
When you run docker build ., the Docker CLI transfers everything in the current directory (the Build Context) across the Docker socket to the build daemon.
A .dockerignore file excludes unnecessary files from this transfer:
- Speeds up builds: Avoids sending hundreds of megabytes of
node_modules,.git, or build caches over the socket. - Prevents cache invalidation: Prevents changes in local files (like
.git/logsorREADME.md) from busting Docker layer cache. - Prevents credential leaks: Ensures
.env, private SSH keys, and local credentials are never accidentally copied into the final image.
text
# Typical .dockerignore
node_modules
npm-debug.log
.git
.gitignore
.env
.env.local
coverage
dist