gpt4 book ai didi

node.js - 在docker中运行nodemon(MERN-docker-compose)

转载 作者:行者123 更新时间:2023-12-02 21:03:02 25 4
gpt4 key购买 nike

我有一个运行MERN应用程序的容器。在我的前端容器中,我正在运行react,并且热重载按预期进行,但是在我的后端容器中,我似乎找不到代码中动态更新的解决方案。我正在尝试使用nodemon程序包,但是我不知道我是否做对了。如果我尝试在nodemon处于 Activity 状态的情况下运行docker,则会收到错误消息。

package.json

{
"name": "api",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node bin/www",
"dev": "nodemon bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.3",
"cors": "^2.8.4",
"debug": "~2.6.9",
"express": "~4.16.0",
"http-errors": "~1.6.2",
"jade": "~1.11.0",
"mongoose": "^5.8.4",
"morgan": "~1.9.0"
},
"devDependencies": {
"nodemon": "^1.19.0"
}
}


docker-compose.yml
version: "3"
services:
client:
image: mhart/alpine-node:12
build: ./client
restart: always
ports:
- "3000:3000"
working_dir: /client
volumes:
- ./client:/client
entrypoint: ["npm", "start"]
links:
- api
networks:
- mynetwork
api:
build: ./api
restart: always
ports:
- "9000:9000"
working_dir: /api
volumes:
- ./api:/api
- /api/node_modules
depends_on:
- mongodb
networks:
- mynetwork
mongodb:
image: mongo
restart: always
container_name: mongodb
volumes:
- ./data-node:/data/db
ports:
- 27017:27017
command: mongod --noauth --smallfiles
networks:
- mynetwork
networks:
mynetwork:
driver: bridge


Dockefile (后端)
FROM mhart/alpine-node:12

WORKDIR /api

COPY package*.json /api/

RUN npm install

COPY . /api/

EXPOSE 80

# CMD ["npm", "start"]

CMD ["npm", "run", "dev"]

错误输出(我已在全局安装nodemon)
sh: nodemon: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! api@0.0.0 dev: `nodemon ./bin/www`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the api@0.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

最佳答案

您的容器中没有nodemon。您需要先安装它。在npm i -G nodemon中的COPY package*.json /api/之后插入Dockerfile

关于node.js - 在docker中运行nodemon(MERN-docker-compose),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59585235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com