Skip to main content

Beginner's Guide to Cloud Hosting for Private Deployment

This document introduces how to create a Go containerized application from scratch and deploy it to Tencent Cloud hosting (CloudBase Run).

Prerequisites:

  1. Activate the Enterprise Edition plan and deploy the WeDa Low-Code private deployment cluster version.
  2. Requires a container image server connected to the cloud hosting network (for storing the images you build)
  3. Requires the environment to reserve sufficient resources to ensure the deployed containerized services can run properly
  4. Requires installing golang and docker for local development

Step 1: Write the Basic Application

Create a new directory named helloworld and navigate into it:

mkdir helloworld
cd helloworld

Initialize Project:

go mod init helloworld

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("/api/post", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
handler(w, r)
})
port := "8089"
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "{\"message\": \"Hello Weda!\"}\n")
}

This code creates a basic web server listening on port 8089. You can extend this code to add input body handling; refer to golang web development for details.

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 --from=builder /app/server /app/server

EXPOSE 8089

# 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 8089:8089  helloworld-go

Access curl -X POST 'http://127.0.0.1:8080/api/post', and you should see the output "{"message": "Hello Weda!"}".

Step 4: Upload the Image to the Container Image Repository

// Note:
// 1) my-regisitry.tencentcloudcr.com is the service name for the container image; this is just an example;
// 2) mysoftware is the container namespace; use your own, this is just an example
// 3) helloworld-go:v0.0.1 is the container name and tag; the tag is similar to a version
docker tag helloworld-go my-regisitry.tencentcloudcr.com/mysoftware/helloworld-go:v0.0.1

// 4) Log in to your container image service
docker login -p xxx -u yyy my-regisitry.tencentcloudcr.com
// 5) Push the image to your repository
docker push my-regisitry.tencentcloudcr.com/mysoftware/helloworld-go:v0.0.1

Step 5: Deploy to the Software-Defined Cloud Hosting

Log in to the WeDa Low-Code private deployment cluster version management console.

Select "Cloud Hosting" --> "Deploy via Container Image", then follow the prompts to proceed with the deployment.

Step 6: Call the Service via APIs

Wait for the service deployment to complete. Log in to the low-code platform --> "Extension Capabilities" --> "Resource Links" --> "API Connectors" --> "Custom APIs" --> "HTTP Requests"

For example: The default domain for your service is: http://helloweb-private:8080, and the corresponding path is /api/post

Then you can set the URL in APIs as: http://helloweb-private:8080/api/post