Help Center/ FunctionGraph/ Developer Guide/ Go/ Creating an Event Function Using a Container Image Built with Go
Updated on 2026-01-04 GMT+08:00

Creating an Event Function Using a Container Image Built with Go

For details about how to use a container image to create and execute an event function, see Creating an Event Function Using a Container Image and Executing the Function. This section describes how to create an image using Go and verify the image locally.

Step 1: Creating an Image

Take the Linux x86 64-bit OS as an example. (There are no specific requirements for system configurations.)

  1. Run the following command to create a folder:
    mkdir custom_container_event_example && cd custom_container_event_example
  2. Implement an HTTP server to process init and invoke requests and give a response. Go is used as an example.
    Create a server_demo.go file, import the gin dependency package, and implement a function handler (method POST and path /invoke). Call the init function to initialize the configuration. Go runs this function automatically. Example code:
    package main
    
    import (
       "fmt"
       "net/http"
       "time"
    
       "github.com/gin-gonic/gin" // Import the Gin framework.
    )
    
    // Initialization function, which is executed when the program is started.
    func init() {
       fmt.Println("init in main.go ")
    }
    
    // Logger is a middleware function used to record request and response information.
    func Logger() gin.HandlerFunc {
       return func(c *gin.Context) {
          start := time.Now()
    
          reqBody, _ := c.GetRawData()
          fmt.Printf("[INFO] Request: %s %s %s\n", c.Request.Method, c.Request.RequestURI, reqBody)
    
          c.Next()
    
          end := time.Now()
          latency := end.Sub(start)
          respBody := string(rune(c.Writer.Size()))
          fmt.Printf("[INFO] Response: %s %s %s (%v)\n", c.Request.Method, c.Request.RequestURI, respBody, latency)
       }
    }
    
    // invoke is a function that processes POST requests sent to the /invoke route.
    func invoke(c *gin.Context) {
       println("hello world")
       c.String(http.StatusOK, "*** hello world ***")
       return
    }
    
    func main() {
       router := gin.Default() // Create a default Gin router.
    
       router.Use(Logger()) // Use the Logger middleware.
    
       router.POST("/invoke", invoke) // Register a route (/invoke) that processes HTTP POST requests. When a client sends a POST request to /invoke, the Gin framework calls the invoke function to process the request.
       err := router.Run(":8000") // Start the HTTP server and listen to port 8000.
       if err != nil {
          return
       }
    }
  3. Execute the following command to compile and generate the server_demo binary file:
    go build server_demo.go
  4. Create a Dockerfile and replace server_demo in the Dockerfile with the compiled file name. The file content is as follows:
    FROM ubuntu:22.04
    
    ENV HOME=/home/paas
    ENV GROUP_ID=1003
    ENV GROUP_NAME=paas_user
    ENV USER_ID=1003
    ENV USER_NAME=paas_user
    
    RUN mkdir -m 550 ${HOME} && groupadd -g ${GROUP_ID} ${GROUP_NAME} && useradd -u ${USER_ID} -g ${GROUP_ID} ${USER_NAME}
    
    COPY server_demo ${HOME}
    
    RUN chown -R ${USER_ID}:${GROUP_ID} ${HOME}
    RUN chmod -R 775 ${HOME}
    
    USER ${USER_NAME}
    WORKDIR ${HOME}
    EXPOSE 8000
    ENTRYPOINT ["./server_demo"]
    Table 1 Parameter description

    Parameter

    Description

    FROM

    Specifies base image ubuntu:22.04. The base image is mandatory and its value can be changed.

    ENV

    Sets environment variables HOME (/home/custom_container), GROUP_NAME and USER_NAME (custom_container), and USER_ID and GROUP_ID (1003). These environment variables are mandatory and their values can be changed.

    RUN

    Executes commands. The format is RUN <command>. For example, RUN mkdir -m 550 ${HOME} means to create directory ${HOME} for user ${USER_NAME} during container building.

    COPY

    Copies files or directories from the build context to the image. Copy server_demo.go to the ${HOME} directory of user ${USER_NAME} in the container.

    USER

    Switches to user ${USER_NAME}.

    WORKDIR

    Switches the working directory to the ${HOME} directory of user ${USER_NAME}.

    EXPOSE

    Informs Docker that the container listens on the specified network ports at runtime. Expose port 8000 of the container and do not change it.

    ENTRYPOINT

    Sets the executable command that is run each time a container starts. Run the ./server_demo command to start a container.

  5. Run the following command to build an image:
    docker build -t custom_container_event_example:latest .

    In the preceding command, the image name is custom_container_event_example, the tag is latest, and the period (.) indicates the directory where the Dockerfile is located. Run the image build command to pack all files in the directory and send the package to a container engine to build an image.

Step 2: Performing Local Verification

  1. Run the following command to start the Docker container:
    docker run -u 1003:1003 -p 8000:8000 custom_container_event_example:latest
  2. Open a new Command Prompt, and send a message through port 8000 to access the /invoke directory specified in the template code.
    curl -XPOST -H 'Content-Type: application/json' -d '{"message":"HelloWorld"}' localhost:8000/invoke

    The following information is returned based on the module code:

    *** hello world ***

Step 3: Creating a Function

Once you build the container image locally, you can create a function on the console.

For details, see Creating an Event Function Using a Container Image and Executing the Function. Start from Step 3: Upload the Image.

Helpful Links

  • For more information about function development, such as the supported runtimes, trigger events, function project packaging specifications, and DLL referencing, see Function Development Overview.
  • For details about the syntax and SDK APIs of function development in Go, see Function Development Overview.