文件更改时,两个带有 Nodemon 的后端服务器不会在 docker 容器中重新加载
Posted
技术标签:
【中文标题】文件更改时,两个带有 Nodemon 的后端服务器不会在 docker 容器中重新加载【英文标题】:Two backend servers with Nodemon doesn't reload in docker container when files change 【发布时间】:2021-03-04 09:12:11 【问题描述】:我开始学习 docker,但遇到了一个问题。我想运行 2 个容器,每个容器都将运行一个带有 nodemon 的快速服务器,但是当项目中发生更改时,nodemon 不会在容器中重新启动。当容器用完时,一切正常。
项目:
服务器/Dockerfile:
FROM node:alpine
WORKDIR /usr/server
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD ["npm", "start"];
任务/Dockerfile:
FROM node:alpine
WORKDIR /usr/tasks
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"];
服务器/package.json:
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts":
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
,
"keywords": [],
"author": "",
"license": "ISC",
"dependencies":
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"mongoose": "^5.10.15",
"nodemon": "^2.0.6"
tasks/package.json:
"name": "tasks",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts":
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
,
"keywords": [],
"author": "",
"license": "ISC",
"dependencies":
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"mongoose": "^5.10.15",
"nodemon": "^2.0.6"
docker-compose.yml:
version: "3"
services:
tasks:
container_name: tasks
build:
context: ./tasks
dockerfile: Dockerfile
command: npm start
ports:
- "3000:3000"
networks:
- app-network
server:
container_name: server
build:
context: ./server
dockerfile: Dockerfile
command: npm start
ports:
- "4000:4000"
networks:
- app-network
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_BASICAUTH_USERNAME: usubanipal
ME_CONFIG_BASICAUTH_PASSWORD: 3241324qwe!
ME_CONFIG_MONGODB_PORT: 27017
ME_CONFIG_MONGODB_ADMINUSERNAME: rootie
ME_CONFIG_MONGODB_ADMINPASSWORD: asdasd!
links:
- mongo
networks:
- app-network
mongo:
image: mongo
container_name: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: rootie
MONGO_INITDB_ROOT_PASSWORD: asdasd!
ports:
- "27017:27017"
volumes:
- /Users/TuTu/COMM/volumes/MongoDB:/data/db
networks:
- app-network
networks:
app-network:
driver: bridge
【问题讨论】:
Docker 镜像的内容一旦构建就固定了;在这里运行像nodemon
这样的工具是没有意义的。我会使用您的工作主机 Node 环境进行日常开发,并将 Docker 映像设置为更直接地运行 CMD ["node", "index.js"]
。
【参考方案1】:
解决了!只需使用正确的语法在 docker-compose.yml 中添加 volumes
version: "3"
services:
tasks:
container_name: tasks
build:
context: ./tasks
dockerfile: Dockerfile
command: npm start
volumes:
- ./tasks:/usr/tasks
ports:
- "3000:3000"
networks:
- app-network
server:
container_name: server
build:
context: ./server
dockerfile: Dockerfile
command: npm start
volumes:
- ./server:/usr/server
ports:
- "4000:4000"
networks:
- app-network
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_BASICAUTH_USERNAME: usubanipal
ME_CONFIG_BASICAUTH_PASSWORD: 3241324qwe!
ME_CONFIG_MONGODB_PORT: 27017
ME_CONFIG_MONGODB_ADMINUSERNAME: rootie
ME_CONFIG_MONGODB_ADMINPASSWORD: asdasd!
links:
- mongo
networks:
- app-network
mongo:
image: mongo
container_name: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: rootie
MONGO_INITDB_ROOT_PASSWORD: asdasd!
ports:
- "27017:27017"
volumes:
- /Users/TuTu/COMM/volumes/MongoDB:/data/db
networks:
- app-network
networks:
app-network:
【讨论】:
以上是关于文件更改时,两个带有 Nodemon 的后端服务器不会在 docker 容器中重新加载的主要内容,如果未能解决你的问题,请参考以下文章
更改文件时如何使用 nodemon 在 npm 脚本上重新启动多个服务器文件