两台带有Nodemon的后端服务器';当文件更改时,不要在docker容器中重新加载
我是从docker开始学习的,但我遇到了一个问题。我想运行2个containeeirs,其中每个containeeirs将运行一个带有nodemon的express服务器,但是当项目发生更改时,nodemon不会在容器中重新启动。当用完容器时,一切都能正常工作 项目: 服务器/Dockerfile:两台带有Nodemon的后端服务器';当文件更改时,不要在docker容器中重新加载,docker,docker-compose,containers,nodemon,Docker,Docker Compose,Containers,Nodemon,我是从docker开始学习的,但我遇到了一个问题。我想运行2个containeeirs,其中每个containeeirs将运行一个带有nodemon的express服务器,但是当项目发生更改时,nodemon不会在容器中重新启动。当用完容器时,一切都能正常工作 项目: 服务器/Dockerfile: FROM node:alpine WORKDIR /usr/server COPY package*.json ./ RUN npm install COPY . . EXPOSE 400
FROM node:alpine
WORKDIR /usr/server
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD ["npm", "start"];
FROM node:alpine
WORKDIR /usr/tasks
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"];
任务/Dockerfile:
FROM node:alpine
WORKDIR /usr/server
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD ["npm", "start"];
FROM node:alpine
WORKDIR /usr/tasks
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"];
server/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"
}
}
{
"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"
}
}
tasks/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"
}
}
{
"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-compose.yml中添加卷
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:
Docker映像的内容在您构建它之后是固定的;在这里运行像
nodemon
这样的工具是没有意义的。我将使用您的工作主机节点环境进行日常开发,并将Docker映像设置为更直接地运行CMD[“Node”,“index.js”]
。