What is the purpose of a .dockerignore file when preparing a Node.js project for cloud deployment?
Short answer
A .dockerignore file excludes files -- most importantly node_modules, .env files, and local build artifacts -- from being copied into a Docker build context, keeping images smaller and preventing accidentally baking local secrets or a platform-specific node_modules into a…
Advertisement
AModel answer
A .dockerignore file excludes files -- most importantly node_modules, .env files, and local build artifacts -- from being copied into a Docker build context, keeping images smaller and preventing accidentally baking local secrets or a platform-specific node_modules into a production image.
Example
node_modules npm-debug.log .env .git coverage
Real-world example
A team's Docker images failed on a different CPU architecture until adding .dockerignore excluding node_modules, forcing 'npm ci' inside the build to install correctly-architected dependencies.
Likely follow-ups
- Why can copying a local node_modules folder into a Docker image cause native-module compatibility issues?
- What other sensitive files should always be excluded from a build context?
Related topics