.dockerignore lists files and folders to leave out of the build context. The build context is the set of files the Docker client sends to the builder. COPY . . cannot copy anything that is excluded.
It matters for three reasons:
- It keeps the context small, so builds start faster.
- It stops unrelated changes, like
.gitactivity, from invalidating theCOPYcache. - It keeps secrets and junk files out of the image.
For a Node project, typical entries are:
node_modules. Modules built on the host, maybe for a different OS, must not overwrite the ones installed in the image.dist.git.envand other env files- logs
- coverage output
- local Docker or editor files
- It excludes files from the build context.
- A smaller context makes builds faster.
- It avoids needless cache invalidation.
- It keeps secrets like .env out of the image.
- Exclude node_modules, .git, dist and .env.
Read more