Reputation: 144
I have an express app and I'm trying to setup hot reloading with nodemon using docker for windows 10 as a dev environment. However when I npm install on the volume it doesn't seem to work.
With powershell I use a volume like so:
docker build -t node-api .
docker run --rm -it -p 8080:8080 -v "${PWD}:/usr/src/app" node-api
Which outputs this error:
[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server/server.js`
internal/modules/cjs/loader.js:605
throw err;
^
Error: Cannot find module 'express'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
at Function.Module._load (internal/modules/cjs/loader.js:529:25)
at Module.require (internal/modules/cjs/loader.js:658:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (/usr/src/app/server/server.js:1:79)
at Module._compile (internal/modules/cjs/loader.js:722:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
[nodemon] app crashed - waiting for file changes before starting...
If I exclude the -v flag and docker run then it starts with no errors, but doesn't detect changes or restart on file saves.
Dockerfile
FROM node:alpine
#Create app directory
WORKDIR /usr/src/app
#Install nodemon for hot reloading
RUN npm install nodemon -g
#Install app dependencies
#A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
RUN npm install
#Bundle app source
COPY . .
EXPOSE 8080
CMD [ "nodemon", "-L", "server/server.js" ]
Folder Structure
server/
>server.js
Dockerfile
package.json
Github repo for code
Upvotes: 1
Views: 4740
Reputation: 2203
The problem is that by mounting the whole host app/
directory as volume, the app/node_modules/
inside the container would be overwritten by the host's app/
, thus all dependencies are missing.
The solution is to only mount the source code folder you need, i.e.
docker run --rm -it -p 8080:8080 -v "${PWD}/server:/usr/src/app/server" node-api
Upvotes: 3