1.5. 将您的 AI 模型打包到 OCI 镜像中


您可以将模型打包成 OCI 镜像,并使用 ModelCar 方法来帮助设置离线环境。使用 ModelCar 方法时,您的模型可以像任何其他容器镜像一样嵌入。

注意

如果您已经有 S3 兼容对象存储或配置的持久性卷声明,您可以将 AI 模型上传到这些资源,但只有 ModelCar 方法经过测试并被支持。

先决条件

  • 有对机器的 root 用户访问权限。
  • 已安装 OpenShift CLI (oc)。
  • podman 已安装。
  • 您的模型可以使用。
  • 您可以了解以下关于构建适合 vLLM 模型服务器的 OCI 镜像的"如何构建 ModelCar 容器"部分中的概念,并在 OpenShift AI 中构建和部署 ModelCar 容器

    注意

    确切的目录结构取决于模型服务器。以下示例使用一个与 OpenVINO Model Server OVMS 兼容的 ResNet-50 模型的 Containerfile。OVMS 通常不需要额外的硬件加速器。

流程

  1. 使用兼容模型和模型服务器准备 Containerfile。

    带有与 OVMS 搭配使用的 ResNet-50 模型的 Containerfile 示例

    FROM registry.access.redhat.com/ubi9/ubi-minimal:latest
    RUN microdnf install -y wget && microdnf clean all
    RUN mkdir -p /models/1 && chmod -R 755 /models/1
    RUN wget -q -P /models/1 \
      https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/resnet50-binary-0001/FP32-INT1/resnet50-binary-0001.bin \
      https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/resnet50-binary-0001/FP32-INT1/resnet50-binary-0001.xml
    Copy to Clipboard Toggle word wrap

  2. 运行以下命令,将 IMAGE_REF 环境变量设置为简化您的过程:

    $ IMAGE_REF=<ovms-resnet50:test> 
    1
    Copy to Clipboard Toggle word wrap
    1
    <ovms-resnet50:test > 替换为镜像引用的名称。在本例中,使用 &lt ;repo:tag& gt; 格式。您的镜像引用名称特定于您的用例。
  3. 运行以下命令来构建 Containerfile:

    $ sudo podman build -t $IMAGE_REF 
    1
    Copy to Clipboard Toggle word wrap
    1
    由于 CRI-O 和 Podman 共享存储,需要使用 sudo 使镜像成为根容器存储的一部分,并可供 MicroShift 使用。

    输出示例

    STEP 1/4: FROM registry.access.redhat.com/ubi9/ubi-minimal:latest
    Trying to pull registry.access.redhat.com/ubi9/ubi-minimal:latest...
    Getting image source signatures
    Checking if image destination supports signatures
    Copying blob 533b69cfd644 done   |
    Copying blob 863e9a7e2102 done   |
    Copying config 098048e6f9 done   |
    Writing manifest to image destination
    Storing signatures
    STEP 2/4: RUN microdnf install -y wget && microdnf clean all
    << SNIP >>
    --> 4c74352ad42e
    STEP 3/4: RUN mkdir -p /models/1 && chmod -R 755 /models/1
    --> bfd31acb1e81
    STEP 4/4: RUN wget -q -P /models/1   https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/resnet50-binary-0001/FP32-INT1/resnet50-binary-0001.bin   https://storage.openvinotoolkit.org/repositories/open_model_zoo/2022.1/models_bin/2/resnet50-binary-0001/FP32-INT1/resnet50-binary-0001.xml
    COMMIT ovms-resnet50:test
    --> 375b265c1c4b
    Successfully tagged localhost/ovms-resnet50:test
    375b265c1c4bc6f0a059c8739fb2b3a46e1b563728f6d9c51f26f29bb2c87
    Copy to Clipboard Toggle word wrap

  4. 可选:运行以下命令将 Containerfile 推送到 registry 中:

    $ sudo podman push $IMAGE_REF
    Copy to Clipboard Toggle word wrap
    重要

    对于离线用例,请包括除 latest 以外的标签。如果使用 latest 标签,则获取和设置模型的容器将被配置为 imagePullPolicy: 参数,并且忽略本地镜像。如果您使用除 latest 之外的任何其他标签,则 imagePullPolicy: 参数将设置为 IfNotPresent

验证

  • 运行以下命令验证镜像是否存在:

    $ sudo podman images ovms-resnet50
    Copy to Clipboard Toggle word wrap

    输出示例

    REPOSITORY                TAG   IMAGE ID        CREATED         SIZE
    localhost/ovms-resnet50   test  375b265c1c4b    3 minutes ago   136 MB
    Copy to Clipboard Toggle word wrap

后续步骤

  • 配置模型运行时。
  • 确认您的 AI 模型已准备好推断。
  • 向模型服务器发出请求。
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat