DEV Community

Discussion on: Introduction to Docker for Javascript Developers (feat Node.js and PostgreSQL)

Collapse
 
daryushn profile image
Daryush Nadim • Edited

Excellent tutorial, Alex! After a few years of having no interest in understanding containers, this tutorial finally explained their practical use coupled with a good example and now I want to learn more!

I have been trying to follow the example in the tutorial, but I'm having an issue. Basically, I opted not to install NPM on the host machine and want to have NPM only installed in the container. When I run the sample without the volume, everything works fine and I can access the Hello World web page, i.e. this works:

docker run -p 3000:8080 --name my-node-app-container my-node-app

However, when I try to run the sample with the volume "mapping", then the command fails, i.e. this does NOT work:

docker run -p 3000:8080 --name my-node-app-container --volume ${PWD}:/usr/src/app my-node-app

This is the output:

PS C:\docker-template> docker run -p 3000:8080 --name my-node-app-container --volume ${PWD}:/usr/src/app my-node-app

> server@1.0.0 start /usr/src/app
> nodemon server.js

sh: nodemon: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! server@1.0.0 start: nodemon server.js
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the server@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2022-01-03T09_07_28_520Z-debug.log

What puzzles me is the "but node_modules missing, did you mean to install" message? Does this mean that since I am syncing my local host folder with the working folder in the container that the syncing ends up overwriting the potentially already built "node_modules" folder and hence it's missing? As mentioned, my local host folder (C:\docker-template) does not have node_modules because I opted not to install NPM on the host and was hoping to get away with NPM only in the container.

I am running Docker on Windows via Docker for Desktop and on WSL2. My C:\docker-template folder contains only 3 files: Dockerfile, package.json and server.js.

Collapse
 
alexeagleson profile image
Alex Eagleson

You bring up a good point there is an issue here when you don't have a node_modules directory in the source. Basically the "lack of the directory" gets overwritten on the volume mount.

Here is a stackoverflow discussion about the issue:

stackoverflow.com/a/32785014

It talks about it in the context of docker-compose but the same principle should apply without compose. The syntax for adding the additional volume would be:

docker run -p 3000:8080 --name my-node-app-container --volume  ${PWD}:/usr/src/app --volume /usr/src/app/node_modules my-node-app
Enter fullscreen mode Exit fullscreen mode