Software Studio
Back to Articles

Deploying Next.js to Google Cloud Run

Next.jsGCPDevOps

Google Cloud Run is an excellent platform for hosting Next.js applications. It offers automatic scaling, per-request billing, and a generous free tier.

The first step is creating a proper Dockerfile. Next.js provides a standalone output mode that creates a minimal production bundle. Your Dockerfile should use a multi-stage build: one stage for installing dependencies and building, and a final stage with only the standalone output.

Environment variables need careful handling. Build-time variables (like NEXT_PUBLIC_ prefixed ones) must be available during the Docker build. Runtime variables should be injected via Cloud Run's environment configuration.

For custom domains, you'll configure Cloud Run's domain mapping feature. This handles SSL certificates automatically through Google-managed certificates.

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.

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.