@duongital

Deploy a NextJS application to Google Cloud Run

NextJS brings a number of powerful features building on top of ReactJS, allowing us to implement full-stack applications using React Server Components. However, deploying a NextJS app can be challenging, and I typically choose Vercel or Netlify providers to simplify the deployment process.

I recently joined an interesting project where the DevOps team manually deploys applications to Google Cloud Run (GCR), and I wanted to try this solution from scratch. Let’s walk through the entire setup process! 😄

flowchart TD
    A[Client Browser] -->|HTTPS Request| B[Custom Domain via DNS]
    B -->|Points to Static IP| C[VPC Network Static IP]
    C --> D[Network Services Load Balancer]
    D -->|Routes Traffic| E[Google Cloud Run Service]

    F[GitHub Repository] -->|Push to main| G[Cloud Build]
    G -->|Builds & Deploys Docker Image| E

    E -->|Generates| H[Default GCR URL]

    style E fill:#4285f4,color:#fff
    style G fill:#34a853,color:#fff
    style D fill:#fbbc04,color:#000
    style C fill:#ea4335,color:#fff

Here are the Google Cloud services we’ll be using:

OK, let’s go!

1. Google Cloud Run + Cloud Build

After finishing your NextJS application implementation, you’ll need to create a Dockerfile. Here’s a reference for a simple Docker configuration:

Example Dockerfile: Next.js with Docker

Next, visit the Google Cloud Run dashboard and create a new service. Choose the “Continuously deploy from a repository” option to integrate with your GitHub repository. This enables Cloud Build to automatically deploy your application on every push to the main branch.

create a GCR service

After successfully deploying your Docker container, Google Cloud Run will generate a URL similar to this: demo link. You can use this URL directly if your service is publicly accessible.

Note: While there is currently a beta feature to map a custom domain directly in Cloud Run, it’s not recommended for production use. For a robust solution, we’ll use VPC Networks and Network Services instead.

2. VPC Network + Network Services

These two services work together to map your custom domain to the URL that Google Cloud Run generated.

First, we’ll create a static IP address using the VPC Network service:

create a static IP address

Next, configure your DNS settings to point to this static IP. Here’s an example using Cloudflare:

DNS config

The final step is to create a load balancer that connects the static IP to your Cloud Run application using Network Services:

load balancer

After completing this configuration, you can visit your custom domain and it will route directly to your Google Cloud Run service. Since Cloud Run is serverless, you don’t need to worry about scaling or managing infrastructure.

Cost consideration: While Cloud Run itself is cost-effective with a generous free tier, using VPC Network and Load Balancer services adds approximately $20/month to your bill. This might be expensive for hobby projects or small-scale applications.

Conclusion