🐳 Docker Beginner

What is the purpose of .dockerignore?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Docker basics — a prerequisite for any developer role.

Answer

The .dockerignore file specifies files and directories that should be excluded from the build context — the set of files sent to the Docker daemon when building an image. Similar to .gitignore syntax: list patterns of files/directories to exclude. Why it matters: when you run docker build ., Docker sends ALL files in the current directory (and subdirectories) to the Docker daemon as the build context. If your project has large directories (node_modules, .git, test data, logs, build artifacts), they all get sent over even if they're not needed in the image — this makes builds slow and the image larger. Typical .dockerignore contents: node_modules/ (installed from scratch in the image via npm install), .git/, *.log, *.md (if not needed at runtime), .env (sensitive — never copy into image), coverage/, dist/ (if built fresh), docker-compose*.yml, Dockerfile*, .dockerignore. Benefits: faster builds (smaller context to transfer), smaller images (only copy what's needed), security (secrets in .env don't accidentally end up in the image), better cache utilization.

Pro Tip

Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Docker answers easy to follow.