Skip to content

Mac 专用容器平台 - Apple Container

本文将介绍 Apple Container 的主要特性、核心功能、架构、CLI命令、MCP Server集成。

什么是 Apple Container?

Apple Container 是一个专为 Mac (Apple Silicon 芯片)设计的原生容器化平台,用于创建和运行 Linux 容器,无需第三方虚拟化程序或复杂配置。它的命令行设计与 Docker 非常相似,对开发者友好。与传统方案(如 Docker Desktop、OrbStack 或 Podman,通常在单一虚拟机中运行多个容器)相比,它更轻量。

注意:Apple Container 目前处于积极开发中。功能和需求可能在版本之间发生变化。

主要特性

  • 原生性能:专为 Apple Silicon 设计,无性能损失
  • OCI 兼容性:与现有容器生态系统完全兼容
  • 注册表支持:可从任何标准容器注册表中拉取
  • 多架构:支持 ARM64 和 AMD64 架构
  • 轻量级:使用轻量级虚拟化,而不是重型模拟
  • Swift 原生:100% Swift 实现,以实现最佳的 macOS 集成

系统要求

  • 硬件:Apple Silicon Mac(M1、M2、M3 或更新型号)
  • 操作系统:macOS 15 Sequoia 或更高版本,建议使用 macOS 26 Tahoe 或更高版本以获得最佳体验。

核心功能

容器管理

  • 创建、运行、停止和管理容器
  • 交互式 Shell 会话
  • 资源限制和控制
  • 网络和卷管理

镜像操作

  • 从 Dockerfile 构建镜像

  • 从容器注册表中拉取和推送

  • 多架构镜像支持

  • 本地图像管理

注册中心

  • 登录容器中心
  • 支持公共和私有注册表
  • 默认注册表配置

资源与链接

安装 CLI 工具

安装

借助 Homebrew 来快速安装

brew install --cask container

启动服务

安装完成后,可通过以下命令启动服务

container system start

该命令会启动容器服务,并自动安装一个由 Kata Containers 提供的 Linux 内核。

升级

首先停止并卸载现有的 container-k 标志会保留你的用户数据),然后启动系统服务

container system stop
uninstall-container.sh -k
container system start

卸载

使用 uninstall-container.sh 脚本删除 container工具。带上 -d 参数将同时删除用户数据

uninstall-container.sh -d

命令帮助

使用 --help-h 选项可查看container及其子命令的帮助。

container --help
container run --help
container system --help

使用示例

Container 容器

运行容器

# 以交互模式运行容器
container run --name ubuntu -it --rm ubuntu:latest bash
# 以后台模式运行容器
container run --name ubuntu -d ubuntu:latest bash

为容器分配内存和CPU

container run --cpus 8 --memory 32g

与容器共享主机文件

-v--volume 选项可与容器共享主机文件,-v 可以有多个。

container run --volume /opt/assets:/content/assets docker.io/python:alpine ls -l /content/assets

查看容器清单

container ls
container ls --all        # 同时查看运行中和停止的容器

查看容器日志

container logs ubuntu
container logs -f ubuntu  # 跟踪日志

查看容器详情

container inspect ubuntu  # 查看容器详情

调试容器

container run --name redis  -it --rm redis /bin/sh
  • -it 打开交互式终端,退出(exit)终端时容器将停止
  • --rm 容器停止时删除容器

进入容器执行命令

container exec -it ubuntu bash

停止容器

container stop ubuntu
container stop --all       # 停止所有容器

删除容器

container delete ubuntu

Image 镜像

查找镜像

container 目前没有提供查找镜像命令,需要前往Docker 官方仓库 自行搜索镜像。

拉取镜像

container image pull ubuntu:latest

启动构建器

container builder start    # 启动构建器
container builder status   # 查看构建器状态
container builder stop     # 停止构建器
container builder delete   # 删除构建器

为构建器配置内存和 CPU

container builder start --cpus 8 --memory 32g

构建镜像

container build -f docker/Dockerfile.prod -t my-app:prod .

构建跨平台镜像

Dockerfile 构建镜像

container build --arch arm64 --arch amd64 web-test --file Dockerfile .

推送镜像

container registry login registry.example.com
container image tag web-test registry.example.com/myorg/web-test:latest # 给镜像打标签
container image push registry.example.com/web-test:latest

删除本地镜像

container image delete registry.example.com/web-test:latest
container image prune  # 删除未使用镜像

导出与导入镜像

container image save registry.example.com/web-test:latest -o web-test.tar.gz  # 导出 OCI兼容镜像 tar 包
container image load -i ./web.test.tar.gz # 导入镜像 tar 包

获取镜像详情

使用 inspect 命令,并将结果发送到 jq 命令,以获取您指定的图像或容器的格式化 JSON 输出:

container image inspect web-test | jq

使用 list 命令与 --format 选项来显示所有镜像或容器的信息。在这个示例中, --all 选项显示停止和运行的容器,而 jq 选择每个运行容器的 IP 地址:

bash
% container ls --format json --all | jq '.[] | select ( .status == "running" ) | [ .configuration.id, .networks[0].address ]'
[
  "my-web-server",
  "192.168.64.3/24"
]
[
  "buildkit",
  "192.168.64.2/24"
]

System 系统

查看系统日志

container system logs -f

查看可用系统属性

使用 container system property list 显示所有可用属性的详细信息:

container system property ls
ID                 TYPE    VALUE                                     DESCRIPTION
build.rosetta      Bool    *undefined*                               Build amd64 images on arm64 using Rosetta, instead of QEMU.
dns.domain         String  *undefined*                               If defined, the local DNS domain to use for containers with unqualified names.
image.builder      String  ghcr.io/apple/container-builder-shim/...  The image reference for the utility container that `container build` uses.
image.init         String  ghcr.io/apple/containerization/vminit...  The image reference for the default initial filesystem image.
kernel.binaryPath  String  opt/kata/share/kata-containers/vmlinu...  If the kernel URL is for an archive, the archive member pathname for the kernel file.
kernel.url         String  https://github.com/kata-containers/ka...  The URL for the kernel file to install, or the URL for an archive containing the kernel file.
network.subnet     String  *undefined*                               Default subnet for IP allocation (used on macOS 15 only).
registry.domain    String  docker.io                                 The default registry to use for image references that do not specify a registry.

禁用为构建启用 Rosetta

如果希望在 Apple Silicon Mac 上构建容器时防止使用 Rosetta 翻译:

container system property set build.rosetta false

当您希望确保构建仅生成原生 arm64 镜像并避免任何 x86_64 模拟时,这很有用。

MCP Server 集成

container-mcp-server@latest 为 AI 助手和自动化工作流程提供 Apple container CLI 集成。

json
{
  "mcpServers": {
    "container": {
      "command": "npx",
      "args": ["-y", "container-mcp-server@latest"]
    }
  }
}

底层技术概述

架构

Apple Container 使用 Containerization Swift 包进行低级容器、镜像和进程管理。它利用 Apple 的原生虚拟化框架实现高效的资源利用和安全性隔离。

什么是容器?

容器是一种将应用程序及其依赖项打包成一个独立单元的方式。在运行时,容器提供与主机以及其他同处环境的容器之间的隔离,使应用程序能够在各种环境中安全高效地运行。

容器化是贯穿整个软件生命周期的关键服务器端技术:

  • 后端开发人员在其个人系统上使用容器来为应用程序创建可预测的执行环境,并在更接近数据中心运行条件的环境下开发和测试应用程序。
  • 持续集成和部署(CI/CD)系统使用容器化来执行应用程序的可重复构建,将结果打包为可部署的镜像,并将其部署到数据中心。
  • 数据中心运行容器编排平台,该平台使用镜像在可靠、高可用的计算集群中运行容器化应用程序。

如果没有确保不同容器实现之间的互操作性,这个工作流程将无法实用。开放容器倡议(OCI)创建并维护容器镜像和运行时的这些标准。

container 如何运行容器?

许多操作系统都支持容器,但最常遇到的容器是在 Linux 操作系统上运行的容器。对于 macOS,运行 Linux 容器的典型方式是启动一个托管所有容器的 Linux 虚拟机(VM)。

container 以不同的方式运行容器。使用开源的容器化软件包,它为每个您创建的容器运行一个轻量级虚拟机。这种方法具有以下特性:

  • **安全性:**每个容器都具有完整虚拟机的隔离特性,使用一组最小的核心工具和动态库来减少资源利用率和攻击面。
  • **隐私性:**当使用 container 共享主机数据时,您仅将必要的数据挂载到每个虚拟机中。使用共享虚拟机时,您需要将所有可能需要使用的数据挂载到虚拟机中,以便可以将其选择性地挂载到容器中。
  • **性能:**使用 container 创建的容器比完整虚拟机需要的内存更少,启动时间与在共享虚拟机中运行的容器相当。

由于 container 消费和生产标准的 OCI 镜像,您可以轻松地构建和运行其他容器应用程序生成的镜像,并且您构建的镜像可以在任何地方运行。

container 与底层的容器化软件包集成了 macOS 的许多关键技术和框架:

  • 用于管理 Linux 虚拟机和它们附加设备的虚拟化框架。
  • XPC 用于进程间通信。
  • Launchd 用于服务管理。
  • Keychain services 用于访问注册凭证。
  • 统一日志系统用于应用程序日志记录。

您使用 container 命令行界面(CLI)来启动和管理您的容器、构建容器镜像,以及从和向 OCI 容器注册表传输镜像。CLI 使用一个与 container-apiserver 及其辅助工具通信的客户端库。

container-apiserver 是一个启动代理,当您运行 container system start 命令时启动,当您运行 container system stop 时终止。它提供管理容器和网络资源的客户端 API。

container-apiserver 启动时,它会启动一个用于镜像管理的 API 的 XPC 辅助工具 container-core-images ,并管理本地内容存储,以及另一个用于虚拟网络的 XPC 辅助工具 container-network-vmnet 。对于您创建的每个容器, container-apiserver 会启动一个容器运行时辅助工具 container-runtime-linux ,该工具暴露了针对该特定容器的管理 API。

diagram showing container functional organization

总访问量
总访问人数 人次