How to Run a React App in a Docker Container

How to Run a React App in a Docker Container

In this tutorial, I am going to explain the simple and easy steps to run a React App in a docker container.

First you need to create an empty react application by following this create-react-app steps. Once you have created the sample react app, the follow the below steps:

  1. Install create-react-app by following command (npm install create-react-app --global)
  2. Create a new React app ( create-react-app react-docker-app)
  3. Go to the react-docker-app folder and run it, to make sure all is good. (cd react-docker-app && yarn start )

That’s all. Now jump to create a Dockerfile file on root directory of your project. Below is the sample code for Dockerfile.

Now build the image, you can run the following command from the project root folder, where your Docker-file is:

Finally, run this container now. To run it locally, you need to provide the name of the image and the port we want the React app to be accessible on. Note that we used port 80 in the serve command, so need to make sure to use 80 when specifying the container port like this:

Now you can access you react application using http://localhost:8080

If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.

Recommended Posts:

Article Tags: , , ,