🔧 NoOps Go on Cloud Run

“There must be a standard way to package a Go container”

peto-smallPeter MalinaCTO | FlowUp

noops-go-cr-main

Every once in a while, there’s this simple thing (which then becomes monstrous and completely consumes 2 days of my work until I successfully abandon it) I really need to test. However, setting up the infrastructure is giving me headaches. Especially when googling “golang docker build” for the 100th time gives me my own article. This article shows, that building and deploying applications can be much easier than remembering Dockerfiles.

The repository for this article can be found on Github.

🎁 Building Golang Images Without a Dockerfile

“There must be a standard way to package a Go container”, I thought once. Indeed, there is, and provided directly by Google. Google ko is a lightweight tool that helps package, publish, and apply Go container images in a standard way.

Before we go any further, if you want to follow this guide:

The application we are going to build is a simple “Hello World” server using the Echo web framework. Here it goes:

// cmd/server/main.go package main import ( "github.com/labstack/echo" "log" "net/http" "os" ) func main() { // Cloud Run sets this variable, 8080 will be used for localhost port := os.Getenv("PORT") if port == "" { port = "8080" } // listen on the port provided by the environment err := http.ListenAndServe(":"+port, EchoHandler()) if err != nil && err != http.ErrServerClosed { log.Fatalln("An error occurred starting: ", err) } } func EchoHandler() http.Handler { e := echo.New() e.GET("/", func(c echo.Context) error { return c.String(http.StatusOK, "Hello World!") }) return e.Server.Handler }


Now let’s build and publish an image of this server with koThe tool will automatically build the binary and pack it in a minimal Docker image. Moreover, it will automatically push the image to the provided registry:

KO_DOCKER_REPO=<your-docker-repo> ko publish ./cmd/server

You should be seeing something like this in your output:

2020/08/19 17:47:45 Using base gcr.io/distroless/static:latest for github.com/petomalina/cloudrun-ko-example/cmd/server 2020/08/19 17:47:47 Building github.com/petomalina/cloudrun-ko-example/cmd/server 2020/08/19 17:47:48 Publishing europe-west3-docker.pkg.dev/petermalina/test/server-58a47f33e8f136c309da82ea42842b7d:latest 2020/08/19 17:47:50 Published europe-west3-docker.pkg.dev/petermalina/test/server-58a47f33e8f136c309da82ea42842b7d@...

🚀 Launching the Application on Cloud Run

Now that we have our image in a Container Registry or Artifact Registry, Cloud Run can pull them without problems. We will be calling the gcloud SDK in the next step to create/update a Cloud Run instance:

gcloud run deploy cloudrun-ko-example \ --image=$APP_IMAGE \ --region=europe-west1 \ --platform=managed \ --allow-unauthenticated

Note that in this example, cloudrun-ko-example is the name of the service I am deploying, and <app-image> needs to be replaced by the image you built in the previous step.

The resulting deployment should look something like this on your GCP:

If you’ve followed, you have just deployed your Cloud Run service without knowing nearly anything about Docker. 🎉

🚢 One-Click Build/Publish/Deploy

In case 2 commands still look like too many, you can further automate the provided commands by passing the published image right into the Cloud Run deployment command:

# deploy.sh # ko displays the published image as the last line of it's script APP_IMAGE=$(KO_DOCKER_REPO=<repo> ko publish ./cmd/server | tail -1) # we can now pass it to the gcloud run deploy gcloud run deploy cloudrun-ko-example \ --image=$APP_IMAGE \ --region=europe-west1 \ --platform=managed \ --allow-unauthenticated

Last Words

I hope you enjoyed this guide. Any questions or ideas, please hit me up on my Twitter @petomalina.

Cheers,

Peter

Can you feel the flow?

Drop us a note

hello@flowup.cz

Contact us using this form and we'll answer you via email ASAP. If you leave us your number in the message, we'll call you back. Looking forward to hearing from you!

We're based in Brno

Kopečná 980/43

Brno

602 00

Czech Republic

map