Google Cloud Deployment Guide
Learn how to deploy your Next.js applications to Google Cloud Platform using Cloud Run and Docker.
Set Up Google Cloud Account
Create a Google Cloud Platform account if you don't have one already. New users get $300 in free credits to spend on Google Cloud Platform services.
Visit cloud.google.com/Install Google Cloud SDK
Install the Google Cloud SDK to interact with Google Cloud services from your command line.
# For macOS (using Homebrew)
brew install --cask google-cloud-sdk
# For Windows (using installer)
# Download from https://cloud.google.com/sdk/docs/install
# For Debian/Ubuntu
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
sudo apt-get install apt-transport-https ca-certificates gnupg
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
sudo apt-get update && sudo apt-get install google-cloud-sdk
Initialize Google Cloud SDK
Initialize the Google Cloud SDK. This will authenticate your account and set up the default project and compute region.
gcloud init
Create a New Project
Create a new Google Cloud project. Replace PROJECT_ID with a unique ID and PROJECT_NAME with a descriptive name.
gcloud projects create PROJECT_ID --name="PROJECT_NAME"
Set the Active Project
Set the newly created project as the active project for subsequent commands.
gcloud config set project PROJECT_ID
Enable Required APIs
Enable the necessary Google Cloud APIs for deployment. This example enables Cloud Build, Cloud Run, and Artifact Registry.
gcloud services enable cloudbuild.googleapis.com
gcloud services enable run.googleapis.com
gcloud services enable artifactregistry.googleapis.com
Create a Dockerfile
Create a Dockerfile in the root of your project. This Dockerfile is optimized for Next.js applications.
# Dockerfile
FROM node:18-alpine AS base
# Install dependencies only when needed
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Copy package.json and install dependencies
COPY package.json package-lock.json* ./
RUN npm ci
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 8080
ENV PORT 8080
CMD ["node", "server.js"]
Update next.config.js
Update your next.config.js to use the 'standalone' output option, which creates a minimal server deployment package.
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
}
module.exports = nextConfig
Create a .dockerignore File
Create a .dockerignore file to exclude unnecessary files from the Docker build context.
.next
node_modules
npm-debug.log
README.md
.git
.env.local
.env.development.local
.env.test.local
.env.production.local
Create Artifact Registry Repository
Create a Docker repository in Artifact Registry to store your container images. Adjust the location as needed.
gcloud artifacts repositories create nextjs-repo --repository-format=docker --location=us-central1 --description="Docker repository for Next.js applications"
Build and Push Docker Image
Build your Docker image and push it to Artifact Registry. Replace PROJECT_ID with your Google Cloud project ID.
# Configure Docker to use Google Cloud credentials
gcloud auth configure-docker us-central1-docker.pkg.dev
# Build and tag the image
docker build -t us-central1-docker.pkg.dev/PROJECT_ID/nextjs-repo/nextjs-app:v1 .
# Push the image to Artifact Registry
docker push us-central1-docker.pkg.dev/PROJECT_ID/nextjs-repo/nextjs-app:v1
Deploy to Cloud Run
Deploy your container to Cloud Run. This creates a fully managed, auto-scaling service. Replace PROJECT_ID with your Google Cloud project ID.
gcloud run deploy nextjs-app \
--image=us-central1-docker.pkg.dev/PROJECT_ID/nextjs-repo/nextjs-app:v1 \
--platform=managed \
--region=us-central1 \
--allow-unauthenticated
Set Environment Variables (Optional)
Set environment variables for your Cloud Run service if needed.
gcloud run services update nextjs-app \
--set-env-vars="DATABASE_URL=your_database_url,API_KEY=your_api_key" \
--region=us-central1
Set Up Custom Domain (Optional)
To use a custom domain with your Cloud Run service, you need to verify domain ownership and map it to your service.
# First, verify your domain in Google Cloud Console
# Then, map the domain to your service
gcloud beta run domain-mappings create \
--service=nextjs-app \
--domain=www.yourdomain.com \
--region=us-central1
Set Up Continuous Deployment (Optional)
You can set up continuous deployment using Cloud Build. Create a cloudbuild.yaml file in your repository:
# cloudbuild.yaml
steps:
# Build the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'us-central1-docker.pkg.dev/$PROJECT_ID/nextjs-repo/nextjs-app:$COMMIT_SHA', '.']
# Push the container image to Artifact Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'us-central1-docker.pkg.dev/$PROJECT_ID/nextjs-repo/nextjs-app:$COMMIT_SHA']
# Deploy container image to Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args:
- 'run'
- 'deploy'
- 'nextjs-app'
- '--image=us-central1-docker.pkg.dev/$PROJECT_ID/nextjs-repo/nextjs-app:$COMMIT_SHA'
- '--region=us-central1'
- '--platform=managed'
- '--allow-unauthenticated'
images:
- 'us-central1-docker.pkg.dev/$PROJECT_ID/nextjs-repo/nextjs-app:$COMMIT_SHA'
Additional Resources
Google Cloud Documentation
Official Google Cloud documentation with comprehensive guides and API references.
Visit DocumentationCloud Run Documentation
Detailed documentation for Cloud Run, Google's serverless container platform.
Visit DocumentationNext.js Deployment
Official Next.js documentation on deploying to various platforms, including Google Cloud.
Visit DocumentationNeed Help with Google Cloud Deployment?
If you need assistance deploying your application to Google Cloud or have questions about implementation, I'm here to help.