Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running development server with create-react-app inside of a docker container

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?

like image 913
hwkd Avatar asked Jun 20 '17 02:06

hwkd


People also ask

How do I run React app in container?

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.

Can I use Docker With React?

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.

How do I set up Docker for React app?

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 .


2 Answers

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.

like image 59
hwkd Avatar answered Sep 28 '22 04:09

hwkd


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.

like image 41
Juliusz Gonera Avatar answered Sep 28 '22 05:09

Juliusz Gonera