Gin
Gin is a high-performance Go Web framework known for its simplicity, efficiency, and ease of use. Built upon httprouter for fast route matching, it supports middleware mechanisms and enables effortless construction of RESTful APIs or microservices.
This guide describes how to deploy the Gin application on Tencent CloudBase through multiple approaches:
Create a Gin application
Note: If you already have a Gin application and have ensured that both the Dockerfile and main.go files are in the root directory, you can skip this step.
To create a new Gin application, first ensure that the Go runtime is installed on your machine.
Create a directory `cloudrun-gin, then cd into it.
Run the following commands in the cloudrun-gin directory:
go mod init cloudrun-gin
go get -u github.com/gin-gonic/gin
In the cloudrun-gin directory, create a main.go file with the following content:
func main() {
router := gin.Default()
router.GET("/json", func(c *gin.Context) {
data := map[string]interface{}{
"id": 0,
"name": "zhangsan",
}
c.JSON(http.StatusOK, data)
})
// Listen and serve on 0.0.0.0:8080
router.Run(":8080")
}
Execute go run main.go to start the service, then access http://localhost:8080/json to view the response.
Configuring Dockerfile
- Create a
Dockerfilefile in thecloudrun-gindirectory with the following content (modify the golang version in the FROM line according to your actual situation):
FROM golang:1.22.3-alpine as builder
# Specify the working directory during the build process
WORKDIR /app
# Copy all files from the current directory (where the dockerfile is located) to the working directory (excluding files specified in .dockerignore)
COPY . /app/
# Execute the build command. Set the operating system parameter to linux, name the compiled binary executable as main, and save it in the current directory.
RUN GOOS=linux go build -o main .
# Select the base image for the runtime environment (GO language selection principle: prioritize base images with a small footprint and containing basic linux components)
FROM alpine:latest
# The container's default time zone is UTC. We use the following time zone setting command to enable Shanghai time zone.
# RUN apk add tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo Asia/Shanghai > /etc/timezone
# Using the HTTPS Protocol to Access Container Cloud for Certificate Installation Invocation
RUN apk add ca-certificates
# Specify the working directory at runtime
WORKDIR /app
# Copy the build artifact /app/main to the working directory at runtime
COPY /app/main /app/
# Execute the startup command.
# Writing multiple independent CMD commands is incorrect! Only the last CMD command will be executed, and all previous ones will be ignored, causing application errors.
# Refer to [Docker official documentation on CMD command](https://docs.docker.com/engine/reference/builder/#cmd)
CMD ["/app/main"]
With the above changes, your gin application will be deployable to Tencent Cloud Hosting.
Deployment to Cloud Hosting
Cloud Hosting provides multiple deployment methods to deploy your application:
Deploy via Console
Open Tencent CloudBase, click Deploy via local code -> enter the service name -> select Upload code package for the deployment method -> select Folder for the code package type -> choose the cloudrun-gin directory for upload -> enter 8080 for the port -> click Create and wait for the creation to complete.
Deploying via cli
If you have installed CloudBase CLI, you can use the following command in the project directory to deploy the application to CloudBase Cloud Hosting:
tcb cloudrun deploy -p 8080
After entering the environment and service names, the CLI will automatically package the application image and deploy it to Cloud Hosting.
Besides manual deployment, you can also install the aforementioned applications with one click: