第 1 章 创建模型镜像并将其推送到容器镜像 registry


您可以创建一个 modelcar 镜像,其中包含您可以使用 Red Hat AI Inference Server 部署的语言模型。

要创建 modelcar 镜像,请从 Hugging Face 下载模型,然后将其打包到容器镜像中,并将 modelcar 容器推送到镜像 registry。

先决条件

  • 已安装 Python 3.11 或更高版本。
  • 已安装 Podman 或 Docker。
  • 您可以访问互联网来从 Hugging Face 下载模型。
  • 您已配置了可将镜像推送到并登录的容器镜像 registry。

流程

  1. 创建 Python 虚拟环境并安装 huggingface_hub Python 库:

    python3 -m venv venv && \
    source venv/bin/activate && \
    pip install --upgrade pip && \
    pip install huggingface_hub
  2. 创建模型下载程序 Python 脚本:

    vi download_model.py
  3. download_model.py 文件中添加以下内容,根据需要调整 model_repo 的值:

    from huggingface_hub import snapshot_download
    
    # Specify the Hugging Face repository containing the model
    model_repo = "ibm-granite/granite-3.1-2b-instruct"
    snapshot_download(
        repo_id=model_repo,
        local_dir="/models",
        allow_patterns=["*.safetensors", "*.json", "*.txt"],
    )
  4. 为 modelcar 创建 Dockerfile

    FROM registry.access.redhat.com/ubi9/python-311:latest as base
    
    USER root
    
    RUN pip install huggingface-hub
    
    # Download the model file from Hugging Face
    COPY download_model.py .
    
    RUN python download_model.py
    
    # Final image containing only the essential model files
    FROM registry.access.redhat.com/ubi9/ubi-micro:9.4
    
    # Copy the model files from the base container
    COPY --from=base /models /models
    
    USER 1001
  5. 构建 modelcar 镜像:

    podman build . -t modelcar-example:latest --platform linux/amd64

    输出示例

    Successfully tagged localhost/modelcar-example:latest

  6. 将 modelcar 镜像推送到容器 registry。例如:

    $ podman push modelcar-example:latest quay.io/<your_model_registry>/modelcar-example:latest

    输出示例

    Getting image source signatures
    Copying blob b2ed7134f853 done
    Copying config 4afd393610 done
    Writing manifest to image destination
    Storing signatures

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2026 Red Hat
返回顶部