.dockerignore

Difficulty: Beginner

Question

What is a .dockerignore file and why is it important for build performance?

Answer

It works exactly like `.gitignore`. It prevents specific files/directories from being sent to the Docker daemon as part of the 'build context'.

**Commonly ignored**: `node_modules`, `.git`, `.env`, and documentation.

Code examples

Typical .dockerignore

# .dockerignore
node_modules
.git
.env
dist
npm-debug.log
Dockerfile
.dockerignore

By ignoring 'node_modules', the 'COPY . .' command won't copy your local, probably OS-specific modules into the Linux-based container.

Key points

Concepts covered

Dockerfile, Build Context, Optimization