Deploying a React application can sometimes be a daunting task, especially for those new to the world of cloud computing and containerization. In this blog, we will navigate through the process of deploying a simple React application using Docker and Google Cloud Run. By the end, you’ll have a better understanding of how to make your application accessible to the world.
Before diving into the deployment process, make sure you have:
Let’s start by creating a basic React application. You can easily do this using Create React App. Open your terminal and run the following command:
npx create-react-app my-react-app cd my-react-app
This command sets up a new directory with a simple React starter application.
Now that your React application is created, let’s run it locally to ensure everything works as expected:
npm start
Open your browser and navigate to http://localhost:3000. You should see your new React app running smoothly.
Next up, we’ll create a Dockerfile to containerize your application. In the root of your React app directory, create a file named Dockerfile
with the following content:
# Step 1: Use the official Node.js image as the base image FROM node:14 as build # Step 2: Set the working directory WORKDIR /usr/src/app # Step 3: Copy package.json and package-lock.json COPY package*.json ./ # Step 4: Install all dependencies RUN npm install # Step 5: Copy the rest of the application code COPY . . # Step 6: Build the application RUN npm run build # Step 7: Use a web server to serve the static files FROM nginx:alpine # Step 8: Copy build files to the Nginx server COPY /usr/src/app/build /usr/share/nginx/html # Step 9: Expose the port the app runs on EXPOSE 80 # Step 10: Command to run the server CMD ["nginx", "-g", "daemon off;"]
In the terminal, build your Docker image using the following command:
docker build -t my-react-app .
This command tells Docker to build an image named my-react-app
using the instructions in the Dockerfile.
Now, let’s test our Docker container locally to ensure it works:
docker run -p 8080:80 my-react-app
This runs your image and maps port 80 of the container to port 8080 on your host. Navigate to http://localhost:8080 in your browser, and you should see your React app.
Next, we need to push our Docker image to Google Container Registry (GCR). First, make sure you are authenticated to Google Cloud by running:
gcloud auth login
Once authenticated, tag your Docker image for GCR. Replace <PROJECT-ID>
with your actual Google Cloud project ID:
docker tag my-react-app gcr.io/<PROJECT-ID>/my-react-app
Now, use the following command to push the image to the registry:
docker push gcr.io/<PROJECT-ID>/my-react-app
Now that our Docker image is in GCR, we can deploy it to Google Cloud Run:
gcr.io/<PROJECT-ID>/my-react-app
.Once deployed, you will receive a URL to access your React application. Visit the URL and enjoy your live React App running on Google Cloud Run!
By following these steps, you should have a solid understanding of deploying a React application using Docker and Google Cloud Run. Each step is crucial in ensuring that your application is not only functional but also scalable and accessible on the cloud. Keep exploring and happy coding!
24/08/2024 | ReactJS
14/09/2024 | ReactJS
24/08/2024 | ReactJS
14/09/2024 | ReactJS
14/09/2024 | ReactJS
24/08/2024 | ReactJS
28/11/2024 | ReactJS
14/09/2024 | ReactJS
16/07/2024 | ReactJS