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

Prerequisites

A Container Registry Enterprise or Personal Edition instance is created. We recommend that you create a Container Registry Enterprise Edition instance.

Create a function in the Function Compute console

  1. Push your function image to the image repository for the default instance.

    In this example, the sample project is in the /tmp directory, the region of Function Compute is China (Shenzhen), and the name of the image repository is nodejs-express.

    1. Run the following command to go to the /tmp directory:
      cd /tmp
    2. Run the following command in the /tmp directory to decompress the sample project:
      git clone https://github.com/awesome-fc/custom-container-docs.git
    3. Run the following command to go to the custom-container-docs/nodejs-express directory:
      cd custom-container-docs/nodejs-express
    4. Run the following command to specify the image repository. Replace your ACR image name with the name of your image. For example, replace your ACR image name with registry.cn-shenzhen.aliyuncs.com/fc-demo/nodejs-express:v0.2.
      export IMAGE_NAME="your ACR image name" 
    5. Run the following command to package the image:
      docker build -t $IMAGE_NAME .
    6. Run the following command to push the image:
      docker push $IMAGE_NAME
  2. Create a service and configure permissions for it.
    1. In the Function Compute console, 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.
      The preceding policies allow Function Compute to obtain the temporary account for the default instance in Container Registry. Then, Function Compute uses the temporary account to push the image from your private image repository.
  3. Create a function.
    In this example, an event function is created. HTTP functions use similar parameter settings.
    1. Log on to the Function Compute console.
    2. In the top navigation bar, select the region where the service resides.
    3. In the left-side navigation pane, click Services and Functions.
    4. Click Create Function. On the Create Function page, move the pointer over the Event Function section and click Configure and Deploy.
    5. In the Configure Function section, set the parameters described in the following table and click Create.
      create custom container
      Parameter Configuration method
      Service Name Select the service that you created in Step 2.
      Function Name Enter a custom function name.
      Runtime Select Custom Container from the drop-down list.
      Instance Type Select a type of instance used to execute the function. Valid values:
      • Elastic Instance
      • Performance Instance

      For more information about instance types, see Instance specifications and usage modes.

      Container Image Click Select Container Image to select an image version on the Instance of Personal Edition or Instance of Enterprise Edition tab.
      Note If you select a Container Registry Enterprise Edition instance, make sure that the default IP address to which the domain name is resolved is the address in the same VPC as Function Compute. The domain name cannot be resolved by using Alibaba Cloud DNS PrivateZone.
      Command Enter a startup command, such as ["/code/myserver"].

      This parameter is optional. If you do not set this parameter, the ENTRYPOINT or CMD command included in the image is used.

      Args Enter additional parameters, such as ["-arg1", "value1"].

      This parameter is optional. If you do not set this parameter, the CMD command included in the image is used.

      Memory Select the memory size used to execute the function. The memory size cannot be less than 512 MB.
      Timeout Enter the timeout period for requests.
      Single Instance Concurrency Enter the maximum number of requests that can be concurrently processed by a single instance.

      For more information, see A single instance that concurrently processes multiple requests.

      Listening Port Enter the port on which the server listens.

      This parameter is optional. Default value: 9000. You can change the listening port on this page without the need to modify the image.

      After the function is created, you can view the function in the function list of the corresponding service.

Use Funcraft to create a function

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

  1. Run the following command to clone the custom-container-docs example:
    git clone https://github.com/awesome-fc/custom-container-docs.git
  2. Run the following command to go to the custom-container-docs/nodejs-express directory:
    cd custom-container-docs/nodejs-express
  3. In the template.yml file, replace the value of the Image parameter with the address of your image in Container Registry.
  4. Run the following command to build an image:
    fun build --use-docker
  5. Run the following command to deploy a function:
    # Deploy the function, push the image via the internet registry host (the function config uses the VPC registry for faster image pulling).
    fun deploy --push-registry acr-internet
    After the function is deployed, you can log on to the Function Compute console and view the function in the function list of the corresponding service.