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.

🌐
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.
🌐
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
🌐
DEV Community
dev.to › nodepractices › docker-best-practices-with-node-js-4ln4
Docker best practices with Node.js - DEV Community
August 24, 2020 - TL;DR: Include a .dockerignore file that filters out common secret files and development artifacts. By doing so, you might prevent secrets from leaking into the image. As a bonus the build time will significantly decrease.
🌐
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
If you’re a Node.js developer using Docker, you’ve likely encountered a frustrating scenario: despite adding `node_modules` to your `.dockerignore` file, Docker still seems to include it in your image. This not only bloats your Docker image with unnecessary dependencies but also slows down ...
🌐
CloudBees
cloudbees.com › blog › dockerizing-node-js-applications
Dockerizing Node.js Applications
You just have to create a .dockerignore file and add the files you want to exclude, like:
🌐
GitHub
github.com › contentful › the-example-app.nodejs › blob › master › .dockerignore
the-example-app.nodejs/.dockerignore at master · contentful/the-example-app.nodejs
Example app for Contentful in node.js. Contribute to contentful/the-example-app.nodejs development by creating an account on GitHub.
Author   contentful
🌐
npm
npmjs.com › package › dockerignore
dockerignore - npm
September 7, 2015 - 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
Find elsewhere
🌐
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
During image build, docker takes all files in the context directory. To increase the docker build’s performance, exclude files and directories by adding a .dockerignore file to the context directory.
🌐
YouTube
youtube.com › watch
How to use Dockerignore file,: DevOps with Docker and Node.js + Mongodb/Redis Part #4 - YouTube
Sometimes we don't want to copy all of our files into our dockerfile. Things like .env file and node modules folder should never be copied over. We'll take a...
Published   May 18, 2021
🌐
npm
npmjs.com › package › @balena › dockerignore
@balena/dockerignore - npm
May 13, 2020 - 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 other projects in the npm registry using ...
      » npm install @balena/dockerignore
    
🌐
GitHub
github.com › chrischeks › docker-nodejs-sample › blob › main › .dockerignore
docker-nodejs-sample/.dockerignore at main · chrischeks/docker-nodejs-sample
This repository is a sample Node.js application for Docker's documentation. - docker-nodejs-sample/.dockerignore at main · chrischeks/docker-nodejs-sample
Author   chrischeks
🌐
Docker
docs.docker.com › guides › node.js › containerize
Containerize a Node.js application
$ git clone https://github.com/kristiyan-velkov/docker-nodejs-sample · Docker provides an interactive CLI tool called docker init that helps scaffold the necessary configuration files for containerizing your application. This includes generating a Dockerfile, .dockerignore, compose.yaml, and README.Docker.md.
🌐
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 - For example, let’s say you want to ignore all .env files except for .env.production. You can use negation for this: # Ignore all .env files *.env # But include .env.production !.env.production · This gives you the flexibility to fine-tune which files are excluded and which are included in your Docker build context. ... Using a .dockerignore file is one of the easiest ways to optimize your Docker builds.
🌐
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:
🌐
Medium
cleverzone.medium.com › dockerizing-first-next-js-application-9026894df384
Dockerizing first next js application | by Cleverzone | Medium
June 25, 2023 - node_modules .dockerignore .git .gitignore .next out public · In this example, we’re excluding the node_modules directory, the .dockerignore file itself, the .git directory, the .gitignore file, the .next directory (Next.js build output), the out directory (Next.js export output), and the public directory (Next.js public assets).
🌐
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
🌐
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 - 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.