Go Quick Start
This document describes how to manually containerize a Go application from scratch and deploy it to Tencent Cloud's managed service (CloudBase Run).
Sample code:
https://github.com/TencentCloudBase/cloudbase-examples/tree/master/cloudbaserun/go
Or deploy to cloud hosting with one click:
Step 1: Write the Basic Application
Create a new directory named helloworld
and navigate into it:
mkdir helloworld
cd helloworld
Create a go.mod
file containing the following:
module helloworld
go 1.24
In the same directory, create a main.go
file and copy the following lines of code into it:
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", handler)
port := "80"
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello World!\n")
}
This code creates a basic Web server that listens on port 80
.
Step 2: Containerize the Application
In the project root directory, create a file named Dockerfile
with the following content:
# Use the official Golang image as the build environment
FROM golang:1.24-alpine as builder
# Set the Working Directory
WORKDIR /app
# Copy Source Code
COPY . .
RUN go mod download
# Build the Binary
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -ldflags="-s -w" -v -o server
# Select the base image for runtime (GO language selection principle: prefer small-sized images with basic linux content)
# https://hub.docker.com/_/alpine
FROM alpine:latest
# Installing Basic Tools and Time Zone Data
RUN apk add --no-cache tzdata ca-certificates && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
apk del tzdata
# Set the Working Directory
WORKDIR /app
# Copy the built binary into the image
COPY /app/server /app/server
EXPOSE 80
# Start Web Service
CMD ["/app/server"]
Add a .dockerignore
file to exclude files from the container image:
vendor/
Dockerfile
README.md
.dockerignore
.gcloudignore
.gitignore
Step 3 (Optional): Build and Run Locally
If you have Docker installed locally, you can run the following command to build the Docker image locally:
docker build -t helloworld-go .
After a successful build, run docker images
to view the built image.
REPOSITORY TAG IMAGE ID CREATED SIZE
helloworld-go latest 6948f1ebee94 8 seconds ago 15 MB
Then you can upload this image to your image repository.
Run the following command to start the container:
docker run -p 80:80 helloworld-python
Access http://localhost
, you should see the "Hello World!" output.
Step 4: Deploy to CloudBase Cloud Run
If you have already installed the CloudBase CLI, you can use the following command in the project directory to deploy the application to CloudBase Cloud Hosting:
tcb cloudrun deploy
After entering the environment and service names, the CLI will automatically package the application image and deploy it to Cloud Hosting. For more deployment methods, refer to Deploying Services.
For Go framework project samples, you can refer to: