Lets Deploy Dockerized Golang App to Google Cloud Run
Published: 2022-07-27 17:32:56

<!-- wp:paragraph -->
<p>In this article, we will create a simple golang webapp using microframework fiber. Then we will run in our local system using the docker container. Then after is done on our local system, we will deploy it to Google Cloud Platform Google Container Registry (GCR). Then we will connect the GCR container to be deployed to Google Cloud Run. So let's begin.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We will create a simple golang web app using fiber web framework which shows a simple hello world text on the screen.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>This is <strong>main.go</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>package main

import (
	"log"
	"github.com/gofiber/fiber/v2"
)

func main() {
	app := fiber.New()
	app.Get("/", func(c *fiber.Ctx) error {
		return c.SendString("Hello, World!")
	})
	log.Fatal(app.Listen(":8080"))
}
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Here we just imported fiber web framework which is basically a nestjs like a framework under golang. We are using a GET method to get all the incoming requests under root and show a hello world text. We are listening to the container to port 8080.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Next, we create a Dockerfile on the root directory to actually dockerized the application. <strong>Dockerfile</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>FROM golang:1.16-alpine

WORKDIR /app

COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . .

RUN go build -o ./out/dist .

CMD ./out/dist
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>We can run the app locally by building the container and running the following under the system cli. Here <strong>golangrun</strong> is the name of the app</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>docker build --platform linux/amd64 -t golangrun .    </code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>docker run -p 8888:8080 golangrun      </code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>If we go to http://localhost:8888/ then we will see our Hello World. </p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<!-- /wp:separator -->

<!-- wp:paragraph -->
<p>Now next step is to deploy the same web app to GCR and run it on Cloud Run. To init the process the container has to be tagged and push to GCR.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's tag our container for Google Docker Registry with GCR name as <strong>readytoworkjapan</strong> followed by app name</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>docker tag golangrun gcr.io/readytoworkjapan/golangrun</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Now let's push our tagged container to GCR and can see it on our GCR portal</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>docker push  gcr.io/readytoworkjapan/golangrun   </code></pre>
<!-- /wp:code -->

<!-- wp:image {"id":830,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.37.02.png" alt="" class="wp-image-830"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The container is in my GCR, We can deploy the container to our preferred Cloud Run, GKE, or GCE. For our use we will deploy to Cloud Run.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":832,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.38.08.png" alt="" class="wp-image-832"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Create Cloud Run Service, by selecting the latest container tag which was just pushed. Name the service name with general selections. The ingress can be for now all incoming traffic and authentication can be unauthenticated invocations. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":835,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.45.16.png" alt="" class="wp-image-835"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After the cloud run service is created, we can see all the details with logs. Just open the URL that is given in the top section. When we open it voila we just get the dockerized golang run running on Cloud Run. The URL, in this case, is<a href=" https://golangrun-ok-24wnkzib7q-uc.a.run.app/"> https://golangrun-ok-24wnkzib7q-uc.a.run.app/</a></p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":837,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full"><img src="https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.48.26.png" alt="" class="wp-image-837"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>This article is only the tip of the iceberg. There are many other sections and topics that can be modified and configured.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If we want to change anything let's say change hello world to <strong>hello Golang</strong>, then we just change the code</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true,"fontSize":"medium"} -->
<ol class="has-medium-font-size"><li>Build the container -&gt;<code> docker build --platform linux/amd64 -t golangrun .</code><br></li><li>Tag the container with change -&gt; <code>docker tag golangrun gcr.io/readytoworkjapan/golangrun</code><br></li><li>Push the container -&gt;<code> docker push gcr.io/readytoworkjapan/golangrun</code><br></li><li>ReDeploy on the same Cloud Run Service and see the changes with the same URL</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Note:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>docker build --platform linux/amd64 -t golangrun .</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p><em>For this use case, we are using mac m1 so the container build is expected to run on linux/amd64 rather than arm64 which is the default case under mac m1.</em></p>
<!-- /wp:paragraph -->