

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 SOCI 加快容器启动速度
<a name="soci-indexing"></a>

SOCI（Seekable 开放容器计划）索引允许在我们的 [亚马逊 SageMaker Studio](studio-updated.md) A [ma SageMaker ](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/what-is-sagemaker-unified-studio.html) zon Unified Studio 中延迟加载自定义容器镜像。SOCI 将您的自定义[自带映像（BYOI）](studio-updated-byoi.md)容器的启动时间显著缩短了大约 30-70%。延迟改善因镜像大小、托管实例可用性和其他应用程序依赖关系而异。SOCI 创建了一个索引，该索引允许仅使用必要的组件启动容器，并根据需要按需获取其他文件。

SOCI 解决了容器启动时间缓慢的问题，这种启动时间会中断自定义映像的迭代机器学习 (ML) 开发工作流程。随着机器学习工作负载变得越来越复杂，容器镜像越来越大，从而造成启动延迟，阻碍了开发周期。

**Topics**
+ [主要优势](#soci-indexing-key-benefits)
+ [SOCI 索引的工作原理](#soci-indexing-how-works)
+ [架构组件](#soci-indexing-architecture-components)
+ [支持的工具](#soci-indexing-supported-tools)
+ [SOCI 索引的权限](soci-indexing-setup.md)
+ [使用 nerdctl 和 SOCI CLI 示例创建 SOCI 索引](soci-indexing-example-create-indexes.md)
+ [将社交索引图像与 Studio 示例集成](soci-indexing-example-integrate-studio.md)

## 主要优势
<a name="soci-indexing-key-benefits"></a>
+ **更快的迭代周期**：根据镜像和实例类型，减少容器启动时间
+ **通用优化**：将性能优势扩展到 Studio 中的所有自定义 BYOI 容器

## SOCI 索引的工作原理
<a name="soci-indexing-how-works"></a>

SOCI 会创建专门的元数据索引，用于映射容器镜像的内部文件结构。该索引允许访问单个文件，而无需下载整个图像。SOCI 索引作为符合 OCI（开放容器倡议）的构件存储在 A [mazon ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html) 中，并链接到您的原始容器映像，从而保留图像摘要和签名有效性。

在 Studio 中启动容器时，系统会使用 SOCI 索引来识别和下载启动所需的基本文件。其他组件可根据您的应用程序需要并行获取。

## 架构组件
<a name="soci-indexing-architecture-components"></a>
+ **原始容器图片**：您的基本容器存储在 Amazon ECR 中
+ **SOCI 索引工件**：映射图像文件结构的元数据
+ **OCI 图像索引清单**：链接您的原始图像和 SOCI 索引
+ **Finch 容器运行时**：启用与 Studio 的延迟加载集成

## 支持的工具
<a name="soci-indexing-supported-tools"></a>


| 工具 | 集成 | 
| --- | --- | 
| nerdctl | 需要安装 containerd | 
| Finch CLI | 原生 SOCI 支持 | 
| Docker \$1 SOCI CLI | 需要其他工具 | 

**Topics**
+ [主要优势](#soci-indexing-key-benefits)
+ [SOCI 索引的工作原理](#soci-indexing-how-works)
+ [架构组件](#soci-indexing-architecture-components)
+ [支持的工具](#soci-indexing-supported-tools)
+ [SOCI 索引的权限](soci-indexing-setup.md)
+ [使用 nerdctl 和 SOCI CLI 示例创建 SOCI 索引](soci-indexing-example-create-indexes.md)
+ [将社交索引图像与 Studio 示例集成](soci-indexing-example-integrate-studio.md)

# SOCI 索引的权限
<a name="soci-indexing-setup"></a>

在将 SOCI 索引与或 Amazon Unified Studio 一起使用之前，请为容器映像创建 SOCI 索引并将其存储在 [亚马逊 SageMaker Studio](studio-updated.md) A [ma SageMaker ](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/what-is-sagemaker-unified-studio.html) zon ECR 中。

**Topics**
+ [先决条件](#soci-indexing-setup-prerequisites)
+ [所需的 IAM 权限](#soci-indexing-setup-iam-permissions)

## 先决条件
<a name="soci-indexing-setup-prerequisites"></a>
+ AWS 账户 具有管理权限的 [AWS Identity and Access Management](https://docs.aws.amazon.com/IAM/latest/UserGuide/getting-started.html)(IAM) 角色
  + [Amazon ECR](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html)
  + [亚马逊 SageMaker AI](https://docs.aws.amazon.com/sagemaker/latest/dg/gs.html)
+ 用于@@ [存储容器镜像的 Amazon ECR 私有存储库](https://docs.aws.amazon.com/AmazonECR/latest/userguide/Repositories.html)
+ [AWS CLI v2.0\$1](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) 版本配置了适当的凭据
+ 以下容器工具：
  + 必需：[社交快照器](https://github.com/awslabs/soci-snapshotter)
  + 选项：
    + [nerdctl](https://github.com/containerd/nerdctl)
    + [雀科](https://github.com/runfinch/finch)

## 所需的 IAM 权限
<a name="soci-indexing-setup-iam-permissions"></a>

您的 IAM 角色需要以下权限：
+ 创建和管理 SageMaker AI 资源（域、图像、应用程序配置）。
  + 您可以使用[SageMakerFullAccess](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AmazonSageMakerFullAccess.html) AWS 托管策略。有关权限的更多详细信息，请参阅[AWS 托管策略： AmazonSageMakerFullAccess](security-iam-awsmanpol.md#security-iam-awsmanpol-AmazonSageMakerFullAccess)。
+ 将@@ [图像推送到 Amazon ECR 私有存储库的 IAM 权限](https://docs.aws.amazon.com/AmazonECR/latest/userguide/image-push-iam.html)。

# 使用 nerdctl 和 SOCI CLI 示例创建 SOCI 索引
<a name="soci-indexing-example-create-indexes"></a>

以下页面提供了有关如何使用 nerdctl 和 SOCI CLI 创建 SOCI 索引的示例。

**创建 SOCI 索引示例**

1. 首先为接下来的 AWS CLI 命令设置变量。以下是设置变量的示例。

   ```
   ACCOUNT_ID="111122223333"
   REGION="us-east-1"
   REPOSITORY_NAME="repository-name"
   ORIGINAL_IMAGE_TAG="original-image-tag"
   SOCI_IMAGE_TAG="soci-indexed-image-tag"
   ```

   变量定义：
   + `ACCOUNT_ID`是你的 AWS 账户 身份证
   + `REGION`是您的 AWS 区域 Amazon ECR 私有注册表
   + `REPOSITORY_NAME`是您的 Amazon ECR 私有注册表的名称
   + `ORIGINAL_IMAGE_TAG`是你原始图片的标签
   + `SOCI_IMAGE_TAG`是你的社交索引图片的标签

1. 安装所需的工具：

   ```
   # Install SOCI CLI, containerd, and nerdctl
   sudo yum install soci-snapshotter
   sudo yum install containerd jq  
   sudo systemctl start soci-snapshotter
   sudo systemctl restart containerd
   sudo yum install nerdctl
   ```

1. 设置您的注册表变量：

   ```
   REGISTRY_USER=AWS
   REGISTRY="$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com"
   ```

1. 导出您的地区并向 Amazon ECR 进行身份验证：

   ```
   export AWS_REGION=$REGION
   REGISTRY_PASSWORD=$(/usr/local/bin/aws ecr get-login-password --region $AWS_REGION)
   echo $REGISTRY_PASSWORD | sudo nerdctl login -u $REGISTRY_USER --password-stdin $REGISTRY
   ```

1. 提取您的原始容器镜像：

   ```
   sudo nerdctl pull $REGISTRY/$REPOSITORY_NAME:$ORIGINAL_IMAGE_TAG
   ```

1. 创建 SOCI 索引：

   ```
   sudo nerdctl image convert --soci $REGISTRY/$REPOSITORY_NAME:$ORIGINAL_IMAGE_TAG $REGISTRY/$REPOSITORY_NAME:$SOCI_IMAGE_TAG
   ```

1. 推送社交索引图片：

   ```
   sudo nerdctl push --platform linux/amd64 $REGISTRY/$REPOSITORY_NAME:$SOCI_IMAGE_TAG
   ```

此过程会为您的 ECR 存储库中的原始容器映像创建两个构件：
+ SOCI 索引-支持延迟加载的元数据
+ 图片索引清单-符合 OCI 的清单

# 将社交索引图像与 Studio 示例集成
<a name="soci-indexing-example-integrate-studio"></a>

要在 Studio 中使用社交索引图像，必须引用 Soci-Indexed 图像标签，而不是原始容器图像标签。使用您在 SOCI 转换过程中指定的标签（例如，`SOCI_IMAGE_TAG`在[使用 nerdctl 和 SOCI CLI 示例创建 SOCI 索引](soci-indexing-example-create-indexes.md)）。

**整合社交索引图片示例**

1. 首先为接下来的 AWS CLI 命令设置变量。以下是设置变量的示例。

   ```
   ACCOUNT_ID="111122223333"
   REGION="us-east-1"
   IMAGE_NAME="sagemaker-image-name"
   IMAGE_CONFIG_NAME="sagemaker-image-config-name"
   ROLE_ARN="your-role-arn"
   DOMAIN_ID="domain-id"
   SOCI_IMAGE_TAG="soci-indexed-image-tag"
   ```

   变量定义：
   + `ACCOUNT_ID`是你的 AWS 账户 身份证
   + `REGION`是您的 AWS 区域 Amazon ECR 私有注册表
   + `IMAGE_NAME`是你的 SageMaker 图片的名字
   + `IMAGE_CONFIG_NAME`是您的 SageMaker 映像配置的名称
   + `ROLE_ARN`是您的执行角色的 ARN，其权限列在[必需](soci-indexing-setup.md#soci-indexing-setup-iam-permissions)的 IAM 权限中
   + `DOMAIN_ID`是[域名 ID](https://docs.aws.amazon.com/sagemaker/latest/dg/domain-view.html)
**注意**  
如果您要将图像附加到 SageMaker Unified Studio 项目，并且需要说明要使用哪个域，请参阅[查看与您的项目关联的 SageMaker AI 域详细信息](https://docs.aws.amazon.com/sagemaker-unified-studio/latest/userguide/view-project-details.html#view-project-details-smai-domain)。
   + `SOCI_IMAGE_TAG`是你的社交索引图片的标签

1. 导出您所在的地区：

   ```
   export AWS_REGION=$REGION
   ```

1. 创建 SageMaker 镜像：

   ```
   aws sagemaker create-image \
       --image-name "$IMAGE_NAME" \
       --role-arn "$ROLE_ARN"
   ```

1. 使用您的 SOCI 索引 URI 创建 SageMaker 图像版本：

   ```
   IMAGE_INDEX_URI="$ACCOUNT_ID.dkr.ecr.$REGION.amazonaws.com/$IMAGE_NAME:$SOCI_IMAGE_TAG"
   
   aws sagemaker create-image-version \
       --image-name "$IMAGE_NAME" \
       --base-image "$IMAGE_INDEX_URI"
   ```

1. 创建应用程序映像配置并更新您的 Amazon SageMaker AI 域以包含应用程序的自定义映像。你可以为基于 Code-OSS、Visual Studio 代码-开源（代码编辑器）和 JupyterLab应用程序的代码编辑器执行此操作。选择下面的应用程序选项以查看步骤。

------
#### [ Code Editor ]

   为代码编辑器创建应用程序映像配置：

   ```
   aws sagemaker create-app-image-config \
       --app-image-config-name "$IMAGE_CONFIG_NAME" \
       --code-editor-app-image-config '{ "FileSystemConfig": { "MountPath": "/home/sagemaker-user", "DefaultUid": 1000, "DefaultGid": 100 } }'
   ```

   更新您的 Amazon SageMaker AI 域名以包含代码编辑器的自定义图片：

   ```
   aws sagemaker update-domain \
       --domain-id "$DOMAIN_ID" \
       --default-user-settings '{
           "CodeEditorAppSettings": {
           "CustomImages": [{
               "ImageName": "$IMAGE_NAME", 
               "AppImageConfigName": "$IMAGE_CONFIG_NAME"
           }]
       }
   }'
   ```

------
#### [ JupyterLab ]

   为以下各项创建应用程序映像配置 JupyterLab：

   ```
   aws sagemaker create-app-image-config \
       --app-image-config-name "$IMAGE_CONFIG_NAME" \
       --jupyter-lab-app-image-config '{ "FileSystemConfig": { "MountPath": "/home/sagemaker-user", "DefaultUid": 1000, "DefaultGid": 100 } }'
   ```

   更新您的 Amazon SageMaker AI 域名以包含以下内容的自定义图片 JupyterLab：

   ```
   aws sagemaker update-domain \
       --domain-id "$DOMAIN_ID" \
       --default-user-settings '{
           "JupyterLabAppSettings": {
           "CustomImages": [{
               "ImageName": "$IMAGE_NAME", 
               "AppImageConfigName": "$IMAGE_CONFIG_NAME"
           }]
       }
   }'
   ```

------

1. 更新域名以包含您的自定义映像后，您可以使用您的自定义映像在 Studio 中创建应用程序。当您[在 Studio 中启动自定义映像](studio-updated-byoi-how-to-launch.md)确保使用的是您的自定义镜像时。