容器 API¶
以下页面包含构建 BentoContainer 的 Python SDK。
- bentoml.container.build(bento_tag: Tag | str, backend: t.Literal['docker'] = 'docker', image_tag: tuple[str, ...] | None = None, features: t.Sequence[str] | None = None, *, file: PathType | None = ..., context_path: PathType | None = ..., add_host: dict[str, str] | ArgType = ..., build_arg: dict[str, str] | ArgType = ..., cache_from: str | dict[str, str] | ArgType = ..., disable_content_trust: t.Literal[True, False] = ..., iidfile: PathType | None = ..., isolation: t.Literal['default', 'process', 'hyperv'] | None = ..., label: dict[str, str] | ArgType = ..., network: str | None = ..., no_cache: t.Literal[True, False] = ..., output: str | dict[str, str] | ArgType = ..., platform: str | ArgType = ..., progress: t.Literal['auto', 'tty','plain'] = ..., pull: t.Literal[True, False] = ..., quiet: t.Literal[True, False] = ..., secret: str | dict[str, str] | ArgType = ..., ssh: str | ArgType = ..., target: str | ArgType = ..., **kwargs: t.Any) t.Any [source]¶
- bentoml.container.build(bento_tag: Tag | str, backend: t.Literal['buildctl'] = 'docker', image_tag: tuple[str] | None = None, features: t.Sequence[str] | None = None, *, file: PathType | None = ..., context_path: PathType | None = ..., output: str | dict[str, str] | ArgType = ..., progress: t.Literal['auto', 'tty','plain'] | ArgType = ..., trace: PathType | None = ..., local: dict[str, str] | ArgType = ..., frontend: str | None = ..., no_cache: t.Literal[True, False] = ..., export_cache: str | dict[str, str] | ArgType = ..., import_cache: str | dict[str, str] | ArgType = ..., secret: str | dict[str, str] | ArgType = ..., allow: str | ArgType = ..., ssh: str | ArgType = ..., metadata_file: PathType | None = ..., opt: tuple[str, ...] | dict[str, str | tuple[str, ...]] | None = ..., **kwargs: t.Any) t.Any
- bentoml.container.build(bento_tag: Tag | str, backend: t.Literal['buildx'] = 'docker', image_tag: tuple[str] | None = None, features: t.Sequence[str] | None = None, *, file: PathType | None = ..., tag: tuple[str] | None = ..., context_path: PathType = ..., add_host: dict[str, str] | ArgType = ..., attest: str | dict[str, str] | ArgType = ..., allow: str | ArgType = ..., build_arg: dict[str, str] | ArgType = ..., build_context: dict[str, str] | ArgType = ..., builder: str | None = ..., cache_from: str | dict[str, str] | ArgType = ..., cache_to: str | dict[str, str] | ArgType = ..., cgroup_parent: PathType | None = ..., iidfile: PathType | None = ..., label: dict[str, str] | ArgType = ..., load: bool = True, metadata_file: PathType | None = ..., network: str | None = ..., no_cache: t.Literal[True, False] = ..., no_cache_filter: str | dict[str, str] | ArgType = ..., output: str | dict[str, str] | ArgType = ..., platform: str | ArgType = ..., progress: t.Literal['auto', 'tty','plain'] = 'auto', provenance: str | dict[str, str] | ArgType = ..., pull: t.Literal[True, False] = ..., push: t.Literal[True, False] = ..., quiet: t.Literal[True, False] = ..., sbom: str | dict[str, str] | ArgType = ..., secret: str | dict[str, str] | ArgType = ..., shm_size: int | None = ..., ssh: str | ArgType = ..., target: str | None = ..., ulimit: str | dict[str, tuple[int, int]] | ArgType = ..., **kwargs: t.Any) t.Any
- bentoml.container.build(bento_tag: Tag | str, backend: t.Literal['nerdctl'] = 'docker', image_tag: tuple[str] | None = None, features: t.Sequence[str] | None = None, *, file: PathType | None = ..., tag: tuple[str] | None = ..., context_path: PathType = '.', build_arg: dict[str, str] | ArgType = ..., buildkit_host: str | None = ..., cache_from: str | dict[str, str] | ArgType = ..., cache_to: str | dict[str, str] | ArgType = ..., iidfile: PathType | None = ..., ipfs: t.Literal[True, False] = ..., label: dict[str, str] | ArgType = ..., no_cache: t.Literal[True, False] = ..., output: str | dict[str, str] | ArgType = ..., platform: str | ArgType = ..., progress: t.Literal['auto', 'tty', 'plain'] | ArgType = ..., quiet: t.Literal[True, False] = ..., rm: t.Literal[True, False] = ..., secret: str | dict[str, str] | ArgType = ..., ssh: str | ArgType = ..., target: str | None = ..., address: str | None = ..., host: str | None = ..., cgroup_manager: str | None = ..., cni_netconfpath: PathType | None = ..., cni_path: PathType | None = ..., data_root: PathType | None = ..., debug: t.Literal[True, False] = ..., debug_full: t.Literal[True, False] = ..., hosts_dir: str | ArgType = ..., insecure_registry: t.Literal[True, False] = ..., namespace: str | None = ..., snapshotter: str | None = ..., storage_driver: str | None = ..., **kwargs: t.Any) t.Any
- bentoml.container.构建(bento_tag: Tag | 字符串, 后端: t.Literal['podman'] = 'docker', 镜像标签: 元组[字符串] | None = None, 特性: t.Sequence[字符串] | None = None, *, 文件: PathType | None = ..., 上下文路径: PathType | None = ..., 添加主机: 字典[字符串, 字符串] | ArgType = ..., 所有平台: t.Literal[True, False] = ..., 注解: 字典[字符串, 字符串] | ArgType = ..., 标签: 字典[字符串, 字符串] | ArgType = ..., 架构: 字符串 | None = ..., 认证文件: PathType | None = ..., 构建参数: 字典[字符串, 字符串] | ArgType = ..., 构建上下文: 字典[字符串, 字符串] | ArgType = ..., 缓存来源: 字符串 | None = ..., 缓存目标: 字符串 | None = ..., 缓存存活时间: 字符串 | None = ..., 添加能力: 字符串 | ArgType = ..., 删除能力: 字符串 | ArgType = ..., 证书目录: PathType | None = ..., cgroup父级: PathType | None = ..., cgroupns: 字符串 | None = ..., 预处理器标志: ArgType = ..., CPU周期: 整数 | None = ..., CPU配额: 整数 | None = ..., CPU份额: 整数 | None = ..., cpuset_cpus: 字符串 | None = ..., cpuset_mems: 字符串 | None = ..., 凭据: 字符串 | 字典[字符串, 字符串] | ArgType = ..., 解密密钥: 字符串 | 字典[字符串, 字符串] | ArgType = ..., 设备: 字符串 | ArgType = ..., 禁用压缩: t.Literal[True, False] = ..., DNS: 字符串 | None = ..., DNS选项: 字符串 | ArgType = ..., DNS搜索域: 字符串 | ArgType = ..., 环境变量: 字符串 | 字典[字符串, 字符串] | ArgType = ..., 强制删除: t.Literal[True, False] = ..., 格式: 字符串 | t.Literal['docker', 'oci'] | None = ..., 钩子目录: 字符串 | ArgType = ..., HTTP代理: t.Literal[True, False] = ..., 身份标签: t.Literal[True, False] = ..., 忽略文件: PathType | None = ..., 镜像ID文件: PathType | None = ..., IPC: 字符串 | PathType | None = ..., 隔离: 字符串 | None = ..., 并行任务数: 整数 | None = ..., 层: t.Literal[True, False] = ..., 日志文件: PathType | None = ..., 清单: 字符串 | None = ..., 内存: 字符串 | None = ..., 内存交换: 字符串 | None = ..., 网络: 字符串 | None = ..., 无缓存: t.Literal[True, False] = ..., 无主机: t.Literal[True, False] = ..., 省略历史: t.Literal[True, False] = ..., 操作系统: 字符串 | None = ..., 操作系统特性: 字符串 | None = ..., 操作系统版本: 字符串 | None = ..., PID: PathType | None = ..., 平台: 字符串 | ArgType = ..., 输出: 字符串 | 字典[字符串, 字符串] | ArgType = ..., 拉取: t.Literal[True, False, 'always', 'true', 'missing', 'never', 'false', 'newer'] = False, 静默: t.Literal[True, False] = ..., 删除中间容器: t.Literal[True, False] = ..., 重试: 整数 | None = ..., 重试延迟: 整数 | None = ..., 运行时: PathType | None = ..., 运行时标志: 字符串 | 字典[字符串, 字符串] | ArgType = ..., 密钥: 字符串 | 字典[字符串, 字符串] | ArgType = ..., 安全选项: 字符串 | ArgType = ..., 共享内存大小: 字符串 | None = ..., 签名者: 字符串 | None = ..., 跳过未使用阶段: t.Literal[True, False] = ..., 压缩层: t.Literal[True, False] = ..., 压缩所有层: t.Literal[True, False] = ..., SSH: 字符串 | ArgType = ..., 标准输入: t.Literal[True, False] = ..., 目标: 字符串 | None = ..., 时间戳: 整数 | None = ..., TLS验证: t.Literal[True, False] = ..., ulimit: 字符串 | 字典[字符串, 元组[整数, 整数]] | ArgType = ..., 取消设置环境变量: 字符串 | ArgType = ..., userns: 字符串 | None = ..., userns_gid_map: 字符串 | 元组[字符串, 字符串, 字符串] | None = ..., userns_gid_map_group: 字符串 | None = ..., userns_uid_map: 字符串 | 元组[字符串, 字符串, 字符串] | None = ..., userns_uid_map_user: 字符串 | None = ..., uts: 字符串 | None = ..., 变体: 字符串 | None = ..., 卷: 字符串 | 元组[字符串, 字符串, 字符串] | None = ..., **kwargs: t.任意类型) t.任意类型
- bentoml.container.build(bento_tag: Tag | str, backend: t.Literal['buildah'] = 'docker', image_tag: tuple[str] | None = None, features: t.Sequence[str] | None = None, *, context_path: PathType = ..., file: PathType | None = ..., tag: tuple[str] | None = ..., add_host: dict[str, str] | ArgType = ..., annotation: dict[str, str] | ArgType = ..., label: dict[str, str] | ArgType = ..., arch: str | None = ..., authfile: PathType | None = ..., build_arg: dict[str, str] | ArgType = ..., cache_from: str | None = ..., cap_add: str | ArgType = ..., cap_drop: str | ArgType = ..., cert_dir: PathType | None = ..., cgroup_parent: PathType | None = ..., cni_config_dir: PathType | None = ..., cni_plugin_path: PathType | None = ..., compress: t.Literal[True, False] = ..., cpu_period: int | None = ..., cpu_quota: int | None = ..., cpu_shares: int | None = ..., cpuset_cpus: str | None = ..., cpuset_mems: str | None = ..., creds: str | dict[str, str] | ArgType = ..., decryption_key: str | dict[str, str] | ArgType = ..., device: str | ArgType = ..., disable_compression: t.Literal[True, False] = ..., dns: str | None = ..., dns_option: str | ArgType = ..., dns_search: str | ArgType = ..., force_rm: t.Literal[True, False] = ..., format: str | t.Literal['docker', 'oci'] | None = ..., http_proxy: t.Literal[True, False] = ..., ignorefile: PathType | None = ..., iidfile: PathType | None = ..., ipc: str | PathType | None = ..., isolation: str | None = ..., jobs: int | None = ..., layers: t.Literal[True, False] = ..., logfile: PathType | None = ..., manifest: str | None = ..., memory: str | None = ..., memory_swap: str | None = ..., network: str | None = ..., no_cache: t.Literal[True, False] = ..., os: str | None = ..., pid: PathType | None = ..., platform: str | ArgType = ..., pull: t.Literal[True, False] = ..., pull_always: t.Literal[True, False] = ..., pull_never: t.Literal[True, False] = ..., quiet: t.Literal[True, False] = ..., rm: t.Literal[True, False] = ..., runtime: PathType | None = ..., runtime_flag: str | dict[str, str] | ArgType = ..., secret: str | dict[str, str] | ArgType = ..., security_opt: str | ArgType = ..., shm_size: str | None = ..., sign_by: str | None = ..., squash: t.Literal[True, False] = ..., ssh: str | ArgType = ..., stdin: t.Literal[True, False] = ..., target: str | None = ..., timestamp: int | None = ..., tls_verify: t.Literal[True, False] = ..., ulimit: str | dict[str, tuple[int, int]] | ArgType = ..., userns: str | None = ..., userns_gid_map_group: str | None = ..., userns_uid_map_user: str | None = ..., uts: str | None = ..., variant: str | None = ..., volume: str | tuple[str, str, str] | None = ..., **kwargs: t.Any) t.Any
构建给定的 BentoML 为 OCI 兼容镜像。
import bentoml bento = bentoml.get("pytorch_vgg:latest") bentoml.container.build(bento, backend='podman', features=["grpc", "tracing"])
- 参数:
bento_tag – 格式为
NAME:VERSION
的 Bento 标签backend –
用于构建镜像的后端。当前支持的构建后端包括
docker
、podman
、buildah
、nerdctl
、buildctl
和buildx
。注意
buildx
是docker buildx build
的语法糖。详情请参阅 https://docs.dockerd.com.cn/build/。这样做的原因是buildx
曾经是bentoml containerize
的默认行为。image_tag – 可选的额外镜像标签,应用于构建的镜像。
features – 可选的包含在容器文件中的功能。有关附加的 BentoML 功能,请参阅 概念/bento:Python 包。
**kwargs – 传递给构建后端的可选关键字参数。请参考上面的重载定义以了解每个后端支持的参数。
- bentoml.container.health(backend: DefaultBuilder) bool [source]¶
- bentoml.container.health(backend: str) bool
检查后端是否健康。
- 参数:
backend – 后端的名称。
注意
如果给定的后端类型是 OCIBuilder 且未注册,请确保使用
bentoml.container.register_backend
注册它。- 返回:
如果后端健康则返回 True,否则返回 False。
- bentoml.container.get_backend(backend: DefaultBuilder) OCIBuilder [source]¶
- bentoml.container.get_backend(backend: str) OCIBuilder
获取给定后端。
- 抛出异常:
ValueError – 如果给定的后端在后端注册表中不可用。
- bentoml.container.register_backend(backend: str, *, buildkit_support: bool, health: t.Callable[[], bool], construct_build_args: t.Callable[P, Arguments], binary: str | None = None, build_cmd: t.Sequence[str] | None = None, env: dict[str, str] | None = None)[source]¶
注册自定义后端,并提供构建和健康检查实现。
- 参数:
backend – 后端的名称。
buildkit_support – 后端是否支持 BuildKit。
health – 健康检查实现。这是一个不接受参数并返回布尔值的可调用对象。
construct_build_args – 这是一个接受可能的后端关键字参数并返回命令行参数列表的可调用对象。
env – 此 OCI 构建器实现的默认环境变量字典。
binary – 可选的二进制文件路径。如果未提供,将使用后端名称作为二进制文件。请确保该二进制文件在你的
PATH
中。build_cmd – 可选的构建命令。如果未提供,命令将为 ‘build’。
示例
from bentoml.container import register_backend register_backend( "lima", binary=shutil.which("limactl"), buildkit_support=True, health=buildx_health, construct_build_args=buildx_build_args, env={"DOCKER_BUILDKIT": "1"}, )
实用函数¶
我们还提供了一些实用函数来帮助你自定义容器化过程。
- bentoml.container.get_containerfile(bento_tag: Tag | str, output_path: str | None = None, enable_buildkit: bool = True, features: t.Sequence[str] | None = None, _bento_store: BentoStore = <simple_di.providers.SingletonFactory object>)[source]¶
返回给定 Bento 的生成的容器文件。
注意,Bento 内的容器文件 (Dockerfile) 是极简的,而此实用函数返回的是 bentoml containerize 将使用的容器文件。
注意
如果未指定
output_path
,则容器文件的内容将打印到sys.stderr
。如果提供了此参数,则最终容器文件将写入到指定的路径。- 参数:
bento_tag – 给定的 bento 标签。
output_path – 可选的输出路径,用于写入最终容器文件。请注意,如果
output_path
是一个目录,则目标文件将是output_path + os.sep + "<bento_tag>.dockerfile"
。enable_buildkit – 容器文件是否包含 BuildKit 语法。
features – 可选的包含在容器文件中的功能。有关附加的 BentoML 功能,请参阅 概念/bento:Python 包。