Skip to main content

Go Quick Start

This document describes how to manually containerize a Go application from scratch and deploy it to Tencent 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 a Basic Application

Create a new directory named helloworld and change to this directory:

mkdir helloworld
cd helloworld

Create a go.mod file containing the following content:

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 will create a basic Web server listening on port 80.

Step 2: Containerize the Application

Create a file named Dockerfile in the project root directory with the following content:

# Use the official Golang image as the building environment.
FROM golang:1.24-alpine as builder

# Set Working Directory
WORKDIR /app

# Copy the source code
COPY . .

RUN go mod download


# Build binary
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -ldflags="-s -w" -v -o server

# Select the base image for the runtime environment (GO language selection principle: prioritize base images with a small footprint and containing basic linux components)
# https://hub.docker.com/_/alpine
FROM alpine:latest

# Install basic tools and timezone 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 Working Directory
WORKDIR /app

# Copy the built binary into the image
COPY --from=builder /app/server /app/server

EXPOSE 80

# Start the 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): Local Build and Run

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

You can then 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 Hosting

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

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 example Go framework projects, refer to: