How to deploy a MERN app on Google Cloud with a custom domain and Which is best way to deploy App Engine or cloud run I have already created account GCP
Hello @Vikramjeet11
MERN stack deployment involves deploying a MongoDB, Express.js, React, and Node.js application to a cloud platform. This typically involves setting up a server, configuring databases, and managing the application’s lifecycle.
Deploying a MERN (MongoDB, Express, React, Node.js) app on Google Cloud Platform (GCP) with a custom domain involves several steps. Below is a systematic guide to help you through the process, including considerations for using App Engine or Cloud Run.
Step 1: Prepare Your MERN App
- Ensure your MERN app is ready for deployment. This includes having a package.json file for Node.js dependencies and a build version of your React app.
Step 2: Set Up Google Cloud Project
- Log in to your GCP account.
- Create a new project or select an existing one.
- Enable the necessary APIs: Cloud Build, Cloud Run, and App Engine.
Step 3: Choose Deployment Method
- App Engine: Best for apps that need automatic scaling and are relatively simple to deploy.
- Cloud Run: Ideal for containerized applications and offers more flexibility and control over the environment.
Step 4: Containerize Your Application (for Cloud Run)
-
Create a Dockerfile in your project root:
# Use Node.js base image FROM node:14 # Set working directory WORKDIR /app # Copy package.json and install dependencies COPY package*.json ./ RUN npm install # Copy the rest of the application code COPY . . # Build the React app RUN npm run build # Start the server CMD ["npm", "start"] -
Build the Docker image:
docker build -t gcr.io/[PROJECT-ID]/mern-app .
Step 5: Deploy to Cloud Run
-
Push the Docker image to Google Container Registry:
docker push gcr.io/[PROJECT-ID]/mern-app -
Deploy the image to Cloud Run:
gcloud run deploy mern-app --image gcr.io/[PROJECT-ID]/mern-app --platform managed --region [REGION]
Step 6: Deploy to App Engine (Alternative)
-
Create an app.yaml file in your project root:
runtime: nodejs14 env: standard -
Deploy the app:
gcloud app deploy
Step 7: Set Up Custom Domain
- Go to the GCP Console and navigate to “App Engine” or “Cloud Run” settings.
- Under “Custom Domains”, add your domain and verify ownership.
- Update your domain’s DNS settings to point to the provided IP addresses.
Step 8: Configure Environment Variables
- Use the GCP Console to set environment variables needed by your app, such as database URIs.
Final Answer
- Best Deployment Method: Use Cloud Run for more flexibility and control, especially if you need to run containerized applications. Use App Engine for simpler, auto-scaling deployments.
- Custom Domain: Set up through GCP Console and update DNS settings.
What a great writeup, learn2skills!
One thing I’d add: As a product manager on the Serverless team which ships both Cloud Run and App Engine, I’d recommend going with Cloud Run rather than App Engine unless you have a strong reason to choose App Engine. Cloud Run is a much newer product with better portability and interoperability, so it’s a better choice for a new application. And you can delpoy from source:
gcloud run deploy --source .
Cloud Run works with Cloud Build in the background to build your image, so you never have to touch a container if you don’t want to.
what is the process for creating virtual instance and if don’t have github repo is there any other option to push code and I have separate folder for react and node in same project so I have to create a individual yml file for that or not neccesssary