文件更改时,Nodemon不会重新加载到Docker容器中 [英] Nodemon doesn't reload in docker container when files change
问题描述
我读过许多关于此的主题,但是没人能解决任何问题。
有人说您必须添加-旧版手表
(或 -L
)到 nodemon
命令。
Others显示了几种不同的配置,显然,当docker容器中的卷上的文件发生更改时,nodody确实知道要怎么做才能重新启动服务器。
到目前为止,这里是我的配置:
Dockerfile:
FROM节点:最新
#创建应用程序目录
RUN mkdir -p / usr / src / app
WORKDIR / usr / src / app
#全局安装nodemon
RUN npm install nodemon -g
#安装依赖项
COPY package * .json ./
运行npm install
#捆绑应用程序源
COPY。 / usr / src / app
#导出
EXPOSE 3000
CMD [ npm, start]
docker-compose.yml
版本: '3.1'
服务:
节点:$ b $ b构建:。
用户:节点
卷:
-./:/usr/src/app
端口:
-3000:3000
取决于:
-mongo
working_dir:/ usr / src / app
环境:
-NODE_ENV =生产
公开:
- 3000
mongo :
图像:mongo
公开:
-27017
卷:
-./data/db:/data/db
环境:
MONGO_INITDB_ROOT_USERNAME:根
MONGO_INITDB_ROOT_PASSWORD:示例
package.json
{
name: node-playground,
version: 1.0.0,
description:,
main: index.js,
scripts:{
test: echo \错误:未指定测试\ && exit 1,
开始: nodemon -L
},
关键字:[],
作者:,
许可证: ISC,
取决于encys:{
ejs: ^ 2.7.1,
express: ^ 4.17.1,
猫鼬: ^ 5.7.1
},
devDependencies:{
nodemon: ^ 1.19.2
}
}
我也尝试了许多不同的设置。就像不全局安装 nodemon
一样,而是仅作为项目依赖项。并在 docker-compse.yml
上运行命令,我相信我现在不记得的很多其他命令。没有。
如果有人对此有确定性,请提供帮助。谢谢!!!!!!
我继续创建了一个示例容器和存储库,以说明如何实现此目标。 / p>
只需遵循以下步骤,概述了如何在 Docker <中使用
nodemon
/ code>容器。
Docker容器:
步骤1 使用文档合成:
请参见 docker-comp ose.yml
文件用于配置
-
cd / path / to / dir /那/有/你的/组成/文件
-
docker-compose up -d
步骤2:验证应用是否正常
步骤3:检查容器日志,以获取基线
docker日志nodemon-test
步骤4:我包含了一个 bash
脚本,以使编辑文件尽可能简单。我们需要在容器上弹出一个外壳,然后运行 bash
脚本( change.sh
)
-
docker exec -it nodemon-test / bin / bash
-
bash change.sh
-
退出
如上一个屏幕截图所示, nodemon
进行更改后已成功重启!
I read many threads sabout this but no one solves anything.
Some say you have to add --legacy-watch
(or -L
) to the nodemon
command.
Others shows several different configurations and apparently nodody really knows what you gotta do to achieve server restarting when a file change at the volume inside a docker container.
Here my configuration so far:
Dockerfile:
FROM node:latest
# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# install nodemon globally
RUN npm install nodemon -g
# Install dependencies
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . /usr/src/app
# Exports
EXPOSE 3000
CMD ["npm", "start"]
docker-compose.yml
version: '3.1'
services:
node:
build: .
user: "node"
volumes:
- ./:/usr/src/app
ports:
- 3000:3000
depends_on:
- mongo
working_dir: /usr/src/app
environment:
- NODE_ENV=production
expose:
- "3000"
mongo:
image: mongo
expose:
- 27017
volumes:
- ./data/db:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
package.json
{
"name": "node-playground",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon -L"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^2.7.1",
"express": "^4.17.1",
"mongoose": "^5.7.1"
},
"devDependencies": {
"nodemon": "^1.19.2"
}
}
I tried many different setups as well. Like not installing globally nodemon
but only as a project dependency. And also running the command at the docker-compse.yml
, and i believe many others I don't remember right now. Nothing.
If someone has any cetainty about this, please help. Thanks!!!!
I went ahead and created an example container and repo to show how you can achieve this..
Just follow the steps below, which outline how to use nodemon
inside of a Docker
container.
Docker Container: at DockerHub
Source Code: at GitHub
package.json:
{
"name": "nodemon-docker-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start:express": "node ./index.js",
"start": "nodemon"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"nodemon": "^1.19.2"
}
}
Dockerfile:
FROM node:slim
WORKDIR /app
COPY package*.json ./
RUN apt-get update
RUN npm install
COPY . /app
# -or-
# COPY . .
EXPOSE 1337
CMD ["npm", "start"]
docker-compose.yml: (if you are using it)
version: "3"
services:
nodemon-test:
image: oze4/nodemon-docker-test
ports:
- "1337:1337"
How to reproduce:
Step 1 USING DOCKER RUN: SKIP IF YOU ARE USING DOCKER COMPOSE (go to step 1 below if you are) pull down example docker container
docker run -d --name "nodemon-test" -p 1337:1337 oze4/nodemon-docker-test
Step 1 USING DOCKER-COMPOSE:
See the docker-compose.yml
file above for configuration
cd /path/to/dir/that/has/your/compose/file
docker-compose up -d
Step 2: verify the app works
Step 3: check the container logs, to get a baseline
docker logs nodemon-test
Step 4: I have included a bash
script to make editing a file as simple as possible. We need to pop a shell on the container, and run the bash
script (change.sh
)
docker exec -it nodemon-test /bin/bash
bash change.sh
exit
Step 5: check the logs again to verify changes were made and that nodemon
restarted
docker logs nodemon-test
As you can see by the last screenshot, nodemon
successfully restarted after changes were made!
这篇关于文件更改时,Nodemon不会重新加载到Docker容器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!