Videos
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
.dockerignoredoes not includenode_modules, Dockerfile would have overwrittennode_modulesanyway? So why bother to putnode_modulesinside 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).
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
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...
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.
» npm install node-dockerignore
» npm install dockerignore
» npm install @balena/dockerignore
Problem Restated:
It appears that you're getting .gitignore confused with .dockerignore:
The later file tells Docker what files to ignore during the build of a Docker image. .dockerignore has nothing to do with Git pushes to upstream branches.
Solution:
Add the docker-compose.yml file(s) to your .gitignore file if you don't want Git tracking the file(s).
Don't delete .dockerignore if you want it read during builds.
If by "Still I get this file pushed over to the remote branch", you mean you are trying to gitignore that file then you can remove it using
git rm --cached docker-composer.yml