<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prajwol KC</title>
    <description>The latest articles on DEV Community by Prajwol KC (@prajwolpkc).</description>
    <link>https://dev.to/prajwolpkc</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F638355%2F1c5b1dbf-9184-4af4-b5f1-658c982d082d.jpg</url>
      <title>DEV Community: Prajwol KC</title>
      <link>https://dev.to/prajwolpkc</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prajwolpkc"/>
    <language>en</language>
    <item>
      <title>Lets Deploy Dockerized Golang App to Google Cloud Run</title>
      <dc:creator>Prajwol KC</dc:creator>
      <pubDate>Wed, 27 Jul 2022 17:32:56 +0000</pubDate>
      <link>https://dev.to/prajwolpkc/lets-deploy-dockerized-golang-app-to-google-cloud-run-1ef2</link>
      <guid>https://dev.to/prajwolpkc/lets-deploy-dockerized-golang-app-to-google-cloud-run-1ef2</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;We will create a simple golang web app using fiber web framework which shows a simple hello world text on the screen.&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;main.go&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;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"))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Next, we create a Dockerfile on the root directory to actually dockerized the application. &lt;strong&gt;Dockerfile&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can run the app locally by building the container and running the following under the system cli. Here &lt;strong&gt;golangrun&lt;/strong&gt; is the name of the app&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;docker build --platform linux/amd64 -t golangrun .    &lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;docker run -p 8888:8080 golangrun      &lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we go to http://localhost:8888/ then we will see our Hello World. &lt;/p&gt;








&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Let's tag our container for Google Docker Registry with GCR name as &lt;strong&gt;readytoworkjapan&lt;/strong&gt; followed by app name&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;docker tag golangrun gcr.io/readytoworkjapan/golangrun&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now let's push our tagged container to GCR and can see it on our GCR portal&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;docker push  gcr.io/readytoworkjapan/golangrun   &lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--O3nSTHU_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.37.02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O3nSTHU_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.37.02.png" alt="" width="880" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hgHVxWUR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.38.08.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hgHVxWUR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.38.08.png" alt="" width="880" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9k2agZyQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.45.16.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9k2agZyQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.45.16.png" alt="" width="880" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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&lt;a href="%20https://golangrun-ok-24wnkzib7q-uc.a.run.app/"&gt; https://golangrun-ok-24wnkzib7q-uc.a.run.app/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LN_2rAgw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.48.26.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LN_2rAgw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://prajwol-kc.com.np/wp-content/uploads/2022/07/Screen-Shot-2022-07-27-at-22.48.26.png" alt="" width="880" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article is only the tip of the iceberg. There are many other sections and topics that can be modified and configured.&lt;/p&gt;

&lt;p&gt;If we want to change anything let's say change hello world to &lt;strong&gt;hello Golang&lt;/strong&gt;, then we just change the code&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Build the container -&amp;gt;&lt;code&gt; docker build --platform linux/amd64 -t golangrun .&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Tag the container with change -&amp;gt; &lt;code&gt;docker tag golangrun gcr.io/readytoworkjapan/golangrun&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Push the container -&amp;gt;&lt;code&gt; docker push gcr.io/readytoworkjapan/golangrun&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;ReDeploy on the same Cloud Run Service and see the changes with the same URL&lt;/li&gt;
&lt;/ol&gt;



&lt;p&gt;Note:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;docker build --platform linux/amd64 -t golangrun .&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;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.&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Go Application deployment on Google App engine flexible environment</title>
      <dc:creator>Prajwol KC</dc:creator>
      <pubDate>Wed, 26 May 2021 19:01:55 +0000</pubDate>
      <link>https://dev.to/prajwolpkc/go-application-deployment-on-google-app-engine-flexible-environment-1f2c</link>
      <guid>https://dev.to/prajwolpkc/go-application-deployment-on-google-app-engine-flexible-environment-1f2c</guid>
      <description>&lt;p&gt;Google Cloud App Engine is a Platform as a Service product that allows us to deploy apps written in various runtimes, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python&lt;/li&gt;
&lt;li&gt;PHP&lt;/li&gt;
&lt;li&gt;Java&lt;/li&gt;
&lt;li&gt;Ruby&lt;/li&gt;
&lt;li&gt;Node&lt;/li&gt;
&lt;li&gt;Go&lt;/li&gt;
&lt;li&gt;Ruby, etc&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;App Engine provides us with two options: the Standard Environment and the Flexible Environment.&lt;/p&gt;

&lt;p&gt;The Standard environment is a cloud-native option because it has a very fast spin-up engine that creates instances for our traffic in milliseconds.&lt;/p&gt;

&lt;p&gt;The Flexible environment, on the other hand, is different due to its container-based environment that relies on Compute Engine VMs. This is important for many use cases as we can customize our server as per our usage and requirement. This can be the installation of custom packages like ImageMagick, MuPDF libraries, or SSH into our server or run our own bash script, etc. We can use any type of runtime to run our app, and the important thing is that our app should respond in the default 8080 port unless we make some changes.&lt;/p&gt;

&lt;p&gt;Here is a detailed idea of the difference between them. &lt;a href="https://cloud.google.com/appengine/docs/the-appengine-environments"&gt;Here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we will be deploying a GO application on Flexible Environment on custom runtime rather than Google App Engine default go runtime as it brings up more concerns, unlike the standard one. We will be adding two files one app.yaml and other Docker file&lt;/p&gt;

&lt;p&gt;Lets a take simple GO application that uses GIN web framework to render Hello World to our web browser.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()
    r.GET("/", func(c *gin.Context) {
        c.String(200, "Hello World")
    })
    r.Run()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Time to show the world 🤠🤠 . Lets make this say through our App Engine Flexible environment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app.yaml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;runtime: custom
env: flex
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is pretty straightforward, as we are defining our custom image as runtime with a flexible environment. We can add other options for scaling purposes and port settings. Check out here&lt;/p&gt;

&lt;p&gt;Now, let create a Docker file that resides into the root directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Golang docker image
FROM golang:1.14

# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64

# install build essentials (optional)
RUN apt-get update &amp;amp;&amp;amp; \
    apt-get install -y wget build-essential pkg-config --no-install-recommends

# Move to working directory /build
WORKDIR /build

# Copy and download dependency using go mod
COPY go.mod .
COPY go.sum .
RUN go mod download

# Copy the code into the container
COPY . .
COPY . /dist

# Build the application
RUN go build -o main .

# Move to /dist directory as the place for resulting binary folder
WORKDIR /dist

# Copy binary from build to main folder
RUN cp /build/main .

# Export necessary port, default GCP App Engine Port
EXPOSE 8080

# Command to run when starting the container
CMD ["/dist/main"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The docker file will contain all the necessary configuration setup that we need to expose the application over 8080. Here 8080 is our default port for GAE.&lt;/p&gt;

&lt;p&gt;Also make sure go.mod and go.sum exists on the root directory so that necessary packages get installed on the deployment system&lt;/p&gt;

&lt;p&gt;Now from the gcloud CLI set project on which the application is going to be deployed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; gcloud app deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After successful deployment, there will be a URL to the application and we have our app successfully deployed on google app engine flexible environment using a custom runtime image.&lt;/p&gt;

&lt;p&gt;For more, if we want our application using GORM to connect over Google Cloud SQL, there is some changes to be done.&lt;/p&gt;

&lt;p&gt;First, Enable the Cloud SQL Admin API in the project&lt;br&gt;
Under app.yaml, we have to tell our application to allow secure connection or else the application can connect to database, make changes as following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;runtime: custom
env: flex
beta_settings:
  cloud_sql_instances: $INSTANCE_CONNECTION_NAME$
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;$INSTANCE_CONNECTION_NAME$ is the connection name of our cloud SQL that we can get from its instance detail.&lt;/p&gt;

&lt;p&gt;Like before deploy application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcloud app deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the flexible environment, there are many configurations that can be implemented to actually make our system like we want to make and behave as we intend to.&lt;/p&gt;

</description>
      <category>googlecloud</category>
      <category>go</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
