27

I am trying to run the create-react-app's development server inside of a docker container and have it recompile and send the changed app code to the client for development purposes, but it isn't picking up the changes from inside of the docker container.

(Of course, I have the working directory of the app as a volume for the container.)

Is there a way to do make this work?

2
  • If you are launching with something like docker run -it --rm -v ${PWD}:/app -v /app/node_modules -p 3001:3000 -e CHOKIDAR_USEPOLLING=true sample:dev make sure you use powershell and that the variable interpolation works ( the part with ${} ). I was working with git bash and it was creating some weird folder and the react live reload was not working. Switching to powershell fixed the issues. Commented Nov 30, 2020 at 18:28
  • I had to write $(pwd) as "$(pwd)" cuz one of the directories in my workspace path had spaces in its name. Commented Jan 7 at 18:32

8 Answers 8

56

Actually, I found an answer here. Apparently create-react-app uses chokidar to watch file changes, and it has a flag CHOKIDAR_USEPOLLING to use polling to watch for file changes instead. So CHOKIDAR_USEPOLLING=true npm start should fix the problem. As for me, I set CHOKIDAR_USEPOLLING=true in my environment variable for the docker container and just started the container.

Sign up to request clarification or add additional context in comments.

6 Comments

This fixes but there is a significant delay for hot reloading and my cpu usage was above 60%
Yes I agree it isn't the optimal solution. Please post an answer if you've found a better solution.
See the answer I just added.
Creating a .env file with CHOKIDAR_USEPOLLING=true worked for me (see stackoverflow.com/a/43281575/470749 and create-react-app.dev/docs/troubleshooting and stackoverflow.com/a/57691204/470749 )
You can configure webpack to use polling and ignore the node_modules while watching. This solves the CPU problem. See the answer for Vue (should be similar for React): stackoverflow.com/a/67209565/3233827
|
10

Polling, suggested in the other answer, will cause much higher CPU usage and drain your battery quickly. You should not need CHOKIDAR_USEPOLLING=true since file system events should be propagated to the container. Since recently this should work even if your host machine runs Windows: https://docs.docker.com/docker-for-windows/release-notes/#docker-desktop-community-2200 (search for "inotify").

However, when using Docker for Mac, this mechanism seems to be failing sometimes: https://github.com/docker/for-mac/issues/2417#issuecomment-462432314

Restarting the Docker daemon helps in my case.

Comments

7

With react-script v5.0.0 onward the command is WATCHPACK_POLLING=true instead of CHOKIDAR_USEPOLLING=true

1 Comment

I am using react-script v5.0.1 on a dockerized react app and when I go with your solution webpack compiles successfully but browser staying still. After compilation I must refresh browser. How can I solve this issue? PS: When I use CHOKIDAR_USEPOLLING browser refreshes but changes are not compiled before it.
3

Clear Answer for react-script v5.0.0 onward


1- Create a .env file in the root directory of the project

2- Add the WATCHPACK_POLLING=true to the .env file

3- build new image

4- run new container

5- verify that the changes being detected.


Or you can just add WATCHPACK_POLLING=true to your script for making the container like this

docker run --name my-app -it --rm -v $(pwd)/src:/app/src -p 3000:3000 -e WATCHPACK_POLLING=true myapp

1 Comment

I am using react-script v5.0.1 on a dockerized react app and when I go with your solution webpack compiles successfully but browser staying still. After compilation I must refresh browser. How can I solve this issue? PS: When I use CHOKIDAR_USEPOLLING browser refreshes but changes are not compiled before it.
2

If your changes are not being picked up, it is probably a problem with the file watching mechanism. A workaround for this issue is to configure polling. You can do that globally as explained by @Javascriptonian, but you can do this also locally via the webpack configuration. This has the benefit of specifying ignored folders (e.g. node_modules) which slow down the watching process (and lead to high CPU usage) when using polling.

In your webpack configuration, add the following configuration:

devServer: {
  watchOptions: {
    poll: true, // or use an integer for a check every x milliseconds, e.g. poll: 1000
    ignored: /node_modules/ // otherwise it takes a lot of time to refresh
  }
}

source: documentation webpack watchOptions


If you are having the same issue with nodemon in a back-end Node.js project, you can use the --legacy-watch flag (short -L) which starts polling too.

npm exec nodemon -- --legacy-watch --watch src src/main.js

or in package.json:

"scripts": {
  "serve": "nodemon --legacy-watch --watch src src/main.js"
}

documentation: nodemon legacy watch

Comments

1

If you use linux then you don't need to use CHOKIDAR_USEPOLLING=true

1 Comment

I would be highly interested in the answer to "Why?".
0

In my case, I was running the docker run command in a Git bash command line (on Windows) and the hot reloading was not working. Using react-script v5.0.0, setting WATCHPACK_POLLING=true in the .env file and running the docker run command in PowerShell worked.

docker run -it --rm -v ${PWD}:/app -v /app/node_modules -p 3000:3000 -e CHOKIDAR_USEPOLLING=true myapp

Comments

0

I had to debug my source with different node versions and various options mentioned above but none worked unfortunately, however the only solution was to properly map the container to host source and use the WORKDIR of the Dockerfile in the docker-compose.json file.

Dockerfile

### EBMBOOK'S SINGLE STAGE NODE VERSION 
FROM node:18.13-alpine

# Create a Virtual directory inside the docker image
WORKDIR /app

# install app dependencies - install will only be triggered if there are new changes in the package.*json files
COPY package.json /app/package.json
COPY package-lock.json /app/package-lock.json
RUN npm install

# copy app too
COPY ./ /app

EXPOSE 3000

# Deploy app for local development
CMD [ "npm", "start" ]

And, in the docker-compose.json link back the source to WORKDIR and of course don't forget the environment WATCHPACK_POLLING=true either.

docker-compose.json

services:
  # frontend
  frontend:
    container_name: central.ebmbook
    restart: always
    build:
      dockerfile: Dockerfile.dev
      context: ../../frontend
    volumes:
      - ../../frontend/src:/app/src
      - /app/node_modules
    environment:
      - WDS_SOCKET_HOST=127.0.0.1 
      - WATCHPACK_POLLING=true 
    networks:
      - public
    ports:
      - 3000:3000
      
    ....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.