Here are simple steps on how to create an empty React app, create a production build of the app and run it inside of a Docker container.
Start with creating an new React app:
- Install create-react-app:
npm install create-react-app --global
2. Create a sample React app:
3. Run the app to make sure all is good:
Let’s add a Dockerfile with the following contents to the root folder:
Lines 1–4 are the first stage of the build, where we copy all source code to the container and execute yarn run build to create a production build.
Lines 6–10 are the second stage of the build where we install serve and on line 9 we copy the output from the first stage (from folder /app/build to current folder (/app)). Finally, we run serve to run the app.
To build the image, you can run docker build -t react-docker-app and run it with docker run -dit -p 8080:80 react-docker-app .
At this point, you can navigate to http://localhost:8080 and see the app running there.