Google Cloud Run for Go

Jaana Dogan
Apr 10, 2019 · 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.14 as build
WORKDIR /go/src/app
COPY . .
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 run deploy hello --image gcr.io/$GCP_PROJECT_NAME/helloworld --project $GCP_PROJECT_NAME
# ...
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.

Image for post
Image for post

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 - Community

Google Cloud community articles and blogs

Jaana Dogan

Written by

See rakyll.org for more.

Google Cloud - Community

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

Jaana Dogan

Written by

See rakyll.org for more.

Google Cloud - Community

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

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store