Google Cloud Run for Go

Jaana B. Dogan
Apr 10 · 2 min read

Google Cloud recently launched a fully managed container execution environment called Cloud Run. It is an environment specifically for request-driven workloads. It provides autoscaling, scaling down to zero, pretty fast deployments, automatic HTTPS support, global names and more. Google Cloud Run doesn’t have language runtime restrictions as soon as the language runtime is supported on gVisor. It only requires the deployments to expose an HTTP server on a port.

In order to deploy a Go server, we will start with a minimal helloworld HTTP server. The container runtime is expecting the server to listen on $PORT:

$ cat main.go
// ...
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello world")
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(":"+port, nil))
}

In order to build a Docker image, create a multi-stage Dockerfile:

$ cat Dockerfile
FROM golang:1.12 as build
WORKDIR /go/src/app
COPY . .
ENV GO111MODULE on
RUN go build -v -o /app .
# Now copy it into our base image.
FROM gcr.io/distroless/base
COPY --from=build /app /app
CMD ["/app"]

Build and push the image to Google Container Registry:

$ docker build -t gcr.io/$GCP_PROJECT_NAME/helloworld .
$ docker push gcr.io/$GCP_PROJECT_NAME/helloworld

Then, either from the Cloud Console or terminal, you can create a new service with the built image:

$ gcloud beta run deploy hello --image gcr.io/$GCP_PROJECT_NAME/helloworld
# ...
Service [hello] revision [hello-00001] has been deployed and is serving traffic at https://hello-yfkfyh7piq-uc.a.run.app

You can use the provided endpoint to access to the service. See the Cloud Run console for logs and other operations.

Google Cloud Run is specifically optimized for request-driven HTTP/HTTPS workloads. The containers can be preempted and migrated, so it is not great if you are planning to use it for long running services. There is no support other than HTTP/HTTPS for now either. See also Cloud Run on GKE if you need more flexibility.

If you have any feedback, please feel free to reach out to jbd@google.com.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Jaana B. Dogan

Written by

Engineer at Google.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade