在重新启动 Swarm 之前,NodeJS 未检测到 Docker Bind Mount 中的更改
NodeJS is not detecting change in Docker Bind Mount until Swarm is restarted
我正在 Docker 上以 Swarm 模式(单节点)构建一个 NodeJS 应用程序。我正在为 NodeJS 源代码使用绑定安装卷。一切运行完美,我可以在本地主机中看到 NodeJS 和 Express 的输出,但是当我更改 NodeJS 代码(位于绑定安装卷中)中的某些内容时,没有任何变化。我必须重新启动我的服务才能观察到变化。早些时候,当我只使用 Docker Compose 时,它从未发生过,但现在当我切换到 Swarm 时,我遇到了问题。
我在 macOS 10.14.6
上使用 Docker 18 和 Visual Studio 代码 1.39
Docker文件
FROM node:12-alpine
WORKDIR /node-dir
COPY package*.json ./
RUN npm install
docker-compose.yml 文件
# Docker-compose.yml
version: '3.7'
services:
node-service:
image: node-img:1.0
ports:
- 80:3000
working_dir: "/node-dir"
volumes:
- ./node-dir/source:/node-dir/source
networks:
- ness-net
command: npm start
networks:
ness-net:
我还了解到这可能是由于索引节点的原因,大多数编辑器在保存文件时都会破坏 link。但它在 docker-compose with Visual Studio 代码下工作正常,它的行为仅在 Docker Swarm 中改变。
更新:我使用带绑定挂载的 Nginx 提供了一个静态 html 文件,我可以使用 VS Code 轻松更改该文件并反映出来。它是唯一不检测文件更改的 NodeJS。
如果您的卷映射正确,源代码更改应该到达您的 node.js 应用程序容器。
您可以在 docker 主机上进行更改后,通过检查容器内的源代码来验证它。
I'm currently in development mode, and I have to test the source code
repeatedly so I want to use bind mounts to make development and
testing easier.
但是,在容器内的节点进程重新加载并获取更改之前,您的源代码更改不会生效。
为了实现这一点,您必须 use nodemon。 Nodemon 将选择源代码中的更改并重新加载节点进程。
另一种更长的替代方法是构建新的 docker 图像,然后使用以下方法更新您的应用:docker service update --image=...
我正在 Docker 上以 Swarm 模式(单节点)构建一个 NodeJS 应用程序。我正在为 NodeJS 源代码使用绑定安装卷。一切运行完美,我可以在本地主机中看到 NodeJS 和 Express 的输出,但是当我更改 NodeJS 代码(位于绑定安装卷中)中的某些内容时,没有任何变化。我必须重新启动我的服务才能观察到变化。早些时候,当我只使用 Docker Compose 时,它从未发生过,但现在当我切换到 Swarm 时,我遇到了问题。
我在 macOS 10.14.6
上使用 Docker 18 和 Visual Studio 代码 1.39Docker文件
FROM node:12-alpine
WORKDIR /node-dir
COPY package*.json ./
RUN npm install
docker-compose.yml 文件
# Docker-compose.yml
version: '3.7'
services:
node-service:
image: node-img:1.0
ports:
- 80:3000
working_dir: "/node-dir"
volumes:
- ./node-dir/source:/node-dir/source
networks:
- ness-net
command: npm start
networks:
ness-net:
我还了解到这可能是由于索引节点的原因,大多数编辑器在保存文件时都会破坏 link。但它在 docker-compose with Visual Studio 代码下工作正常,它的行为仅在 Docker Swarm 中改变。
更新:我使用带绑定挂载的 Nginx 提供了一个静态 html 文件,我可以使用 VS Code 轻松更改该文件并反映出来。它是唯一不检测文件更改的 NodeJS。
如果您的卷映射正确,源代码更改应该到达您的 node.js 应用程序容器。
您可以在 docker 主机上进行更改后,通过检查容器内的源代码来验证它。
I'm currently in development mode, and I have to test the source code repeatedly so I want to use bind mounts to make development and testing easier.
但是,在容器内的节点进程重新加载并获取更改之前,您的源代码更改不会生效。
为了实现这一点,您必须 use nodemon。 Nodemon 将选择源代码中的更改并重新加载节点进程。
另一种更长的替代方法是构建新的 docker 图像,然后使用以下方法更新您的应用:docker service update --image=...