当数据在Docker容器内更改时,Create-react-app服务器不会重新编译 [英] Create-react-app server not recompiling when data changes inside docker container

查看:106
本文介绍了当数据在Docker容器内更改时,Create-react-app服务器不会重新编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在docker内部运行React应用程序.我正在容器中运行 npm start 的create-react-app软件包中包含的脚本运行,并且正在使用绑定安装在主机中工作并反映容器中的更改.遗憾的是,即使文件确实发生了更改,react软件包随附的重新编译功能也无法在容器内部运行.所要抓住的是,我正在使用摘要工具箱.你们知道这可能是什么问题吗?为什么不重新编译?

I'm currently trying to run a React application inside docker. I'm running the scripts included in the create-react-app package running npm start inside the container and I'm using bind mounts to work in the host and reflect changes in the container. Sadly, the recompile feature that comes with the react package is not working inside the container even though the files do change. The catch is that I'm using docket toolbox. Do you guys know what could be the problem? Why isn't it recompiling?

我的文件结构如下.

project
|   .dockerignore
|   .gitignore
|   docker-compose.yml
|   Dockerfile
|   LICENSE
|   README.md
|
\---frontend
    +---nodemodules\*
    |   package-lock.json
    |   package.json
    |   README.md
    |
    +---public
    |       index.html
    |
    \---src
        |   index.js
        |
        \---container
                App.jsx

Dockerfile

FROM node:8.11.1

COPY . /usr/src/

WORKDIR /usr/src/frontend

ENV PATH /usr/src/frontend/node_modules/.bin:$PATH

RUN npm install -g create-react-app
RUN npm install

EXPOSE 3000

CMD [ "npm", "start" ]

docker-compose.yml

version: "3.5"

services:
  react:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - "./frontend:/usr/src/frontend"
      - '/usr/src/frontend/node_modules'
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=development

推荐答案

有时候,当应用在容器中运行时,React不会在更改文件时重新编译代码.

Sometimes React does not recompile the code upon changing files when app is running inside the container.

如果项目在虚拟机(例如由Vagrant提供的VirtualBox)之类的虚拟机中运行,请在项目目录中创建一个 .env 文件(如果该文件不存在),然后添加 CHOKIDAR_USEPOLLING =正确.这样可以确保下次运行 npm start 时,观察者根据需要在VM内部使用轮询模式.有关详细信息,请参见文档.

If the project runs inside a virtual machine such as (a Vagrant provisioned) VirtualBox, create an .env file in your project directory if it doesn’t exist, and add CHOKIDAR_USEPOLLING=true to it. This ensures that the next time you run npm start, the watcher uses the polling mode, as necessary inside a VM. See documentation for details.

这篇关于当数据在Docker容器内更改时,Create-react-app服务器不会重新编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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