How to deploy a container to Google Cloud Run
Containerize a Go app and deploy it to Google Cloud Run with autoscaling. Covers Dockerfile, Cloud Build, deployment, and concurrency tuning.
Google Cloud Run runs containers serverlessly. You provide an image that listens on a port; Cloud Run handles scaling, TLS, and request routing, billing only while requests are being served. It suits any language as long as it ships in a container.
This tutorial deploys a small Go web server to Cloud Run.
Prerequisites
- A Google Cloud project with billing enabled.
- The
gcloudCLI authenticated (gcloud auth login). - Docker installed locally.
Steps
1. Write the app
Create main.go that reads the PORT env var Cloud Run injects:
package main
import (
"fmt"; "net/http"; "os"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello from cloud run")
})
http.ListenAndServe(":"+os.Getenv("PORT"), nil)
}
2. Add a Dockerfile
FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /app
FROM gcr.io/distroless/static
COPY --from=build /app /app
ENTRYPOINT ["/app"]
3. Enable APIs
gcloud services enable run.googleapis.com artifactregistry.googleapis.com
4. Build and push
Let Cloud Build do the work, no local Docker push required:
gcloud builds submit --tag <region>-docker.pkg.dev/<project>/apps/hello
5. Deploy the service
gcloud run deploy hello \
--image <region>-docker.pkg.dev/<project>/apps/hello \
--region <region> --allow-unauthenticated
The command returns a public HTTPS URL.
6. Tune scaling
Set concurrency (requests per instance) and instance bounds:
gcloud run services update hello \
--concurrency 80 --min-instances 0 --max-instances 10
Minimum instances above zero reduces cold starts at a small idle cost.
Verification
curl $(gcloud run services describe hello --region <region> --format 'value(status.url)')
You should see the greeting. Check Cloud Run metrics for request count and instance utilization.
Next Steps
Lock down the service with IAM instead of --allow-unauthenticated, add a Cloud SQL connection, and wire continuous deployment from a Git repository.
Prerequisites
- Google Cloud project
- gcloud CLI installed
- Docker installed
Steps
- 1Write the app
- 2Add a Dockerfile
- 3Enable APIs
- 4Build and push
- 5Deploy the service
- 6Tune scaling