GCP-Docker-Golang: Deploying a Go Application to Google Cloud Container Registry and Cloud Run
In this guide, we’ll walk through the process of deploying a simple Go application to Google Cloud Platform (GCP). We’ll use Google Cloud Container Registry (GCR) to store our Docker image and Google Cloud Run to deploy and run the application.
It works well also eventhough your computer is arm64 that usually problem when deploying to GCP Linux that is using AMD64
Prerequisites
Before you begin, ensure you have the following set up:
- Google Cloud SDK: Install the Google Cloud SDK, which includes the
gcloud
command-line tool.
- Download and install instructions are available here.
2. Docker: Install Docker on your local machine.
- Download Docker Desktop for your operating system from here.
3. Git (optional): If you want to clone the sample code repository.
- Install Git from here.
Step 1: Clone the Sample Application
If you haven’t already, clone the sample Go application repository from GitHub:
git clone https://github.com/novrian6/go-simpleapp.git
cd go-simpleapp
This repository contains a simple Go application (main.go
) using the Gin web framework and a Dockerfile (Dockerfile
) for building the application into a Docker image.
Step 2: Review the Application Code
Here’s the main.go
code that defines a simple web server with various routes:
package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
r.POST("/submit", func(c *gin.Context) {
c.String(http.StatusOK, "Submission successful")
})
r.GET("/user/:name", func(c *gin.Context) {
name := c.Param("name")
c.String(http.StatusOK, fmt.Sprintf("Hello, %s", name))
})
r.GET("/welcome", func(c *gin.Context) {
firstName := c.Query("first_name")
lastName := c.Query("last_name")
c.String(http.StatusOK, fmt.Sprintf("Welcome, %s %s", firstName, lastName))
})
r.POST("/form_post", func(c *gin.Context) {
message := c.PostForm("message")
c.String(http.StatusOK, fmt.Sprintf("Received message: %s", message))
})
r.POST("/upload", func(c *gin.Context) {
file, _ := c.FormFile("file")
c.SaveUploadedFile(file, file.Filename)
c.String(http.StatusOK, fmt.Sprintf("'%s' uploaded!", file.Filename))
})
r.Run(":8080")
}
Step 3: Review the Dockerfile
Here’s the Dockerfile
used to build the application:
# Use the official Golang image to create a build artifact.
# Use a specific Go version as needed.
FROM golang:1.21.3 AS builder
# Set the Current Working Directory inside the container
WORKDIR /app
# Copy go mod and sum files
COPY go.mod go.sum ./
# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download
# Copy the source from the current directory to the Working Directory inside the container
COPY . .
# Build the Go app for Linux OS and amd64 architecture
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o myapp .
# Start a new stage from scratch
FROM alpine:latest
# Set up certificates
RUN apk --no-cache add ca-certificates
# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/myapp /app/myapp
# Expose port 8080 to the outside world
EXPOSE 8080
# Command to run the executable
CMD ["/app/myapp"]
Step 4: Authenticate with gcloud
First, authenticate gcloud
with your Google Cloud account:
gcloud auth login
Follow the prompts in your web browser to authenticate and authorize gcloud
to access your Google Cloud account. This step is necessary to perform actions on Google Cloud Platform.
Enter your new google cloud account above.
Note: As I am using cloudguru, If you are use Cloudguru you can find the information as follows. User the relevant value from your cloud guru.
Configure the project. Replace the project id custom-images-251316 with your own project id found in GCP Console.
gcloud config set project playground-s-11-90e0072a
Step 5: Configure Docker Authentication for GCR
Configure Docker to use gcloud
as a credential helper for Google Container Registry (GCR):
gcloud auth configure-docker
This command configures Docker to use gcloud
as the credential helper, allowing Docker to push and pull images from GCR using your Google Cloud credentials.
Step 5: Build and Push the Docker Image to GCR
Visit Google Cloud Registry from the Google Cloud Console Web
https://console.cloud.google.com/welcome?project=direct-outlet-299902
Enable the API as follows to use Google Artifact Registry.
Now, let’s build the Docker image and push it to Google Artifact Registry (GCR):
Note: for the project id used in the below command line, such as playground-s-11–90e0072a , please adjust it accordingly with your project id found on GCP Console
# Authenticate Docker with GCR
gcloud auth configure-docker
# Build the Docker image
docker build -t gcr.io/playground-s-11-90e0072a/mygoapp:latest .
Check the image has been created using this command
docker images
# Push the Docker image to GCR
docker push gcr.io/playground-s-11-90e0072a/mygoapp:latest
Trouble shoot:Setting the Active Account
If you encounter authentication issues during Docker image push, ensure that the active gcloud
account is correct and has sufficient permissions. Here's how to verify and set the active account:
Check Active Account
Run the following command to list active gcloud
accounts:
gcloud auth list
Set Active Account
To set the active account, use the following command.
Replace student638282350@whizlabs.in
with the Google account or service account email that has the necessary permissions to push Docker images to GCR.
gcloud config set account student638282350@whizlabs.in
Step 5: Deploy the Application to Google Cloud Run
Deploy the Docker image to Google Cloud Run:
gcloud run deploy my-go-app \
--image gcr.io/playground-s-11-90e0072a/mygoapp:latest \
--platform managed \
--region us-central1 \
--allow-unauthenticated
if you are asked to enable API as above, answer yes.
Follow the prompts to select your Google Cloud project (playground-s-11-53304458
in this case) and allow unauthenticated invocations if you want the service to be publicly accessible.
Step 6: Access the Deployed Application
Once deployment is successful, the command will output a URL where your application is accessible. Open this URL in your web browser to interact with your Go application deployed on Google Cloud Run.
On above case visit : https://my-go-app-e5ecwjnycq-uc.a.run.app/welcome
It will shows:
In this article, we’ve covered how to deploy a Go application to Google Cloud Platform using Google Container Registry and Cloud Run. We started by cloning a sample application repository, reviewed the code and Dockerfile, built and pushed the Docker image to GCR, deployed the application to Cloud Run, and finally accessed the deployed application through a web browser.
For more information on advanced configurations or troubleshooting, refer to the Google Cloud Run documentation.