Next.js has become a popular choice for building modern web applications, thanks to its powerful features and excellent developer experience. When it comes to deploying your Next.js app, Google Cloud Run offers a scalable and cost-effective solution. In this guide, we'll walk through the process of deploying a Next.js application on Google Cloud Run, step by step.
Before we begin, make sure you have the following:
First, let's create a new project in Google Cloud:
Once your project is created, make sure to note down the Project ID, as we'll need it later.
Enable the necessary APIs for your project:
To deploy your Next.js app on Cloud Run, we need to make a few adjustments:
package.json
file to include a start script:{ "scripts": { "dev": "next dev", "build": "next build", "start": "next start -p $PORT" } }
.dockerignore
file in your project root to exclude unnecessary files:Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
.git
Dockerfile
in your project root:# Use the official Node.js 14 image as a parent image FROM node:14-alpine # Set the working directory WORKDIR /app # Copy package.json and package-lock.json COPY package*.json ./ # Install dependencies RUN npm install # Copy the rest of your app's source code COPY . . # Build your Next.js app RUN npm run build # Expose the port the app runs on EXPOSE 3000 # Start the app CMD ["npm", "start"]
Now, let's build and push your Docker image to Google Container Registry:
gcloud auth configure-docker
docker build -t gcr.io/[PROJECT-ID]/nextjs-app .
Replace [PROJECT-ID]
with your Google Cloud Project ID.
docker push gcr.io/[PROJECT-ID]/nextjs-app
With your image pushed to the Container Registry, you can now deploy it to Cloud Run:
gcloud run deploy nextjs-app \ --image gcr.io/[PROJECT-ID]/nextjs-app \ --platform managed \ --region us-central1 \ --allow-unauthenticated
To automate future deployments, let's set up Cloud Build:
cloudbuild.yaml
file in your project root:steps: # Build the container image - name: 'gcr.io/cloud-builders/docker' args: ['build', '-t', 'gcr.io/$PROJECT_ID/nextjs-app', '.'] # Push the container image to Container Registry - name: 'gcr.io/cloud-builders/docker' args: ['push', 'gcr.io/$PROJECT_ID/nextjs-app'] # Deploy container image to Cloud Run - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk' entrypoint: gcloud args: - 'run' - 'deploy' - 'nextjs-app' - '--image' - 'gcr.io/$PROJECT_ID/nextjs-app' - '--region' - 'us-central1' - '--platform' - 'managed' - '--allow-unauthenticated' images: - 'gcr.io/$PROJECT_ID/nextjs-app'
cloudbuild.yaml
Now, every time you push to your specified branch, Cloud Build will automatically build and deploy your Next.js app to Cloud Run.
You've successfully deployed your Next.js application on Google Cloud Run! This serverless platform allows your app to scale automatically based on traffic, and you only pay for the resources you use. With the continuous deployment setup, you can focus on developing your app while Cloud Build takes care of the deployment process.
Remember to monitor your app's performance and costs in the Google Cloud Console, and make adjustments as needed to optimize your deployment.
08/09/2024 | Next.js
02/10/2024 | Next.js
12/10/2024 | Next.js
02/10/2024 | Next.js
12/10/2024 | Next.js
08/10/2024 | Next.js