Deploying your ReactJS Application to a Docker container

Building the application ready for deployment

  • Use the npm command supplied with the create-react-app scaffold to build a production ready version of the application.

npm run build
  • Wait for the process to complete and observe the new folder called build that has been created. This contains everything that is needed to deploy the application.

Create a Dockerfile to configure the Docker Container

  • In the root folder of the project, create a new file called Dockerfile.

  • Add the following code to the Dockerfile:

FROM nginx:alpine COPY /build /usr/share/nginx/html EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]
  • Save the file

Build the Docker Image

  • Point the command line at the folder that contains your Dockerfile - this should be the root folder of your project.

  • Build a local image replacing <image_name> with your name for the image using the command:

docker build . -t <image_name>
  • Wait for the process to finish.

Running an Instance of the Image

  • Run the container locally using the command:

  • Wait for the process to finish.

Viewing Your Application

  • Open a browser and point it to http://<container IP>