All Products
Search
Document Center

Elastic Container Instance:Commit a container to generate an image and push the image to an image repository in Container Registry

Last Updated:May 08, 2024

After you start an elastic container instance, you may need to modify a container in the instance based on your business requirements. If you want to retain the modified container environment for future use, you can package the new environment into an image and push the image to an image repository in Container Registry. This topic describes how to use the CommitContainer and DescribeCommitContainerTask API operations to commit a container, generate an image, and push the image to an image repository in Container Registry.

Preparations

  1. Create a Container Registry instance and an image repository, and obtain the address of the image repository.

    For more information about how to create a Container Registry instance and an image repository, see the following documents:

    After the Container Registry instance and image repository are created, you can obtain the repository address from the Repositories page. In this example, a VPC endpoint is obtained.

    镜像仓库地址.png

  2. Create a RAM role for Elastic Container Instance and attach a policy to the RAM role. The policy allows the RAM role to access Container Registry. Then, obtain the Alibaba Cloud Resource Name (ARN) of the RAM role.

    1. Create a RAM role for Elastic Container Instance

      For more information, see Create a RAM role for a trusted Alibaba Cloud service. Take note of the following configurations:

      • In the Select Role Type step of the Create Role wizard, select Alibaba Cloud Service in the Select Trusted Entity section.

      • In the Configure Role step, set the Role Type parameter to Normal Service Role, configure the RAM Role Name parameter, and select ECI from the trusted service drop-down list.

    2. Attach a policy to the RAM role.

      For more information, see Grant permissions to a RAM role. Select the AliyunContainerRegistryFullAccess policy.

    3. Obtain the ARN of the RAM role.

      You can obtain the ARN in the Basic Information section of the role details page.

      角色ARN.png

Configuration description

The CommitContainer API operation allows you to create an asynchronous task. You can save a container in an elastic container instance as an image and push the image to an image repository in Alibaba Cloud Container Registry. The following table describes the major parameters of CommitContainer. For more information, see CommitContainer.

Parameter

Type

Example

Description

ContainerGroupId

string

eci-bp1do4xz75fa5sd****

The ID of the elastic container instance for which you want to create a CommitContainer task.

ContainerName

string

container-1

The name of the container for which you want to create an image.

Image.Repository

string

registry-vpc.cn-beijing.aliyuncs.com/test/test-****

The address of the image repository in Container Registry.

For security reasons, you can only PUSH images to image repositories in Container Registry.

Image.Tag

String

1.0.1

The image tag.

AcrRegistryInfo.InstanceId

string

cri-nwj395hgf6f3****

The ID of the Container Registry Enterprise Edition instance.

You do not need to configure this parameter if you use a Container Registry Personal Edition instance. You must configure this parameter if you use a Container Registry Enterprise Edition instance.

AcrRegistryInfo.RegionId

string

cn-beijing

The region ID of the Container Registry Enterprise Edition instance.

You do not need to configure this parameter if you use a Container Registry Personal Edition instance. You must configure this parameter if you use a Container Registry Enterprise Edition instance.

Arn.RoleArn

string

acs:ram::1609982529******:role/commit-test

The ARN of the RAM role. For information about how to configure and obtain the ARN, see the Preparations section in this topic.

Arn.RoleType

string

service

The type of the authorization. A value of service indicates that a RAM role is used for authorization.

After you call the CommitContainer API operation, the response contains the TaskId parameter. You can call the DescribeCommitContainerTask API operation to query the status of the CommitContainer task based on the task ID. If the returned TaskStatus value is Succeeded, the image is generated and pushed to an image repository in Container Registry. For more information about the DescribeCommitContainerTask API operation, see DescribeCommitContainerTask.

Sample configurations

  1. Create an Elastic Container Instance A (Instance A).

  2. Connect to Instance A and perform operations in the container.

    In this example, a file (hello.txt) is written to the container.

    eci-commit

  3. Call the CommitContainer API operation to package the container environment in Instance A into an image and push the image to an image repository in Container Registry.

    Example of parameters:

    RegionId=cn-beijing
    ContainerGroupId=eci-2zeh3j8kdnqvnf5n****
    ContainerName-container-1
    Image.Repository=registry-vpc.cn-beijing.aliyuncs.com/***-test/***-registry
    Image.Tag=1.0.1
    Arn,RoleArn=acs:ram::1609982529******:role/commit-test
    Arn.RoleType=service

    If you push the image to an image repository of a Container Registry Enterprise Edition instance, you must configure the access credentials of the Enterprise Edition instance by using AcrRegistryInfo relevant parameters. Example:

    AcrRegistryInfo.InstanceId=cri-nwj395hgf6f3****
    AcrRegistryInfo.RegionId=cn-beijing

    The following result is returned:

    {
      "TaskId": "t-2zej6nstkg744qc3****",
      "RequestId": "4ADE19FF-C487-5020-A6E4-03D5D9708834"
    }
  4. Call the DescribeCommitContainerTask API operation to query the status of the CommitContainer task based on the task ID.

    Example of parameters:

    RegionId=cn-beijing
    TaskId=["t-2zej6nstkg744qc3****"]

    The following result is returned. If the TaskStatus value is Succeeded, the CommitContainer task is successfully executed. In the CommitPhaseInfos section, the status of the PushCommittedImage phase is success. This indicates that the image has been pushed to the specified image repository in the Container Registry instance.

    {
      "TotalCount": 1,
      "CommitTasks": [
        {
          "TaskId": "t-2zej6nstkg744qc3****",
          "ContainerName": "container-1",
          "CommitPhaseInfos": [
            ......
            {
              "Status": "Success",
              "RecordTime": "2023-02-01T11:08:04.313047785+08:00",
              "Phase": "PushCommittedImage",
              "Message": "Push image registry-vpc.cn-beijing.aliyuncs.com/***-test/***-registry:1.0.1 successfully"
            }
          ],
          "CreationTime": "2023-02-01T11:08:00Z",
          "TaskStatus": "Succeeded",
          "FinishedTime": "2023-02-01T11:08:10Z"
        }
      ],
      "NextToken": "",
      "RequestId": "42E65299-8224-5D4A-915E-5AEC04A523FC"
    }

    Log on to the Container Registry console. You can view the new image that is pushed by Elastic Container Instance in the image repository in the Container Registry instance.

    推送镜像.png

  5. Create an Elastic Container Instance B (Instance B) based on the new image that is pushed to the image repository in the Container Registry instance.

    The new image is registry-vpc.cn-beijing.aliyuncs.com/***-test/***-registry:1.0.1 in this example.

    eci-commit2

    Note

    If you select an image from an image repository in Container Registry Personal Edition instance, select My Images. If you select an image from an image repository in Container Registry Enterprise Edition instance, select Container Registry Enterprise Edition image.

  6. Check whether the container environment in Instance B is the same as that in Instance A.

    In this example, the hello.txt file is added to a container of Instance A. An image is generated based on the container of Instance A. Instance B is created based on the image. As a result, the hello.txt file is displayed in the container of Instance B by default.

    eci-commit3