Go Serverless: Part 2 Google Cloud Run
Tags: go, serverless, cloudrun, googlecloud
Published: 2019-04-12 17:01:05

---
title: Go Serverless: Part 2 Google Cloud Run
published: true
series: Go Serverless
tags: go, serverless, cloudrun, googlecloud
canonical_url: https://blog.merzlabs.com/posts/go-serverless-2/
---

# Running Serverless Functions in Containers

## What are Cloud Run and Knative?

Google Cloud Run was [recently announced at Google Cloud Next](https://cloud.google.com/blog/products/serverless/announcing-cloud-run-the-newest-member-of-our-serverless-compute-stack) after a private alpha period and we are finally able to talk about it. Cloud run basically allows us to deploy stateless HTTP(S) endpoints via containers without managing servers or Kubernetes clusters. Now we have a serverless platform for our Docker containers with pricing and deployment as easy as most serverless platforms. Everything is managed like scaling the container, routing traffic, custom domains and HTTPS certificates.

Cloud Run is built on Knative an open-source Kubernetes based project which gives confidence that our code/containers are still highly portable.

Check out [Googles Documentation](https://cloud.google.com/run/docs/concepts) for concepts behind Cloud Run and Knative.

## Building the Docker Container for Go

Cloud Run needs Docker images in our projects registry to deploy services and their revisions.

Like [part 1](https://blog.merzlabs.com/posts/go-serverless-1) we are using the project [Gistdirect](https://github.com/NiklasMerz/gistdirect) for trying Google Cloud Run. This function serves a simple redirect service.

 {% link niklasmerz/go-serverless-part-1-google-cloud-functions-2i35 %} 

 {% github niklasmerz/gistdirect %}

We need a Docker container to run our Go function. Gistdirect has a Dockerfile similar to this [Dockerfile](https://blog.merzlabs.com/posts/docker-go) with modifications to use [Go modules](https://github.com/golang/go/wiki/Modules), because Gistdirect is structured to use modules:

```
FROM golang:alpine as builder

ENV PATH /go/bin:/usr/local/go/bin:$PATH
ENV GOPATH /go

RUN apk --no-cache add gcc g++ make ca-certificates

COPY . /home/gistdirect

RUN set -x \
    && cd /home/gistdirect/main \
    && go build \
    && mv main /usr/bin/gistdirect \
    && rm -rf /go \
    && echo "Build complete."

FROM alpine:latest

RUN	apk add --no-cache \
    ca-certificates

COPY --from=builder /usr/bin/gistdirect /usr/bin/gistdirect

EXPOSE 8080
ENTRYPOINT ["gistdirect"]
CMD [""]
```

This Dockerfile builds the `main.go` file which uses the redirect handler in the `main()` function and makes it executable to the final Docker image we will run.

To prepare the Dockerfile for Cloud Run we need to have the `gcloud` CLI tool and must be signed in to a project with billing enabled (see [part 1](https://blog.merzlabs.com/posts/go-serverless-1)). If you followed through part 1 use the same project to reuse your domain. We can submit our project to Google Cloud Build which builds the image and puts it into the registry of our project.

```
gcloud builds submit --tag gcr.io/[PROJECTID]/gistdirect
```

## Deploying to Cloud Run

Creating the service is really easy with the Google Cloud Console. Just go to Cloud Run, “Create new Service”, Select the image, set the environment variable and we are good to go.

When the service is ready we get a URL like this: `gistdirect-khsgfjhsg-uc.a.run.app` and that’s our function.

## Get a nicer domain

If you followed through [part 1](https://blog.merzlabs.com/posts/go-serverless-1) you probably already set up and verified a domain to this project. You can go to the Cloud Run main page and select “Manage custom domain”. There you can use your previously added domain, set another custom domain and map it to your service. Once HTTPS is ready and you did the CNAME change in your DNS, you are ready to call your container.