Software Studio
Back to Articles

Deploying Next.js to Google Cloud Run

Next.jsGCPDevOps
Cover illustration for Deploying Next.js to Google Cloud Run

Google Cloud Run is an excellent platform for hosting Next.js applications. It offers automatic scaling, per-request billing, and a generous free tier. You hand it a container and it handles the rest.

Getting a Next.js app into that container properly takes a few specific steps. Here is the path we follow, including the gotchas that tend to cost people an afternoon.

Step 1: Build a standalone Docker image

The first step is creating a proper Dockerfile. Next.js provides a standalone output mode that creates a minimal production bundle containing only the files the server needs. Turn it on in your config:

// next.config.ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  output: "standalone",
};

export default nextConfig;

Your Dockerfile should then use a multi-stage build: one stage for installing dependencies and building, and a final stage with only the standalone output.

FROM node:24-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:24-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
EXPOSE 8080
CMD ["node", "server.js"]

The final image has no build tools and no development dependencies, so it is small and starts quickly. That matters on Cloud Run, where a cold start includes pulling and booting your container. We go deeper on image hygiene in Docker for Production: Beyond the Basics.

Step 2: Handle environment variables correctly

Environment variables need careful handling, because Next.js reads them at two different times.

  • Build-time variables, like the ones prefixed with NEXT_PUBLIC_, are baked into the JavaScript bundle. They must be available during the Docker build.
  • Runtime variables, like a database URL or an API key, are read by the server when it runs. They should be injected through Cloud Run's environment configuration.

Mixing these up is the most common source of "it works locally but not in production." If a NEXT_PUBLIC_ value is missing at build time, setting it on the Cloud Run service later will not help, because the bundle was already built without it.

Keep secrets out of the image entirely. Anything sensitive belongs in Cloud Run's configuration or Secret Manager, never in a build argument.

Step 3: Listen on the PORT variable

One gotcha: Cloud Run containers must listen on the PORT environment variable, which Cloud Run sets automatically. Make sure your Next.js start script respects this.

The standalone server.js reads PORT on its own, so the Dockerfile above works as written. If you start the app another way, check that nothing hardcodes port 3000. A container that listens on the wrong port fails its startup check, and the deploy reports that the container failed to start with little other explanation.

Step 4: Deploy the service

With the image built and pushed, deploying is one command:

gcloud run deploy my-app \
  --image gcr.io/my-project/my-app \
  --region us-central1 \
  --allow-unauthenticated

Cloud Run creates a new revision and shifts traffic to it once it is healthy. If something goes wrong, the previous revision is still there and you can route traffic back to it.

Step 5: Add a custom domain

For custom domains, you'll configure Cloud Run's domain mapping feature. You point your DNS records at the values Google gives you, and it handles SSL certificates automatically through Google-managed certificates. There is nothing to renew and no certificate files to manage.

Step 6: Automate deployment with Cloud Build

We also recommend setting up Cloud Build triggers for continuous deployment. A simple cloudbuild.yaml can build your container and deploy it automatically on every push to main.

steps:
  - name: gcr.io/cloud-builders/docker
    args: ["build", "-t", "gcr.io/$PROJECT_ID/my-app", "."]
  - name: gcr.io/cloud-builders/docker
    args: ["push", "gcr.io/$PROJECT_ID/my-app"]
  - name: gcr.io/google.com/cloudsdktool/cloud-sdk
    entrypoint: gcloud
    args: ["run", "deploy", "my-app",
           "--image", "gcr.io/$PROJECT_ID/my-app",
           "--region", "us-central1"]

Once that is in place, shipping a change means merging a pull request.

A short checklist

  • Standalone output enabled in the Next.js config.
  • Multi-stage Dockerfile with only the standalone output in the final stage.
  • Build-time variables available during the build, runtime variables set on the service.
  • The server listens on PORT.
  • Domain mapping configured for your custom domain.
  • A Cloud Build trigger deploying on every push to main.

If your app talks to a database, read Prisma Patterns for Next.js Applications next. Connection handling is the other thing that changes when you move to a platform that scales instances up and down.