Docker化的React应用与Nodemon监控

huangapple go评论72阅读模式
英文:

Dockerized React App x Nodemon monitoring

问题

I've got a React App running on docker with Nodemon checking for any modifications in the App.js file. I first initialise the dockerfile via an initialise.bat which just builds.

initialise.bat

docker build . -f Dockerfile -t react-dev-app

DockerFile


WORKDIR /app

COPY package.json package-lock.json /app/
COPY public /app/public
COPY src /app/src

RUN npm install --verbose --only=production
COPY . .
RUN npm install --verbose -g nodemon

EXPOSE 3000

CMD ["nodemon", "--watch", "**/*", "App.js"]

docker-compose.yml

services:
  devreact:
    image: react-dev-app
    stdin_open: true
    tty: true
    volumes:
      - ./:/app
      - node_modules:/app/node_modules
    ports: 
      - "3000:3000"
volumes:
  node_modules:

Package.Json

    "name": "app",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "@testing-library/jest-dom": "^5.16.5",
        "@testing-library/react": "^13.4.0",
        "@testing-library/user-event": "^13.5.0",
        "nodemon": "^2.0.22",
        "nth-check": "^2.0.1",
        "react": "^18.2.0",
        "react-dom": "^18.2.0",
        "react-scripts": "^5.0.1",
        "web-vitals": "^2.1.4"
    },
    "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
    },
    "eslintConfig": {
        "extends": [
            "react-app",
            "react-app/jest"
        ]
    },
    "browserslist": {
        "production": [
            ">0.2%",
            "not dead",
            "not op_mini all"
        ],
        "development": [
            "last 1 chrome version",
            "last 1 firefox version",
            "last 1 safari version"
        ]
    },
    "devDependencies": {
        "css-select": "^4.1.3",
        "nodemon": "^2.0.22"
    }
}```

<details>
<summary>英文:</summary>

I&#39;ve got a React App running on docker with Nodemon checking for any modifications in the App.js file. I first initialise the dockerfile via an initialise.bat which just builds.

**initialise.bat**

docker build . -f Dockerfile -t react-dev-app



**DockerFile**


FROM node:latest

WORKDIR /app

COPY package.json package-lock.json /app/
COPY public /app/public
COPY src /app/src

RUN npm install --verbose --only=production
COPY . .
RUN npm install --verbose -g nodemon

EXPOSE 3000

CMD ["nodemon", "--watch", "**/*", "App.js"]

**docker-compose.yml**
```version: &#39;3&#39;
services:
  devreact:
    image: react-dev-app
    stdin_open: true
    tty: true
    volumes:
      - ./:/app
      -  node_modules:/app/node_modules
    ports: 
      - &quot;3000:3000&quot;
volumes:
  node_modules:

Package.Json

{
    &quot;name&quot;: &quot;app&quot;,
    &quot;version&quot;: &quot;0.1.0&quot;,
    &quot;private&quot;: true,
    &quot;dependencies&quot;: {
        &quot;@testing-library/jest-dom&quot;: &quot;^5.16.5&quot;,
        &quot;@testing-library/react&quot;: &quot;^13.4.0&quot;,
        &quot;@testing-library/user-event&quot;: &quot;^13.5.0&quot;,
        &quot;nodemon&quot;: &quot;^2.0.22&quot;,
        &quot;nth-check&quot;: &quot;^2.0.1&quot;,
        &quot;react&quot;: &quot;^18.2.0&quot;,
        &quot;react-dom&quot;: &quot;^18.2.0&quot;,
        &quot;react-scripts&quot;: &quot;^5.0.1&quot;,
        &quot;web-vitals&quot;: &quot;^2.1.4&quot;
    },
    &quot;scripts&quot;: {
        &quot;start&quot;: &quot;react-scripts start&quot;,
        &quot;build&quot;: &quot;react-scripts build&quot;,
        &quot;test&quot;: &quot;react-scripts test&quot;,
        &quot;eject&quot;: &quot;react-scripts eject&quot;
    },
    &quot;eslintConfig&quot;: {
        &quot;extends&quot;: [
            &quot;react-app&quot;,
            &quot;react-app/jest&quot;
        ]
    },
    &quot;browserslist&quot;: {
        &quot;production&quot;: [
            &quot;&gt;0.2%&quot;,
            &quot;not dead&quot;,
            &quot;not op_mini all&quot;
        ],
        &quot;development&quot;: [
            &quot;last 1 chrome version&quot;,
            &quot;last 1 firefox version&quot;,
            &quot;last 1 safari version&quot;
        ]
    },
    &quot;devDependencies&quot;: {
        &quot;css-select&quot;: &quot;^4.1.3&quot;,
        &quot;nodemon&quot;: &quot;^2.0.22&quot;
    }
}

the react app starts up fine but it's not detecting any changes made in App.js or anywhere.

i tried changing the start string in Package.json but no luck there. also tried a different package as an alternative to nodemon and still got the same outcome. tried different CMD commands in the dockerfile and still no luck. i either enter a big loop or it just errors.

EDIT: there appears to be some sort of cross-compatibility issue between Mac and pc. On Mac, file changes are being monitored and then restarts as intended then I accessed the files on my windows pc no changes are being monitored.

答案1

得分: 0

在Windows系统中,你需要加入 --legacy watch 选项来检查文件的变化。

version: '3'
services:
  devreact:
    build:
      context: .
      dockerfile: Dockerfile
    stdin_open: true
    tty: true
    volumes:
      - .:/app
      - node_modules:/app/node_modules
    ports:
      - "3000:3000"
    command: nodemon --legacy-watch App.js
volumes:
  node_modules:
英文:

turns out in windows you need to put --legacy watch to check files changes.

version: &#39;3&#39;
services:
  devreact:
    build:
      context: .
      dockerfile: Dockerfile
    stdin_open: true
    tty: true
    volumes:
      - .:/app
      - node_modules:/app/node_modules
    ports:
      - &quot;3000:3000&quot;
    command: nodemon --legacy-watch App.js
volumes:
  node_modules:

huangapple
  • 本文由 发表于 2023年4月6日 21:21:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950027.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定