This topic describes how to create a custom container function by using the Function Compute console or Serverless Devs.


Use the console to create a function

This topic describes how to create a function by running commands in the /tmp directory. In this example, the region of Function Compute is China (Shenzhen) and the image repository name is nodejs-express.

Step 1: Push the image to the default image repository

  1. Run the following command to go to the /tmp directory:
    cd /tmp
  2. Run the following command in the /tmp directory to clone the sample project:
    git clone
  3. Run the following command to go to the project directory:
    cd custom-container-docs/nodejs-express
  4. Run the following command to specify the image repository:
    export IMAGE_NAME="" 
  5. Run the following command to package the image:
    docker build -t $IMAGE_NAME .
    Note When you build images, you must specify the image compilation platform as Linux AMD64 to implement cross-platform compilation if you use a device running the ARM architecture, such as a Mac computer equipped with Apple chips. Sample code: docker build --platform linux/amd64 -t $IMAGE_NAME.
  6. Run the following command to push the image:
    docker push $IMAGE_NAME

Step 2: Create a service and configure permissions for the service

  1. Log on to the Function Compute console and create a service. For more information, see Create a Service.
  2. Attach the AliyunContainerRegistryReadOnlyAccess or AliyunContainerRegistryFullAccess policy to the service. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
    Function Compute uses the preceding policies to obtain the temporary account for the default instance in Container Registry and then uses the temporary account to push the image from your private image repository.

Step 3: Create a function

  1. Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
  2. In the top navigation bar, select a region. On the Services page, click the desired service.
  3. On the Functions page, click Create Function.
  4. On the Create Function page that appears, select Use Container Image, configure the following parameters based on your business requirements, and then click Create.
    • Basic Settings: Configure basic information about the function, including the function name, web server mode, and the handler type.
      Parameter Description
      Function Name Enter a name for the function. If you do not specify this parameter, Function Compute automatically creates a name for you.
      Web Server Mode Select whether to enable the web server mode. Valid values:
      • Yes: enables the web server mode. You need to implement a web server in your container image to listen on ports and process requests.
      • No: disables the web server mode. In this case, Request Type can only be set to Event Requests. After the request is processed, you need to actively exit the process and set ExitCode to 0. In addition, the requested event parameters are passed to the function in the form of environment variables.
      Request Type Specify the type of requests that are processed by the function. The requests can be event requests or HTTP requests. Valid values:
      • Event Requests: Triggers the execution of the function by using a timer, calling API operations, using SDKs, or using triggers integrated with other Alibaba Cloud services.
      • HTTP Requests: Processes HTTP requests or WebSocekt requests. If your scenario is a Web scenario, we recommend that you select Use Custom Runtime.
    • Image Configurations: Configure the image that is used to create the function.
      Parameter Description
      Image Selection Mode Select Use Sample Images or select your own image to create a function.
      • Use Sample Images: Select a sample image in Function Compute.
      • Use Container Registry Images: Click Select a Container Registry image below Container Image. In the Select Container Image panel that appears, configure Container Image Instance and Container Registry Repository. Among the images that appear, find the desired image and click Select in the Actions column.
      Startup Command Specify the startup command of the container. If you do not configure this parameter, the Entrypoint or CMD command in the image is used.
      Listener Port Specify the port on which the HTTP server that is implemented by using the container image listens. The default port is 9000. If you do not specify this parameter, the container image runs in non-interactive mode.
    • Advanced Settings: Configure the instance information, execution timeout period, and handler of the function.
      Parameter Description
      GPU Acceleration
      Note This option is only available if you select Use Container Image when you create the function. In this case, you can select Enable GPU to use the GPU acceleration feature.
      Select whether to use GPU-accelerated instances based on your business requirements. By default, elastic instances are used and GPU-accelerated instances are not used. For more information, see Instance types. For information about the billing for each instance type, see Billing overview.
      • Enable GPU
      • Disable GPU
      • Select Enable GPU

        Configure GPU Specification based on your business requirements. Function Compute automatically configures vCPU Specification and Memory Capacity based on the GPU specification that you select. In this case, custom configurations of vCPU Specification and Memory Capacity are not supported.

      • Select Disable GPU.

        Configure vCPU Specification and Memory Capacity based on your business requirements.

      For more information about the billing for resource usage, see Billing overview.
      Note The ratio of vCPU specification to memory capacity (in GB) must be set from 1:1 to 1:4.
      Disk Size Select the disk size based on your business requirements. Function Compute provides you with a free quota of disk capacity up to 512 MB. For more information, see Billing overview.
      Instance Concurrency If you set Web Server Mode to No, you do not need to set this parameter. Specify the concurrency of function instances. For more information, see Specify the instance concurrency.
      Execution Timeout Period Configure the timeout period for the execution of the function. The default value is 60 seconds. The maximum value is 86,400 seconds. We recommend that you set this parameter to 600 seconds.
      Note If the execution time of the function exceeds the specified timeout period, the function fails to be executed. If you want to specify a longer timeout period, Submit a ticket.
    • Environment Variables: Configure the environment variables in the runtime environment of the function. For more information, see Environment variables.
    • Trigger Configurations: Configure a trigger for the function. You can use the trigger to trigger the execution of the function. For more information, see Manage triggers.
    After the function is created, you can view and update the function in the function list of the service.
Note When you update a function, you can only change the specified listening port. You cannot delete the port or add extra listening ports. If a listening port is configured when you create the function, the listening port is retained when you update the function if you do not specify the listening port.

Use Serverless Devs to create a function

You can use Serverless Devs to build and push container images and deploy functions with a few clicks.

  1. Run the following command to initialize a project:
    s init start-fc-custom-container-event-nodejs14
    Sample command output:
     Serverless Awesome:
     Please input your project name (init dir) start-fc-custom-container-event-nodejs14
     file decompression completed
         ____  _     _ ___  _ _     _        _____ ____
        /  _ \/ \   / \\  \/// \ /\/ \  /|  /    //   _\
        | / \|| |   | | \  / | | ||| |\ ||  |  __\|  /
        | |-||| |_/\| | / /  | \_/|| | \||  | |   |  \__
        \_/ \|\____/\_//_/   \____/\_/  \|  \_/   \____/
     please select credential alias default
        Welcome to the Aliyun FC start application
         This application requires to open these services:
             FC :
         * Note: Actions are declared in the s.yaml file:
            Run the following command before project deployment: s build --use-docker --dockerfile ./code/Dockerfile.
           If you do not need to build the project, you can use annotations to skip the preceding command. 
           > Before the deployment, replace the value of the image parameter in the s.yaml file with the address of your image in Container Registry. 
         * The project is initialized. You can go to the project directory and run the s deploy command to deploy the project. 
     Thanks for using Serverless-Devs
     You could [cd /test/test1/start-fc-custom-container-event-nodejs14] and enjoy your serverless journey!
     If you need help for this example, you can use [s -h] after you enter folder.
     Document Star:
     Do you want to deploy the project immediately? No
  2. Run the following command to go to the project directory:
    cd start-fc-custom-container-event-nodejs14
  3. Edit the s.yaml file.
    Replace the value of the image parameter with the address of your image in Container Registry.
  4. Run the following command to deploy the project:
    s deploy
    Sample command output:
    [2021-12-15 07:54:30] [INFO] [S-CLI] - Start ...
    [2021-12-15 07:54:30] [INFO] [S-CLI] - Start the pre-action
    [2021-12-15 07:54:30] [INFO] [S-CLI] - Action: s build --use-docker --dockerfile ./code/Dockerfile
    [2021-12-15 07:54:31] [INFO] [S-CLI] - Start ...
    [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Build artifact start...
    [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Use docker for building.
    [2021-12-15 07:54:32] [INFO] [FC-BUILD] - Building image...
    Sending build context to Docker daemon   5.12kB
    Step 1/7 : FROM node:14.5.0-alpine3.11
     ---> 5d97b3d11dc1
    Step 7/7 : ENTRYPOINT [ "node", "server.js" ]
     ---> Using cache
     ---> a5ef1c015e7e
    Successfully built a5ef1c015e7e
    Successfully tagged
    SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
    Build image( successfully
    [2021-12-15 07:54:33] [INFO] [FC-BUILD] - Build artifact successfully.
    Tips for next step
    * Invoke Event Function: s local invoke
    * Invoke Http Function: s local start
    * Deploy Resources: s deploy
    End of method: build
    [2021-12-15 07:54:33] [INFO] [S-CLI] - End the pre-action
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using access alias: default
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Checking Service hello-world-service exists
    [2021-12-15 07:54:35] [INFO] [FC-DEPLOY] - Setting role: AliyunFCDefaultRole
    [2021-12-15 07:54:35] [INFO] [RAM] - Checking Role AliyunFCDefaultRole exists
    [2021-12-15 07:54:35] [INFO] [RAM] - Updating role: AliyunFCDefaultRole
    [2021-12-15 07:54:35] [INFO] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists
    [2021-12-15 07:54:35] [INFO] [FC-DEPLOY] - Checking Function nodejs14-event-function exists
    [2021-12-15 07:54:36] [INFO] [FC-DEPLOY] - Using image registry:
    [2021-12-15 07:54:36] [INFO] [FC-DEPLOY] - Try to use a temporary token for docker login
    Login to registry: with user: cr_temp_user
    Pushing docker image:
    The push refers to repository []
    cdf38e7753b7: Layer already exists                                                                                                                                                           43128f71725b: Layer already exists                                                                                                                                                           0fb36a16ab83: Layer already exists                                                                                                                                                           dd966b9fd474: Layer already exists                                                                                                                                                           a1915d7a1111: Layer already exists                                                                                                                                                           c4491b3ee709: Layer already exists                                                                                                                                                           9fb10d900487: Layer already exists                                                                                                                                                           3e207b409db3: Layer already exists                                                                                                                                                           nginx: digest: sha256:02b69157def85ceb72f32cb1c5845d00e1d8df19caf6eaf720a9bc77bb57db76 size: 1991
    √ Make service hello-world-service success.
    √ Make function hello-world-service/nodejs14-event-function success.
    [2021-12-15 07:54:39] [INFO] [FC-DEPLOY] - Checking Service hello-world-service exists
    [2021-12-15 07:54:39] [INFO] [FC-DEPLOY] - Checking Function nodejs14-event-function exists
    There is auto config in the service: hello-world-service
    Tips for next step
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
      region:   cn-hangzhou
        name: hello-world-service
        name:       nodejs14-event-function
        runtime:    custom-container
        handler:    not-used
        memorySize: 256
        timeout:    60
  5. Run the following command to debug the function:
    s invoke -e "{\"key\":\"val\"}"
    Sample command output:
    [2021-12-15 08:00:17] [INFO] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: 768945c8-f92d-428e-89c2-ecd50883****
    FC Invoke End RequestId: 768945c8-f92d-428e-89c2-ecd50883****
    Duration: 3.05 ms, Billed Duration: 4 ms, Memory Size: 256 MB, Max Memory Used: 10.77 MB
    ========= FC invoke Logs end =========
    FC Invoke Result:
    End of method: invoke