Reputation: 1612
I'm working with NodeJS and Nodemon on Docker. When I try to run my NodeJS app using nodemon command directly in docker compose file, it runs.
Like this (working): [docker-compose]
command: nodemon source/index.js
But when I use a script from package.json, it doesn't work
Like this (not-working): [docker-compose]
command: npm run dev
Where my package.json file is
"scripts": {
"start": "node source/index.js",
"dev": "nodemon source/index.js"
}
I tried different things, when I simply run start script without nodemon, it works
Like this (working): [docker-compose]
command: npm run start
But when I try to use dev again with nodemon command inside it, it doesn't work. Container won't start. I have also tried the following and it also works
Like this (working): [docker-compose]
command: nodemon --exec npm start
I still don't understand, why nodemon command is not working inside script dev
I'm using Docker in Swarm Mode
Here are my both files
docker-compose
version: '3.7'
services:
node-service:
image: node-img:1.0
ports:
- 4000:4000
working_dir: "/node-dir"
volumes:
- ./node-dir/source:/node-dir/source
networks:
- ness-net
command: npm run dev
networks:
ness-net:
package.json
{
"name": "node-pkg",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node source/index.js",
"dev": "nodemon source/index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"nodemon": "^1.19.4"
}
}
Upvotes: 3
Views: 3281
Reputation: 11
just add in package.json--->>
"scripts": {
"dev":"nodemon -L app.js"
}
in docker file--->>>
FROM node:latest
WORKDIR /app
COPY ./package*.json ./
RUN npm install
RUN npm install -g nodemon
COPY . .
EXPOSE 5500
CMD ["npm", "run","dev"]
run cmd-->>
docker build -t your_imge_name .
docker run --rm -d -p 5500:5500 -v %cd% --name your_container_name your_imge_name
Upvotes: 0
Reputation: 656
Just add the "." to define the path in your package.json like this
"scripts": {
"start": "node ./source/index.js",
"dev": "nodemon ./source/index.js"
}
Upvotes: 2
Reputation: 430
Try this solution:
services:
node-app:
container_name: node-app
image: node:latest
restart: always
volumes:
- ./node/source:home/node/source
working_dir: /home/node/source
ports:
- 4000:4000
networks:
- main-network
command: "tail -f /dev/null && npm start"
depends_on:
- db
logging:
driver: "json-file"
options:
max-file: "4"
max-size: "100m
Here is package.json
"main": "index.js",
"scripts": {
"preinstall": "npm i nodemon -g",
"start": "nodemon index.js",
}
Please make sure there should be index.js and package.json in working directory.
Upvotes: 0
Reputation: 11
You need to add an environment variable to point on npm when running nodemon
C:\........\npm
the path should be like this , and choose a name
Upvotes: 0