Google Cloud Run for Go

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.
COPY --from=build /app /app
CMD ["/app"]

Build and push the image to Google Container Registry:

$ docker build -t$GCP_PROJECT_NAME/helloworld .
$ docker push$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$GCP_PROJECT_NAME/helloworld --project $GCP_PROJECT_NAME
# ...
Service [hello] revision [hello-00001] has been deployed and is serving traffic at

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



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