After you create an Elastic Container Instance (ECI) instance, you can modify its containers. To save these modifications, you can commit a container to a new image and push that image to Container Registry. This topic describes how to do this by using the CommitContainer and DescribeCommitContainerTask API operations.
Prerequisites
-
Create a Container Registry instance and an image repository, and get the repository address.
To learn how to create a Container Registry instance and an image repository, see the following documents:
After you create the repository, get its address from the image repository page. This topic uses a private network address.
In the image repository list, click the operation button for the target repository to view three types of endpoint addresses: public endpoint (
registry.cn-beijing.aliyuncs.com), VPC endpoint (registry-vpc.cn-beijing.aliyuncs.com), and Classic Network endpoint (registry-internal.cn-beijing.aliyuncs.com). The VPC and Classic Network endpoints are private network addresses. -
Create a RAM role, grant permissions to the role, and then get the ARN of the role.
-
Create a RAM role.
For more information, see Create a RAM role for a trusted Alibaba Cloud service. Note the following settings:
-
On the Select Role Type page, select Alibaba Cloud Service as the trusted entity.
-
On the Configure Role page, set Normal Service Role to Normal Service Role, enter a custom role name, and select ECI as the trusted service.
-
-
Grant permissions to the RAM role.
For more information, see Manage permissions for a RAM role. When you grant permissions, attach the AliyunContainerRegistryFullAccess policy.
-
Get the ARN of the role.
You can get the ARN from the Basic Information section on the role details page.
-
Parameters
Call the CommitContainer API operation to create an asynchronous task that saves a specified container in an ECI instance to an image and pushes the image to an image repository in Container Registry. The following table describes the main parameters of the CommitContainer API operation. For more information, see CommitContainer.
|
Parameter |
Type |
Example |
Description |
|
ContainerGroupId |
string |
eci-bp1do4xz75fa5sd**** |
The ID of the ECI instance that contains the container to commit. |
|
ContainerName |
string |
container-1 |
The name of the container to commit. |
|
Image.Repository |
string |
registry-vpc.cn-beijing.aliyuncs.com/test/test-**** |
The address of the Container Registry image repository. For security reasons, you can only push images to 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. Required for Enterprise Edition instances only. |
|
AcrRegistryInfo.RegionId |
string |
cn-beijing |
The region of the Container Registry Enterprise Edition instance. Required for Enterprise Edition instances only. |
|
Arn.RoleArn |
string |
acs:ram::1609982529******:role/commit-test |
The ARN of the authorized RAM role. For information about how to configure and get the ARN, see the Prerequisites section. |
|
Arn.RoleType |
string |
service |
The authorization type. Set this to |
A successful call returns a TaskId. Use this ID with the DescribeCommitContainerTask API operation to query the task status. If TaskStatus is Succeeded, the image has been successfully generated and pushed to Container Registry. For more information about the parameters of the DescribeCommitContainerTask API operation, see DescribeCommitContainerTask.
Example
-
Create an ECI instance A.
-
Connect to ECI instance A and run commands in the container.
For example, write a file named
hello.txtinto the container.Elastic Container Service! root@eci-a:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@eci-a:/# echo hello eci >> hello.txt root@eci-a:/# ls bin boot dev etc hello.txt home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@eci-a:/# cat hello.txt hello eci root@eci-a:/# -
Call the CommitContainer API operation to commit the container in instance A to an image and push the image to Container Registry.
The following sample parameters are used:
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=serviceFor a Container Registry Enterprise Edition instance, you must also specify the instance ID and region by using the AcrRegistryInfo parameters. Example:
AcrRegistryInfo.InstanceId=cri-nwj395hgf6f3**** AcrRegistryInfo.RegionId=cn-beijingThe following response is expected:
{ "TaskId": "t-2zej6nstkg744qc3****", "RequestId": "4ADE19FF-C487-5020-A6E4-03D5D9708834" } -
Call the DescribeCommitContainerTask API operation to check the task status.
The following sample parameters are used:
RegionId=cn-beijing TaskId=["t-2zej6nstkg744qc3****"]The following response is expected. A
TaskStatusofSucceededindicates that the task is successful. WithinCommitPhaseInfos, theSuccessstatus for thePushCommittedImagephase confirms that the image has been pushed to the specified image repository.{ "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" }You can then log on to the Container Registry console and find the new image pushed by the ECI instance in the target image repository.
-
Create ECI instance B from the new image.
Use the image that the CommitContainer task pushed to Container Registry. For example,
registry-vpc.cn-beijing.aliyuncs.com/***-test/***-registry:1.0.1.NoteWhen you select the image, choose My Images for a Personal Edition repository, or Container Registry Enterprise Edition image for an Enterprise Edition repository.
-
Verify that the container environment in instance B is consistent with that of instance A.
In this example, the
hello.txtfile was added to the container of instance A. Because instance B is created from the image of instance A, its container already includes thehello.txtfile.Elastic Container Service! root@eci-b:/# ls bin boot dev etc hello.txt home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@eci-b:/# cat hello.txt hello eci root@eci-b:/#