Unable to run a generic node.js container with a volume mount specified in the command line

MacOS Ventura 13.5.2
Docker Desktop for Mac 4.23.0 (120376)

Tried the following variations of the command line with no luck:

docker run -d --mount type=bind,source=$(pwd),target=/usr/src/app -p 8080:8080 --name nodejs dennismccray/nodejs

docker run -d -v $(pwd):/usr/src/app -p 8080:8080 --name nodejs nodejs

The log output from the container is as follows:

2023-09-18 18:42:27 node:internal/modules/cjs/loader:1051
2023-09-18 18:42:27 throw err;
2023-09-18 18:42:27 ^
2023-09-18 18:42:27
2023-09-18 18:42:27 Error: Cannot find module ‘express’
2023-09-18 18:42:27 Require stack:
2023-09-18 18:42:27 - /usr/src/app/server.js
2023-09-18 18:42:27 at Module._resolveFilename (node:internal/modules/cjs/loader:1048:15)
2023-09-18 18:42:27 at Module._load (node:internal/modules/cjs/loader:901:27)
2023-09-18 18:42:27 at Module.require (node:internal/modules/cjs/loader:1115:19)
2023-09-18 18:42:27 at require (node:internal/modules/helpers:130:18)
2023-09-18 18:42:27 at Object. (/usr/src/app/server.js:3:17)
2023-09-18 18:42:27 at Module._compile (node:internal/modules/cjs/loader:1241:14)
2023-09-18 18:42:27 at Module._extensions…js (node:internal/modules/cjs/loader:1295:10)
2023-09-18 18:42:27 at Module.load (node:internal/modules/cjs/loader:1091:32)
2023-09-18 18:42:27 at Module._load (node:internal/modules/cjs/loader:938:12)
2023-09-18 18:42:27 at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) {
2023-09-18 18:42:27 code: ‘MODULE_NOT_FOUND’,
2023-09-18 18:42:27 requireStack: [ ‘/usr/src/app/server.js’ ]
2023-09-18 18:42:27 }
2023-09-18 18:42:27
2023-09-18 18:42:27 Node.js v20.6.1

Yet this command works just fine every time:

docker run -d -p 8080:8080 --name nodejs nodejs

All files are located on my GitHub here: GitHub - dennismccray/nodejs: Generic Node Container

New to Docker and containers and software development for that matter. Ultimately would like to have a nice little development container which is why I want to be able to add a volume.

Any help would be appreciated.

Well, it seems npm modules are missing.

Did you install them on your host (npm install), before you mounted the directory into the container?

2 recommendations:

  • use alpine images, they tend to be a lot smaller, not just latest
  • maybe look into multi stage builds (doc)