Generate optimized .dockerignore files to reduce Docker build context size and improve build performance.
Last updated: March 2026
A .dockerignore file tells Docker which files and directories to exclude from the build context. Similar to .gitignore for Git, it reduces the amount of files sent to the Docker daemon, making builds faster and more efficient.
When you run docker build, Docker creates a build context by packaging all files in the build directory. By excluding unnecessary files like node_modules, .git, logs, and IDE settings, you significantly reduce build times and keep your Docker images lean.
This is especially important in CI/CD pipelines where every second counts. A well-configured .dockerignore can reduce build context size by 50-90%, depending on your project structure.
Follow these steps to use .dockerignore:
Node.js project .dockerignore:
# Dependencies node_modules/ npm-debug.log* # Build outputs dist/ build/ # Environment files .env .env.local # Version control .git .gitignore # IDE settings .vscode/ .idea/ # OS files .DS_Store Thumbs.db
No. Files in .dockerignore are excluded from the build context but not from the final image if they already exist. It only prevents copying unnecessary files during the build process.
.gitignore controls which files Git tracks; .dockerignore controls which files are sent to Docker during build. You can have different ignore patterns for each.
Yes! Your Dockerfile should run 'npm install' during build. Excluding node_modules keeps your build context small and ensures dependencies are fresh.
The test matcher is approximate and simplified. It matches basic glob patterns (* and ?) but does not handle all Docker's .dockerignore semantics (directory rules, anchored paths, negations with !). Always test your generated file with 'docker build' to verify behavior.
Yes. Use ! prefix to negate a pattern. Example: exclude * then include with !important-file.txt (order matters).
Typically 20-80% reduction, depending on your project. Projects with large node_modules or .git directories see the most improvement.
Yes. Docker Compose uses the same .dockerignore file when building images specified in the compose.yml file.
Related Tools
Convert between number systems.
Convert binary to hexadecimal.
Convert binary to octal.
Convert decimal to hexadecimal.
Convert decimal to octal.
Convert HEX to RGB.