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.
We will create a simple golang web app using fiber web framework which shows a simple hello world text on the screen.
This is main.go
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"))
}
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.
Next, we create a Dockerfile on the root directory to actually dockerized the application. Dockerfile
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
We can run the app locally by building the container and running the following under the system cli. Here golangrun is the name of the app
docker build --platform linux/amd64 -t golangrun .
docker run -p 8888:8080 golangrun
If we go to http://localhost:8888/ then we will see our Hello World.
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.
Let's tag our container for Google Docker Registry with GCR name as readytoworkjapan followed by app name
docker tag golangrun gcr.io/readytoworkjapan/golangrun
Now let's push our tagged container to GCR and can see it on our GCR portal
docker push gcr.io/readytoworkjapan/golangrun
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.
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.
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 https://golangrun-ok-24wnkzib7q-uc.a.run.app/
This article is only the tip of the iceberg. There are many other sections and topics that can be modified and configured.
If we want to change anything let's say change hello world to hello Golang, then we just change the code
- Build the container ->
docker build --platform linux/amd64 -t golangrun .
- Tag the container with change ->
docker tag golangrun gcr.io/readytoworkjapan/golangrun
- Push the container ->
docker push gcr.io/readytoworkjapan/golangrun
- ReDeploy on the same Cloud Run Service and see the changes with the same URL
Note:
docker build --platform linux/amd64 -t golangrun .
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.
Top comments (0)