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?
Step 1: Create a React application using the following command. Step 2: Move to the project_name folder. Project Structure: At this point, the project structure should look like this. Dockerfile for development: At the root of our react project create a Dockerfile for the development phase.
React is a JavaScript library for building user interfaces. It makes it painless to create interactive UIs. You can design simple views for each state in your application, and React efficiently update and render just the right components when your data changes.
Set Up a React App You first instructed Docker to pull the official Node image, set a working directory for your app, and install all your dependencies. To ensure you don't end up with a bloated container, add a . dockerignore file to avoid copying unnecessary files into the container. Inside your .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With