Deploy golang service to Google Cloud

We will be using Google Cloud Run with the help of a Dockerfile. First, let’s say you have an application like this (main.go file below)

// Sample run-helloworld is a minimal Cloud Run service.
package main

import (

func main() {
	log.Print("starting server...")
	http.HandleFunc("/", handler)

	// Determine port for HTTP service.
	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
		log.Printf("defaulting to port %s", port)

	// Start HTTP server.
	log.Printf("listening on port %s", port)
	if err := http.ListenAndServe(":"+port, nil); err != nil {

func handler(w http.ResponseWriter, r *http.Request) {
	name := os.Getenv("NAME")
	if name == "" {
		name = "World"
	fmt.Fprintf(w, "Hello %s!\n", name)

Then you’ll need to create a Dockerfile in your directory (same as main.go):

FROM golang:1.19 as build
WORKDIR /go/src/app
COPY . .
RUN go build -v -o app .

COPY --from=build /go/src/app/. /
CMD ["/app"]

Then we can run this command (with Google Cloud SDK installed). myservice is the new name of the image I give here.

gcloud builds submit --tag

All building from the Dockerfile is built from Cloud Build. You can check how it is going at the Cloud Build page under history. We now have an image we can publish. Run the following command to deploy the image:

gcloud run deploy myservice --image

Then we are done!

0 0 votes
Article rating
Notify of

Inline Feedbacks
View all comments