Goal: get a Dockerfile which fit for development on local machine.
Requirements: App should not rely on anything at your local machine despite of Docker installation and the app code.
For node.js app you need to have installed node_modules. So we need install it somewhere and it comes to the 3 points in the previous comment.
So, we happy to do npm install in Dockerfile because that good for both development and production environments. By default node_modules installs at the same as your app directory folder in our case /usr/src/app/node_modules. Modules installed during the build. Then because development on local machine requires that your changes in the code reflect on the app inside docker we mount our local folder with the app(where we don't have node_modules) to the container. It overrides the /usr/src/app in the container and app will not start without node_modules. To use node_modules which were installed during the build-time, there is a hack of using volume as described in stack overflow.
Ah, I finally get it! 😅
Thanks for the detailed explanation!
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.