So I got the answer based on tkausl and David Maze's comments (thanks guys!) .

The node_modules inside the image is generated from the Dockerfile 'npm install' step.

And as for the question:

if .dockerignore does not include node_modules, Dockerfile would have overwritten node_modules anyway? So why bother to put node_modules inside the .dockerignore file?

the comment I received was that if I left out the node_modules from the .dockerignore file, then the docker build process would have slowed down due to unnecessary copying of the node_modules, PLUS the image would have been larger.

Therefore, the prudent thing is to keep node_modules inside .dockerignore file to achieve more efficiency in the docker build process.

UPDATE: I did try to run the scenario with the node_modules removed from .dockerignore file, and the image is showing 940MB, whereas creating the image with node_modules inside the .dockerignore file - though the process may be more efficient - the output image is 942MB (2MB larger).

Answer from punsoca on Stack Overflow
🌐
GitHub
github.com › BretFisher › node-docker-good-defaults › blob › main › .dockerignore
node-docker-good-defaults/.dockerignore at main · BretFisher/node-docker-good-defaults
sample node app for Docker examples. Contribute to BretFisher/node-docker-good-defaults development by creating an account on GitHub.
Author   BretFisher
Top answer
1 of 4
12

So I got the answer based on tkausl and David Maze's comments (thanks guys!) .

The node_modules inside the image is generated from the Dockerfile 'npm install' step.

And as for the question:

if .dockerignore does not include node_modules, Dockerfile would have overwritten node_modules anyway? So why bother to put node_modules inside the .dockerignore file?

the comment I received was that if I left out the node_modules from the .dockerignore file, then the docker build process would have slowed down due to unnecessary copying of the node_modules, PLUS the image would have been larger.

Therefore, the prudent thing is to keep node_modules inside .dockerignore file to achieve more efficiency in the docker build process.

UPDATE: I did try to run the scenario with the node_modules removed from .dockerignore file, and the image is showing 940MB, whereas creating the image with node_modules inside the .dockerignore file - though the process may be more efficient - the output image is 942MB (2MB larger).

2 of 4
2

Consider docker as a entire new box, where everything will be installed and given commands to run specific tasks

In this case, try two things

  1. Create .dockerignore file and ignore all files which you think you don't want to include or let docker handles from itself.

    .git

    */node_modules

    ####### other files...

  2. And in Dockerfile just copy package.json as docker it use for deployment, so it makes sense that only package.json file will get change. So try adding COPY package*.json ./ in Dockerfile.

🌐
Medium
medium.com › @bounouh.fedi › mastering-the-dockerignore-file-boosting-docker-build-efficiency-398719f4a0e1
Mastering the .dockerignore File: Boosting Docker Build Efficiency | by Fedi Bounouh | Medium
November 9, 2024 - It works much like .gitignore: • Each line defines a file or directory to ignore. ... • You can use ! to negate a pattern, which means including certain files even if their parent directory is excluded.
🌐
GitHub
gist.github.com › allanchua101 › 0ff808ea0aa3d6db7fd5a6b6e004fec1
.dockerignore file for Node JS · GitHub
.dockerignore file for Node JS. GitHub Gist: instantly share code, notes, and snippets.
🌐
DEV Community
dev.to › nodepractices › docker-best-practices-with-node-js-4ln4
Docker best practices with Node.js - DEV Community
August 24, 2020 - Should Kubernetes be aware of that, ... ["node", "index.js"] TL;DR: Include a .dockerignore file that filters out common secret files and development artifacts....
🌐
npm
npmjs.com › package › node-dockerignore
node-dockerignore - npm
July 12, 2018 - node docker ignore. Latest version: 0.0.1, last published: 8 years ago. Start using node-dockerignore in your project by running `npm i node-dockerignore`. There are 2 other projects in the npm registry using node-dockerignore.
      » npm install node-dockerignore
    
Published   Jul 12, 2018
Version   0.0.1
🌐
GitHub
github.com › RisingStack › kubernetes-nodejs-example › blob › master › .dockerignore
kubernetes-nodejs-example/.dockerignore at master · RisingStack/kubernetes-nodejs-example
Node.js example application with Kubernetes and CircleCI config - kubernetes-nodejs-example/.dockerignore at master · RisingStack/kubernetes-nodejs-example
Author   RisingStack
🌐
npm
npmjs.com › package › @balena › dockerignore
@balena/dockerignore - npm
May 13, 2020 - dockerignore is a file filter library compatible with Docker and the node-ignore API. Latest version: 1.0.2, last published: 6 years ago. Start using @balena/dockerignore in your project by running `npm i @balena/dockerignore`. There are 25 ...
      » npm install @balena/dockerignore
    
Find elsewhere
🌐
Docker
docs.docker.com › guides › node.js › containerize
Containerize a Node.js application
# Optimized .dockerignore for Node.js + React Todo App # Based on actual project structure # Version control .git/ .github/ .gitignore # Dependencies (installed in container) node_modules/ # Build outputs (built in container) dist/ # Environment files .env* # Development files .vscode/ *.log coverage/ .eslintcache # OS files .DS_Store Thumbs.db # Documentation *.md docs/ # Deployment configs compose.yml Taskfile.yml nodejs-sample-kubernetes.yaml # Non-essential configs (keep build configs) *.config.js !vite.config.ts !esbuild.config.js !tailwind.config.js !postcss.config.js !tsconfig.json
🌐
CloudBees
cloudbees.com › blog › dockerizing-node-js-applications
Dockerizing Node.js Applications
. EXPOSE 3000 CMD node index.js ... You can and should do the same with Docker as well, skipping node_modules and log files. You just have to create a .dockerignore file and add the files you want to exclude, like:...
🌐
Atmosera
atmosera.com › home › containerize a node app in 5 minutes
Containerize a Node App in 5 Minutes - Atmosera
December 16, 2023 - Simply create a text file called .dockerignore that has 1 line it it: ... Once the Dockerfile and the .dockerignore files are in place, youre ready to build the image. With a shell (CMD, Powershell, Bash etc.) in the app directory, you can run the following command.
🌐
Hasura
hasura.io › blog › an-exhaustive-guide-to-writing-dockerfiles-for-node-js-web-apps-bbee6bd2f3c4
An Exhaustive Guide to Writing Dockerfiles for Node.js Web Apps
node-app ├── Dockerfile ├── ... docker build’s performance, exclude files and directories by adding a .dockerignore file to the context directory....
🌐
npm
npmjs.com › package › dockerignore
dockerignore - npm
September 7, 2015 - Command line tool for creating or updating a .dockerignore file based on .gitignore.. Latest version: 0.2.0, last published: 10 years ago. Start using dockerignore in your project by running `npm i dockerignore`. There are no other projects in the npm registry using dockerignore.
      » npm install dockerignore
    
Published   Jul 09, 2015
Version   0.2.0
Author   James Talmage
🌐
GitHub
github.com › isa-group › project-template-nodejs › blob › master › .dockerignore
project-template-nodejs/.dockerignore at master · isa-group/project-template-nodejs
A initial template for developing Node JS project with development policy and Grunt JS for automation - project-template-nodejs/.dockerignore at master · isa-group/project-template-nodejs
Author   isa-group
🌐
DEV Community
dev.to › minima_desk_cd9b151c4e2fb › dockerize-your-nodejs-application-a-step-by-step-guide-iel
Dockerize Your Node.js Application: A Step-by-Step Guide - DEV Community
April 12, 2025 - This creates or updates the package-lock.json file, which ensures consistent dependency versions across environments. ... Verify all routes, middleware, and features are working correctly. Fix any issues before proceeding with containerization. ... Remove any files or directories that are not needed in the container, such as logs, temporary files, or development-specific resources. You can use a .dockerignore file to exclude these during the build process, which we will explore in the next section.
🌐
Docker
docker.com › blog › 9-tips-for-containerizing-your-node-js-application
9 Tips for Containerizing Your Node.js Application | Docker
April 19, 2023 - For this tutorial, your .dockerignore file should contain just one line: ... This line excludes the node_modules directory — which contains output from Maven — from the Docker build context.
🌐
.dockerignore
dockerignore.com › dockerignores › languages-node
Node.js .dockerignore
Ready-to-use .dockerignore template for Node.js languages projects. Copy and paste directly into your project.
🌐
Mrugesh Mohapatra
hn.mrugesh.dev › how-to-use-a-dockerignore-file-a-comprehensive-guide-with-examples
How to Use a .dockerignore File: A Comprehensive Guide with Examples • Mrugesh Mohapatra
May 25, 2023 - The .dockerignore file allows you to specify which files and directories Docker should ignore when building an image. This speeds up the build process and results in smaller Docker images because the unnecessary files are not included.
🌐
codestudy
codestudy.net › blog › confusion-with-node-modules-being-ignored-by-dockerignore
Why Is node_modules Not Being Ignored by .dockerignore? Fixing Common Docker + Node.js Confusion — codestudy.net
To ignore all of them, use a glob pattern in .dockerignore: # Ignore all node_modules directories, including nested ones **/node_modules/ In multi-stage builds, node_modules is often needed in the "builder" stage but not the final image. Ensure it’s excluded from the final stage: # Builder stage: Install dependencies and build FROM node:18-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm install COPY .
🌐
Medium
cleverzone.medium.com › dockerizing-first-next-js-application-9026894df384
Dockerizing first next js application | by Cleverzone | Medium
June 25, 2023 - Open the .dockerignore file and add the files and directories that you want Docker to ignore during the build process. These can include development-specific files and directories that are not needed in the production image. Here's an example of a .dockerignore file for a Next.js project: node_modules .dockerignore .git .gitignore .next out public