Language and Framework Guides
Run a ReactJS Container App
23 min
back4app containers is a powerful platform for hosting react applications with its ability to automatically deploy dockerized react apps, you can launch your project in a scalable and flexible environment with ease in this guide, we will walk you through the process of preparing and deploying your react application on back4app containers, covering everything from simple projects to more complex setups we will begin with the necessary preparations, then move on to dockerizing the application, testing it locally, pushing the project to github, setting up the project on back4app containers, monitoring deployment, and troubleshooting common issues if you have any questions or comments, feel free to join the conversation in the #containers channel on the back4app community on slack for discussions specific to react on back4app containers anytime you can also contact us at community\@back4app com at any time, if you want to check a sample working react project on back4app containers, go to https //github com/templates back4app/containers react sample 1 prepare your react application a project structure verify that your react application follows a proper directory structure, with all necessary files and folders, such as src , public , components , and assets , organized appropriately the main entry point for your application should be the src/index js or src/index jsx file b dependencies check if all required dependencies are listed in the package json file, including their correct versions ensure that you have installed all dependencies using npm install or yarn install to generate a package lock json or yarn lock file c build process for more complex projects that involve a build process, ensure that your build scripts and configurations (e g , webpack, babel, or create react app) are properly set up you should be able to run the build process locally without any issues d environment variables if your application relies on environment variables, ensure that you have a env file with the necessary variables defined when deploying to back4app containers, you will need to configure these environment variables in the deployment settings e server setup (if applicable) if your react application includes a custom server (e g , express), make sure it is correctly set up and configured to serve your react application test your server locally to ensure it works as expected f application optimization optimize your react application by minimizing bundle sizes, using code splitting, and implementing performance best practices use tools like lighthouse to audit your application and address any performance or accessibility issues g cross browser compatibility test your application across multiple browsers and devices to ensure proper rendering and functionality once you have thoroughly reviewed and prepared your react application, you can proceed to the next step, which is creating a dockerfile for your project 2 dockerization dockerizing a react application involves creating a dockerfile in the root directory of your project the dockerfile contains instructions to build a docker image of your application, which can then be deployed to back4app containers here's a detailed explanation of how to create a dockerfile for a regular react application 1 create a new file named dockerfile (without any file extension) in the root directory of your react application 2 define the base image start the dockerfile by specifying a base image using the from command for a typical react application, the base image should be a node js image, e g , node 14 or node 16 from node 14 3 set the working directory use the workdir command to set the working directory for your application within the docker container this is where your application files will be stored and executed workdir /app 3 copy package json and package lock json copy the package json and package lock json files from your local machine to the docker container using the copy command these files are required to install your application's dependencies copy package json / 4 install dependencies use the run command to install your application's dependencies from the package json file this is typically done using npm ci, which installs the exact dependency versions specified in the package lock json file run npm ci 5 copy the rest of the project files use the copy command again to copy the remaining files and folders from your local machine to the docker container copy 6 build the react application add a run command to build your react application using your build script, typically npm run build this generates a production ready version of your react application in the build folder run npm run build 7 configure the server you need a server to serve your built react application one common choice is serve first, install it globally in the docker container using the run command run npm install g serve 8 expose the server port use the expose command to specify the port on which your server will run inside the docker container for example, you can use port 5000 expose 5000 9 start the server use the cmd command to specify the command that starts the server to serve your built react application with serve, you can specify the s flag for a single page application and the build folder as the source cmd \["serve", " s", "build", " l", "5000"] the complete dockerfile for a regular react application should look like this from node 14 workdir /app copy package json / run npm ci copy run npm run build run npm install g serve expose 5000 cmd \["serve", " s", "build", " l", "5000"] another dockerfiles examples you can use as a reference example 1 basic react application this dockerfile is for a simple react application it installs the necessary packages from the package json file, copies the project files, serves the react application using nginx exposing port 80 the cmd specifies the command to start the nginx server \# stage 1 build the react application from node 14 as build workdir /app copy package json / run npm install copy run npm run build \# stage 2 serve the react application using nginx from nginx\ stable alpine copy from=build /app/build /usr/share/nginx/html \# copy the default nginx conf provided by the docker image copy nginx/nginx conf /etc/nginx/conf d/default conf expose 80 cmd \["nginx", " g", "daemon off;"] example 2 react application with a custom server this dockerfile is for a more complex react application that uses a custom server (e g , express) it installs the necessary packages from the package json file, copies the project files, and exposes port 8000 for the custom server the cmd specifies the command to start the custom server \# base image from node 14 \# working directory workdir /app \# copy package json and package lock json and install dependencies copy package json / run npm ci \# copy the rest of the project files copy \# expose the server port expose 8000 \# command to start the server cmd \["npm", "run", "server"] 4 test your project locally before deploying your react application on back4app containers, it's important to test it locally using docker this helps ensure that your application runs as expected and helps you identify and fix any issues before deployment build the docker image for your react application in your terminal, navigate to your project's root directory and run the following command, replacing your app name with the name of your application docker build t your app name run the docker container locally next, run the following command to start the docker container locally this command maps the container's exposed port (e g , 3000) to a port on your local machine (e g , 3000) docker run p 3000 3000 your app name test your application open a web browser and navigate to http //localhost 3000 http //localhost 3000 to view your react application make sure everything works as expected if you encounter any issues, address them before moving on to the next step 5 push your project to github create a gitignore file in your project's root directory to exclude unnecessary or sensitive files from your repository (e g , node modules , env , build , etc ) initialize a git repository, commit your project files, and push them to a remote repository (e g , on github) git add git commit m "initial commit" git remote add origin \<your remote repo url> git push u origin master``` 6 deploy your application on back4app containers after creating your back4app account you can follow the steps listed on the docs 1 connect you github repo with back4app 2 prepare your project for deployment in summary containers will follow the instructions detailed on your dockerfile and start to create your app 7 monitor deployment and address possible errors keep an eye on the deployment logs and status on the back4app containers dashboard address any errors or issues that arise during deployment in case of more complex projects, ensure that all necessary services (such as databases or external apis) are correctly configured and accessible 8 troubleshooting common problems common deployments errors when running a react app on back4app containers are listed here other possible common errors when deploying a react application are using npm start instead of npm run build npm start is recomended when you have a development scenario but for production environments its more adequate to use npm run build also this building command will result in less memory conumption serve static files using nginx another good practive is to serve static files with an application server like nginx incorrect port configuration react applications should be configured to run on a specified port when deployed on back4app containers if the application is still not accessible, check the dockerfile to make sure the correct port is exposed (e g , expose 5000 for port 5000) incompatible or missing dependencies ensure that all required dependencies are listed in the package json file and that their versions are compatible with each other and your application code missing or incompatible dependencies can lead to runtime errors invalid environment variables or configuration check if your application relies on specific environment variables or configuration files, and ensure they are correctly set up in the back4app containers environment set any necessary environment variables in your dockerfile using the env command application crashes or unhandled exceptions unhandled exceptions or crashes in your react application code can cause deployment failures or unexpected behavior examine your application logs for any error messages, and address any issues in your code check the container logs by running docker logs your app name to see if there are any errors or exceptions being thrown use a tool like sentry to track and monitor errors in your application server side rendering configuration if your react application uses server side rendering (ssr), ensure that your ssr setup is correctly configured in your dockerfile and that it starts your application using the right entry point if you encounter any other issues while deploying your react application on back4app containers, contact the back4app support team at community\@back4app com 9 scale your application for more complex projects that require additional resources or horizontal/vertical scaling, consider upgrading your back4app containers plan to handle increased traffic and load 10 sample application for a sample django application project you can go to https //github com/templates back4app/containers python flask sample