DEV Community

Discussion on: Using Docker for Node.js in Development and Production

Collapse
 
alex_barashkov profile image
Alex Barashkov

Tried npm ci and got the bug which has not fixed yet github.com/npm/npm/issues/21007. So can't use it. Tested it on simple configuration - works well, but because of the bug can't use it with unified dev/prod configs. Will wait once they fix it and then test it properly. Especially weird that PR is already submitted with the fix, but nobody is even replied about the plans of merging it.

Collapse
 
yamalight profile image
Tim Ermilov

Huh, that's an annoying bug.

Why would you want to have node_modules as a volume though? 🤔

Thread Thread
 
alex_barashkov profile image
Alex Barashkov

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.

Thread Thread
 
yamalight profile image
Tim Ermilov

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? 🤔

Thread Thread
 
alex_barashkov profile image
Alex Barashkov • Edited

.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
Thread Thread
 
yamalight profile image
Tim Ermilov

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 🤔

Thread Thread
 
alex_barashkov profile image
Alex Barashkov

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.

Thread Thread
 
yamalight profile image
Tim Ermilov

Ah, I finally get it! 😅
Thanks for the detailed explanation!