Node.js Docker容器未更新到卷的更改 [英] Node.js docker container not updating to changes in volume

查看:59
本文介绍了Node.js Docker容器未更新到卷的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows计算机上托管开发环境,该环境托管前端和后端容器.到目前为止,我只在后端上工作.所有文件都在通过Docker桌面共享的C驱动器上.

I am trying to host a development environment on my Windows machine which hosts a frontend and backend container. So far I have only been working on the backend. All files are on the C Drive which is shared via Docker Desktop.

我有以下docker-compose文件和Dockerfile,后者在根目录内的一个名为backend的目录内.

I have the following docker-compose file and Dockerfile, the latter is inside a directory called backend within the root directory.

Dockerfile:

Dockerfile:

FROM node:12.15.0-alpine

WORKDIR /usr/app

COPY package*.json ./

RUN npm install

EXPOSE 5000

CMD [ "npm", "start" ]

docker-compose.yml:

docker-compose.yml:

version: "3"
services:
  backend:
    container_name: backend
    build:
        context: ./backend
        dockerfile: Dockerfile
    volumes:
      - ./backend:/usr/app
    environment:
      - APP_PORT=80
    ports:
      - '5000:5000'

  client:
    container_name: client
    build:
      context: ./client
      dockerfile: Dockerfile
    volumes:
      - ./client:/app
    ports:
      - '80:8080'

由于某些原因,当我在本地文件中进行更改时,它们未反映在容器内.我正在通过稍微修改我的一个文件的输出来进行测试,但是我每次都必须重新构建容器才能看到更改生效.

For some reason, when I make changes in my local files they are not reflecting inside the container. I am testing this by slightly modifying the outputs of one of my files, but I am having to rebuild the container each time to see the changes take effect.

我以前在PHP应用程序中使用Docker,并且基本上做了相同的事情.所以我不确定为什么Node.js应用程序不能使用它.我想知道我是否就为什么不起作用缺少明显的东西.

I have worked with Docker in PHP applications before, and have basically done the same thing. So I am unsure why this is not working with by Node.js app. I am wondering if I am just missing something glaringly obvious as to why this is not working.

任何帮助将不胜感激.

推荐答案

这里的node和PHP的区别在于php自动获取请求之间的文件系统更改,而节点服务器却没有.

The difference between node and PHP here is that php automatically picks up file system changes between requests, but a node server doesn't.

我想,如果您通过用docker-compose down然后再弹起容器重新启动节点来重新启动节点,则文件更改将被提取(无需重建!).

I think you'll see that the file changes get picked up if you restart node by bouncing the container with docker-compose down then up (no need to rebuild things!).

如果您希望节点无需重新启动服务器即可选择文件系统更改,则可以使用某些节点工具.nodemon是一个: https://www.npmjs.com/package/nodemon .按照本地安装的安装说明进行操作,并更新您的启动脚本以使用nodemon代替node.

If you want node to pick up file system changes without needing to bounce the server you can use some of the node tooling. nodemon is one: https://www.npmjs.com/package/nodemon. Follow the installation instructions for local installation and update your start script to use nodemon instead of node.

另外,我确实确实认为您的dockerfile中有一个错误,您需要将源代码复制到您的工作目录中.我假设您从这里获得了最初的食谱:

Plus I really do think you have a mistake in your dockerfile and you need to copy the source code into your working directory. I'm assuming you got your initial recipe from here: https://dev.to/alex_barashkov/using-docker-for-nodejs-in-development-and-production-3cgp. This is the docker file is below. You missed a step!

FROM node:10-alpine

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

CMD [ "npm", "start" ]

这篇关于Node.js Docker容器未更新到卷的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆