re: Using Docker for Node.js in Development and Production VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Huh, that's an annoying bug. Why would you want to have node_modules as a volume though? 🤔

When you mount app to the container it overrides completely destination folder, so your installed during build modules will be vanished. I want to keep them so use that hack to exclude node modules folder. Did not find any better solution for the time being.

I get that (we usually even add node_modules to .dockerignore to evade cross-platform compat issues). I'm just not entirely sure why you'd want to have node_modules as a volume since you run npm install during image build anyway. Am I missing something? 🤔

.dockerignore only works on copy/add command during build time. But when you mount a folder it will override everything which were copied/installed to the container during the build.

it gives you 3 options:

  • install also modules at your local machine and they will present on container after mount. don't like it, you will also rely on your machine set up and cross platform problems will appear
  • use a hack described here and prevent overriding stackoverflow.com/questions/300438...
  • install node modules in a custom directory that also described by the link above

But you are using COPY in the example Dockerfiles in the article - that's what confuses me 😅
Or are you talking about using pre-built docker image for development using local code? Then it makes sense, but the whole approach is indeed quite cumbersome 🤔

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!

code of conduct - report abuse