【发布时间】:2023-07-06 12:07:01
【问题描述】:
我开始学习 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"]。
标签: docker docker-compose containers nodemon