すべてのプロダクト
Search
ドキュメントセンター

Function Compute:カスタムコンテナ関数を作成する

最終更新日:Mar 27, 2025

Function Compute の組み込みランタイムがビジネス要件を満たせない場合は、カスタムコンテナランタイムを使用して関数を作成できます。このトピックでは、Function Compute コンソールまたは Serverless Devs を使用してカスタムコンテナ関数を作成する方法について説明します。

使用上の注意

Function Compute でカスタムコンテナ関数を作成するには、関数と同じリージョンにあり、同じアカウントに属する Alibaba Cloud Container Registry イメージを使用する必要があります。Apple チップで動作する Mac コンピュータなどの ARM ベースのコンピュータを使用する場合は、イメージのコンパイルプラットフォームを linux/amd64 として指定する必要があります。以下はコマンドの例です:docker build --platform linux/amd64 -t $IMAGE_NAME .

前提条件

Function Compute コンソールで関数を作成する

ステップ 1:サービスを作成し、権限を構成する

  1. Function Computeコンソールで関数を作成します。

    詳細については、「サービスを作成する」をご参照ください。

  2. [aliyuncontainerregistryreadonlyaccess] ポリシーまたは [aliyuncontainerregistryfullaccess] ポリシーをサービスにアタッチします。

    詳細については、「他の Alibaba Cloud サービスにアクセスするための権限を Function Compute に付与する」をご参照ください。上記のポリシーにより、Function Compute は、デフォルトの Container Registry インスタンスにログインしてプライベートイメージリポジトリからイメージをプッシュするために使用できる一時アカウントを取得できます。

ステップ 2:関数を作成する

  1. Function Compute コンソールにログインします。左側のナビゲーションウィンドウで、[サービスと関数] をクリックします。

  2. 上部のナビゲーションバーで、リージョンを選択します。[サービス] ページで、目的のサービスをクリックします。

  3. [関数] ページで、[関数を作成] をクリックします。

  4. [関数を作成] ページで、コンテナーイメージの使用 を選択し、ビジネス要件に基づいて次のパラメータを構成し、[作成] をクリックします。

    • [基本設定]:作成する関数の基本情報を構成します。このような情報には、関数名とハンドラタイプが含まれます。

      パラメータ

      説明

      [関数名]

      関数の名前を入力します。このパラメータを空のままにすると、Function Compute によって関数の名前が自動的に作成されます。

      [ハンドラタイプ]

      関数によって処理されるリクエストのタイプを指定します。リクエストは、イベントリクエストまたは HTTP リクエストです。有効な値:

      • [イベントハンドラ]:タイマーの使用、 API 操作の呼び出し、 SDK の使用、または他の Alibaba Cloud サービスのトリガーを使用して関数の実行をトリガーします。

      • [HTTP ハンドラ]: HTTP リクエストを送信するか、WebSocket 接続を確立することで関数の実行をトリガーします。Web シナリオでは、カスタムランタイムの使用 を選択して関数を作成することをお勧めします。

    • [イメージの構成]:関数のイメージを構成します。

      パラメータ

      説明

      [イメージ選択モード]

      [サンプルイメージを使用] を選択するか、独自のイメージを使用して関数を作成します。

      • [サンプルイメージを使用]Function Compute の組み込みサンプルイメージを選択します。

      • [container Registry イメージを使用]:Container Registry イメージを使用します。[コンテナイメージ] の下にある [container Registry イメージを選択] をクリックします。[コンテナイメージを選択] パネルで、[コンテナイメージインスタンス] パラメータと [container Registry リポジトリ] パラメータを構成します。使用するイメージを見つけて、[アクション] 列の [選択] をクリックします。

        説明
        • Container Registry から別のアカウントに属するパブリックイメージを使用して関数を作成することはできません。

        • Container Registry Enterprise Edition の場合、Function Compute は、選択したイメージのタグを使用して関数インスタンスを作成します。

          • Container Registry Enterprise Edition ( Advanced Edition または Standard Edition ) のイメージを使用する場合は、[イメージバージョン][不変] を有効にすることをお勧めします。そうしないと、イメージタグが他の場所で更新され、Function Compute が更新されたイメージデータを取得して関数インスタンスを起動する可能性があります。詳細については、「リポジトリを不変として構成する」をご参照ください。

          • Container Registry Enterprise Edition ( Basic Edition ) または Container Registry Personal Edition のイメージを使用する場合は、イメージリポジトリで同じ名前のイメージタグを更新した後、Function Compute コンソールでイメージ情報に基づいて関数を更新する必要があります。その後、Function Compute は新しいイメージで関数インスタンスを起動できます。

      [起動コマンド]

      コンテナの起動コマンドを指定します。このパラメータを構成しない場合、イメージ内の Entrypoint または CMD 命令が使用されます。

      [リスニングポート]

      コンテナイメージに実装されている HTTP サーバーがリスニングするポートを指定します。デフォルトポートは 9000 です。

    • [詳細設定]:インスタンス関連の情報や実行タイムアウト期間などの詳細設定を構成します。

      パラメータ

      説明

      [GPU アクセラレーション]

      ビジネス要件に基づいて GPU インスタンスを使用するかどうかを指定します。デフォルトでは、GPU インスタンスではなく CPU インスタンスが使用されます。詳細については、「インスタンスタイプ」をご参照ください。各インスタンスタイプの課金については、「課金の概要」をご参照ください。有効な値:

      • [GPU を有効にする]

      • [GPU を無効にする]

      [仕様]

      • [GPU を有効にする]

        [GPU タイプ] ドロップダウンリストから値を選択し、ビジネス要件に基づいて [GPU メモリサイズ] を指定します。Function Compute は、選択した GPU メモリサイズに基づいて、[vcpu 容量] フィールドと [メモリ容量] フィールドに自動的に入力します。各 GPU サイズに対して、特定の vCPU 容量とメモリ容量のみが許可されます。

      • [GPU を無効にする]

        ビジネス要件に基づいて [vcpu 容量][メモリ容量] を構成します。

      リソースの課金については、「課金の概要」をご参照ください。

      説明

      vCPU 仕様とメモリ容量 ( GB 単位) の比率は 1:1 から 1:4 である必要があります。

      [一時ディスクのサイズ]

      ビジネス要件に基づいてディスクサイズを選択します。Function Compute は 512 MB の無料ディスク容量を提供します。詳細については、「課金の概要」をご参照ください。

      [インスタンスの同時実行数]

      インスタンスの同時実行数を指定します。詳細については、「インスタンスの同時実行数を構成する」をご参照ください。

      [実行タイムアウト期間]

      関数の実行のタイムアウト期間を指定します。デフォルト値は 60 秒です。最大値は 86400 秒です。このパラメータを 600 秒に設定することをお勧めします。

      [タイムゾーン]

      関数のタイムゾーンを指定します。タイムゾーンを選択すると、環境変数 [TZ] が関数に自動的に追加されます。変数の値は、選択したタイムゾーンです。

    • [環境変数]:関数のランタイムの環境変数を構成します。詳細については、「環境変数」をご参照ください。

    • [トリガーの構成]:関数の呼び出しのトリガーを構成します。詳細については、「トリガーを管理する」をご参照ください。

    関数が作成された後、サービスの関数リストで関数を確認および更新できます。

説明

関数を更新するときに、指定したリスニングポートを変更できますが、リスニングポートを削除したり、追加したりすることはできません。関数を作成するときにリスニングポートが指定されている場合、別のリスニングポートを指定しないと、関数を更新するときにリスニングポートが保持されます。

Serverless Devs を使用して関数を作成する

Serverless Devs を使用すると、数回クリックするだけでコンテナイメージをビルドおよびプッシュし、関数をデプロイできます。

  1. 次のコマンドを実行してプロジェクトを初期化します。プロンプトが表示されたら、Container Registry で作成したイメージリポジトリのアドレスを入力する必要があります。

    sudo s init start-fc-custom-container-event-nodejs14

    次の出力が返されます。

     Serverless Awesome: https://github.com/Serverless-Devs/package-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 : https://fc.console.aliyun.com/
             ACR: https://cr.console.aliyun.com/
    
         * Note: The actions information is declared in the s.yaml file: 
            Run the sudo s build --use-docker --dockerfile ./code/Dockerfile command before the project is deployed.
           If you do not need to build the project, you can comment out this part. 
           > Note: Before project deployment, replace the value of the image parameter in the s.yaml file with the address of the image repository 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: https://github.com/Serverless-Devs/Serverless-Devs
     Do you want to deploy the project immediately?  No
  2. 次のコマンドを実行して、プロジェクトのディレクトリに移動します。

    cd start-fc-custom-container-event-nodejs14
  3. s.yaml ファイルを編集します。.yaml ファイルのパラメータの詳細については、「YAML 構文と権限管理」をご参照ください。

  4. 次のコマンドを実行して、プロジェクトをデプロイします。

    sudo s deploy

    次の出力が返されます。

    [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 registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx
    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(registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx) 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: yourAccessKeyID
    [2021-12-15 07:54:34] [INFO] [FC-DEPLOY] - Using accessKeySecret: yourAccessKeySecret
    [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: registry.cn-hangzhou.aliyuncs.com
    [2021-12-15 07:54:36] [INFO] [FC-DEPLOY] - Try to use a temporary token for docker login
    Login to registry: registry.cn-hangzhou.aliyuncs.com with user: cr_temp_user
    Pushing docker image: registry.cn-hangzhou.aliyuncs.com/fc-example/test:nginx...
    The push refers to repository [registry.cn-hangzhou.aliyuncs.com/fc-example/test]
    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
    
    
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       nodejs14-event-function
        runtime:    custom-container
        handler:    not-used
        memorySize: 256
        timeout:    60
  5. 次のコマンドを実行して、関数をデバッグします。

    sudo s invoke -e "{\"key\":\"val\"}"

    次の出力が返されます。

    [2021-12-15 08:00:17] [INFO] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: 768945c8-f92d-428e-89c2-ecd50883****
    {"key":"val"}
    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:
    OK
    
    
    End of method: invoke

関連情報

  • GPU インスタンスを使用する場合は、カスタムコンテナ関数を作成する必要があります。GPU インスタンスの詳細については、「インスタンスタイプと使用モード」をご参照ください。

  • コードパッケージと比較して、コンテナイメージは必要なランタイム環境が含まれているため、ダウンロードと展開に時間がかかることがよくあります。このレイテンシを軽減する方法については、「コールドスタートの軽減に関するベストプラクティス」をご参照ください。

  • Function Compute API を呼び出して関数を作成することもできます。詳細については、「CreateFunction」をご参照ください。

  • 組み込みランタイム、カスタムランタイム、およびカスタムコンテナランタイムの一般的なシナリオと違いについては、「関数のランタイムの選択」をご参照ください。