skip to Main Content

I’m creating a React app with docker in WSL2 and create-react-app and everything seems to be working fine except the app is not updating with changes in the code. I mean, when I make a change in the code, the browser should update the changes automatically, but it doesn’t and I have to restart the container to see them. I added CHOKIDAR_USEPOLLING=true in ENV but it’s not working either. These are the configuration files:

dockerfile

# pull official base image
FROM node:16.13.1

# set working directory
WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH

# install app dependencies

COPY package.json ./
COPY package-lock.json ./
RUN npm install

# add app
COPY . ./

# start app
CMD ["npm", "start"]

docker-compose.yml

services:
  react:
    build: ./frontend
    command: npm start
    ports:
      - 3000:3000
    volumes: 
      - ./frontend:/app
      - '/app/node_modules'
    env_file:
      - 'env.react'

env.react

CHOKIDAR_USEPOLLING=true

package.json

{
  "name": "app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "mdbreact": "^5.2.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-painter": "^0.4.0",
    "react-router-dom": "^6.2.1",
    "react-scripts": "5.0.0",
    "sass": "^1.45.1",
    "web-vitals": "^2.1.2"
  },
  "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"
    ]
  }
}

Can you see what I’m doing wrong? Thanks!

5

Answers


  1. Chosen as BEST ANSWER

    The problem is with WSL2, as it's said in this answer www.github.com/microsoft/WSL/issues/6255#issuecomment-730701001: "If it is in the Windows file system, I believe you're running into the fact that the Plan 9 server in WSL 2 does not support file watching on Windows files. As a workaround I'd recommend you place your files into the Linux file system". I moved the files to \wsl$Ubuntuhomeuser and now the reload is working fine. I also stopped using Docker with React and now I directly use Node, as recommended in the comments.

    Note that I was having the same issue with Node alone, with files outside the Linux file system.


  2. Add this enviroment variable:

    environment:
          - CHOKIDAR_USEPOLLING=true
    
    Login or Signup to reply.
  3. CHOKIDAR_USEPOLLING will no longer work in react-scripts ^5, as Webpack started using its own filesystem watcher (Watchpack) as a replacement for Chokidar, try:

    environment:
      - WATCHPACK_POLLING=true
    
    Login or Signup to reply.
  4. I’m using ubuntu:22.04 but I dont know why in Docker the live react-app update doesn’t work for me while running the container …..In stackoverflow i found CHOKIDAR_USEPOLLING but this also don’t work any more.

    Instead of that you can just add -e WATCHPACK_POLLING=true in your docker run command …and that worked for me.

    Login or Signup to reply.
  5. Note: use cmd terminal instead of git-bash

    For mounting volume on windows following command not working for GIT-bash but works fine on cmd terminal

    docker run -d -p 3000:3000 --name react-container -v %cd%/src:/app/src -e CHOKIDAR_USEPOLLING=true react-img
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search